| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| tierno | 9202102 | 2018-09-12 16:29:23 +0200 | [diff] [blame] | 4 | # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U. |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5 | # This file is part of openmano |
| 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact with: nfvlabs@tid.es |
| 22 | ## |
| 23 | |
| 24 | ''' |
| 25 | NFVO 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 | |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 30 | # import imp |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 31 | import json |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 32 | import yaml |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 33 | import utils |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 34 | from utils import deprecated |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 35 | import vim_thread |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 36 | import console_proxy_thread as cli |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 37 | import vimconn |
| 38 | import logging |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 39 | import collections |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 40 | import math |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 41 | from uuid import uuid4 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 42 | from db_base import db_base_Exception |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 43 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 44 | import nfvo_db |
| 45 | from threading import Lock |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 46 | import time as t |
| tierno | 01b3e17 | 2017-04-21 10:52:34 +0200 | [diff] [blame] | 47 | from lib_osm_openvim import ovim as ovim_module |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 48 | from lib_osm_openvim.ovim import ovimException |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 49 | from Crypto.PublicKey import RSA |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 50 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 51 | import osm_im.vnfd as vnfd_catalog |
| 52 | import osm_im.nsd as nsd_catalog |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 53 | from pyangbind.lib.serialise import pybindJSONDecoder |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 54 | from copy import deepcopy |
| 55 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 56 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 57 | # WIM |
| 58 | import wim.wimconn as wimconn |
| 59 | import wim.wim_thread as wim_thread |
| 60 | from .http_tools import errors as httperrors |
| 61 | from .wim.engine import WimEngine |
| 62 | from .wim.persistence import WimPersistence |
| 63 | from copy import deepcopy |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 64 | from pprint import pformat |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 65 | # |
| 66 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 67 | global global_config |
| 68 | global vimconn_imported |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 69 | # WIM |
| 70 | global wim_engine |
| 71 | wim_engine = None |
| 72 | global wimconn_imported |
| 73 | # |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 74 | global logger |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 75 | global default_volume_size |
| 76 | default_volume_size = '5' #size in GB |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 77 | global ovim |
| 78 | ovim = None |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 79 | global_config = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 80 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 81 | vimconn_imported = {} # dictionary with VIM type as key, loaded module as value |
| 82 | vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 83 | vim_persistent_info = {} |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 84 | # WIM |
| 85 | wimconn_imported = {} # dictionary with WIM type as key, loaded module as value |
| 86 | wim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-WIMs |
| 87 | wim_persistent_info = {} |
| 88 | # |
| 89 | |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 90 | logger = logging.getLogger('openmano.nfvo') |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 91 | task_lock = Lock() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 92 | last_task_id = 0.0 |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 93 | db = None |
| 94 | db_lock = Lock() |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 95 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 96 | |
| 97 | class NfvoException(httperrors.HttpMappedError): |
| 98 | """Common Class for NFVO errors""" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 99 | |
| 100 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 101 | def get_task_id(): |
| 102 | global last_task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 103 | task_id = t.time() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 104 | if task_id <= last_task_id: |
| 105 | task_id = last_task_id + 0.000001 |
| 106 | last_task_id = task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 107 | 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)) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 109 | |
| 110 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 111 | def new_task(name, params, depends=None): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 112 | """Deprected!!!""" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 113 | task_id = get_task_id() |
| 114 | task = {"status": "enqueued", "id": task_id, "name": name, "params": params} |
| 115 | if depends: |
| 116 | task["depends"] = depends |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 117 | return task |
| 118 | |
| 119 | |
| 120 | def is_task_id(id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 121 | return True if id[:5] == "TASK-" else False |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 122 | |
| 123 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 124 | def 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 |
| tierno | 98c11d8 | 2019-05-06 13:24:12 +0000 | [diff] [blame] | 129 | if tenant_name: |
| 130 | name = datacenter_name[:16] + "." + tenant_name[:16] |
| 131 | if name not in vim_threads["names"]: |
| 132 | vim_threads["names"].append(name) |
| 133 | return name |
| 134 | name = datacenter_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 135 | vim_threads["names"].append(name) |
| 136 | return name |
| 137 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 138 | # -- Move |
| 139 | def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id): |
| 140 | name = wim_name[:16] |
| 141 | if name not in wim_threads["names"]: |
| 142 | wim_threads["names"].append(name) |
| 143 | return name |
| 144 | name = wim_name[:16] + "." + tenant_name[:16] |
| 145 | if name not in wim_threads["names"]: |
| 146 | wim_threads["names"].append(name) |
| 147 | return name |
| 148 | name = wim_id + "-" + tenant_id |
| 149 | wim_threads["names"].append(name) |
| 150 | return name |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 151 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 152 | |
| 153 | def start_service(mydb, persistence=None, wim=None): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 154 | global db, global_config |
| Anderson Bravalheri | dfed511 | 2019-02-08 01:44:14 +0000 | [diff] [blame] | 155 | db = nfvo_db.nfvo_db(lock=db_lock) |
| 156 | mydb.lock = db_lock |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 157 | db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name']) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 158 | global ovim |
| 159 | |
| Anderson Bravalheri | dfed511 | 2019-02-08 01:44:14 +0000 | [diff] [blame] | 160 | persistence = persistence or WimPersistence(db) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 161 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 162 | # Initialize openvim for SDN control |
| 163 | # TODO: Avoid static configuration by adding new parameters to openmanod.cfg |
| 164 | # TODO: review ovim.py to delete not needed configuration |
| 165 | ovim_configuration = { |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 166 | 'logger_name': 'openmano.ovim', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 167 | 'network_vlan_range_start': 1000, |
| 168 | 'network_vlan_range_end': 4096, |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 169 | 'db_name': global_config["db_ovim_name"], |
| 170 | 'db_host': global_config["db_ovim_host"], |
| 171 | 'db_user': global_config["db_ovim_user"], |
| 172 | 'db_passwd': global_config["db_ovim_passwd"], |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 173 | 'bridge_ifaces': {}, |
| 174 | 'mode': 'normal', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 175 | 'network_type': 'bridge', |
| 176 | #TODO: log_level_of should not be needed. To be modified in ovim |
| 177 | 'log_level_of': 'DEBUG' |
| 178 | } |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 179 | try: |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 180 | # starts ovim library |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 181 | ovim = ovim_module.ovim(ovim_configuration) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 182 | |
| 183 | global wim_engine |
| 184 | wim_engine = wim or WimEngine(persistence) |
| 185 | wim_engine.ovim = ovim |
| 186 | |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 187 | ovim.start_service() |
| 188 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 189 | #delete old unneeded vim_wim_actions |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 190 | clean_db(mydb) |
| 191 | |
| 192 | # starts vim_threads |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 193 | from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\ |
| 194 | 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid' |
| 195 | select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin', |
| 196 | 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id', |
| 197 | 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id', |
| 198 | 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id') |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 199 | vims = mydb.get_rows(FROM=from_, SELECT=select_) |
| 200 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 201 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 202 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 203 | if vim["config"]: |
| 204 | extra.update(yaml.load(vim["config"])) |
| 205 | if vim.get('dt_config'): |
| 206 | extra.update(yaml.load(vim["dt_config"])) |
| 207 | if vim["type"] not in vimconn_imported: |
| 208 | module_info=None |
| 209 | try: |
| 210 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 211 | pkg = __import__("osm_ro." + module) |
| 212 | vim_conn = getattr(pkg, module) |
| 213 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 214 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 215 | vimconn_imported[vim["type"]] = vim_conn |
| 216 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 217 | # if module_info and module_info[0]: |
| 218 | # file.close(module_info[0]) |
| tierno | cdee8cc | 2017-04-25 13:42:06 +0200 | [diff] [blame] | 219 | raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 220 | vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 221 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 222 | thread_id = vim['datacenter_tenant_id'] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 223 | vim_persistent_info[thread_id] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 224 | try: |
| 225 | #if not tenant: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 226 | # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 227 | myvim = vimconn_imported[ vim["type"] ].vimconnector( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 228 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| 229 | tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'], |
| 230 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| 231 | user=vim['user'], passwd=vim['passwd'], |
| 232 | config=extra, persistent_info=vim_persistent_info[thread_id] |
| 233 | ) |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 234 | except vimconn.vimconnException as e: |
| 235 | myvim = e |
| 236 | logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'], |
| 237 | vim['datacenter_id'], e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 238 | except Exception as e: |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 239 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 240 | httperrors.Internal_Server_Error) |
| tierno | 98c11d8 | 2019-05-06 13:24:12 +0000 | [diff] [blame] | 241 | thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['datacenter_id'], vim['vim_tenant_name'], |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 242 | vim['vim_tenant_id']) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 243 | new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'], |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 244 | vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 245 | new_thread.start() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 246 | vim_threads["running"][thread_id] = new_thread |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 247 | |
| 248 | wim_engine.start_threads() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 249 | except db_base_Exception as e: |
| 250 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 251 | except ovim_module.ovimException as e: |
| 252 | message = str(e) |
| 253 | if message[:22] == "DATABASE wrong version": |
| 254 | message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\ |
| 255 | "at host {dbhost}".format( |
| 256 | msg=message[22:-3], dbname=global_config["db_ovim_name"], |
| 257 | dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"], |
| 258 | ver=message[-3:-1], dbhost=global_config["db_ovim_host"]) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 259 | raise NfvoException(message, httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 260 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 261 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 262 | def stop_service(): |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 263 | global ovim, global_config |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 264 | if ovim: |
| 265 | ovim.stop_service() |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 266 | for thread_id, thread in vim_threads["running"].items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 267 | thread.insert_task("exit") |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 268 | vim_threads["deleting"][thread_id] = thread |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 269 | vim_threads["running"] = {} |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 270 | |
| 271 | if wim_engine: |
| 272 | wim_engine.stop_threads() |
| 273 | |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 274 | if global_config and global_config.get("console_thread"): |
| 275 | for thread in global_config["console_thread"]: |
| 276 | thread.terminate = True |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 277 | |
| tierno | 6ddeded | 2017-05-16 15:40:26 +0200 | [diff] [blame] | 278 | def get_version(): |
| 279 | return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"], |
| 280 | global_config["version_date"] )) |
| 281 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 282 | def clean_db(mydb): |
| 283 | """ |
| 284 | Clean unused or old entries at database to avoid unlimited growing |
| 285 | :param mydb: database connector |
| 286 | :return: None |
| 287 | """ |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 288 | # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 289 | now = t.time()-3600*24*7 |
| 290 | instance_action_id = None |
| 291 | nb_deleted = 0 |
| 292 | while True: |
| 293 | actions_to_delete = mydb.get_rows( |
| 294 | SELECT=("item", "item_id", "instance_action_id"), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 295 | FROM="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid " |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 296 | "left join instance_scenarios as i on ia.instance_id=i.uuid", |
| 297 | WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None, |
| 298 | "va.status": ("DONE", "SUPERSEDED")}, |
| 299 | LIMIT=100 |
| 300 | ) |
| 301 | for to_delete in actions_to_delete: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 302 | mydb.delete_row(FROM="vim_wim_actions", WHERE=to_delete) |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 303 | if instance_action_id != to_delete["instance_action_id"]: |
| 304 | instance_action_id = to_delete["instance_action_id"] |
| 305 | mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id}) |
| 306 | nb_deleted += len(actions_to_delete) |
| 307 | if len(actions_to_delete) < 100: |
| 308 | break |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 309 | # clean locks |
| 310 | mydb.update_rows("vim_wim_actions", UPDATE={"worker": None}, WHERE={"worker<>": None}) |
| 311 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 312 | if nb_deleted: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 313 | logger.debug("Removed {} unused vim_wim_actions".format(nb_deleted)) |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 314 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 315 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 316 | def get_flavorlist(mydb, vnf_id, nfvo_tenant=None): |
| 317 | '''Obtain flavorList |
| 318 | return result, content: |
| 319 | <0, error_text upon error |
| 320 | nb_records, flavor_list on success |
| 321 | ''' |
| 322 | WHERE_dict={} |
| 323 | WHERE_dict['vnf_id'] = vnf_id |
| 324 | if nfvo_tenant is not None: |
| 325 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 326 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 327 | #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict ) |
| 328 | #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 329 | flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict ) |
| 330 | #print "get_flavor_list result:", result |
| 331 | #print "get_flavor_list content:", content |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 332 | flavorList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 333 | for flavor in flavors: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 334 | flavorList.append(flavor['flavor_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 335 | return flavorList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 336 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 337 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 338 | def get_imagelist(mydb, vnf_id, nfvo_tenant=None): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 339 | """ |
| 340 | Get used images of all vms belonging to this VNFD |
| 341 | :param mydb: database conector |
| 342 | :param vnf_id: vnfd uuid |
| 343 | :param nfvo_tenant: tenant, not used |
| 344 | :return: The list of image uuid used |
| 345 | """ |
| 346 | image_list = [] |
| 347 | vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id}) |
| 348 | for vm in vms: |
| tierno | 89aada4 | 2018-12-19 16:00:25 +0000 | [diff] [blame] | 349 | if vm["image_id"] and vm["image_id"] not in image_list: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 350 | image_list.append(vm["image_id"]) |
| 351 | if vm["image_list"]: |
| 352 | vm_image_list = yaml.load(vm["image_list"]) |
| 353 | for image_dict in vm_image_list: |
| 354 | if image_dict["image_id"] not in image_list: |
| 355 | image_list.append(image_dict["image_id"]) |
| 356 | return image_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 357 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 358 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 359 | def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None, |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 360 | vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None, ignore_errors=False): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 361 | '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 362 | return dictionary with {datacenter_id: vim_class, ... }. vim_class contain: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 363 | 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd' |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 364 | raise exception upon error |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 365 | ''' |
| 366 | WHERE_dict={} |
| 367 | if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| 368 | if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 369 | if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 370 | if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name |
| 371 | if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 372 | if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name |
| 373 | if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 374 | 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' |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 375 | select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 376 | 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id', |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 377 | 'user','passwd', 'dt.config as dt_config') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 378 | else: |
| 379 | from_ = 'datacenters as d' |
| 380 | select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name') |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 381 | try: |
| 382 | vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict ) |
| 383 | vim_dict={} |
| 384 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 385 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 386 | 'datacenter_id': vim.get('datacenter_id'), |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 387 | '_vim_type_internal': vim.get('type')} |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 388 | if vim["config"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 389 | extra.update(yaml.load(vim["config"])) |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 390 | if vim.get('dt_config'): |
| 391 | extra.update(yaml.load(vim["dt_config"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 392 | if vim["type"] not in vimconn_imported: |
| 393 | module_info=None |
| 394 | try: |
| 395 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 396 | pkg = __import__("osm_ro." + module) |
| 397 | vim_conn = getattr(pkg, module) |
| 398 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 399 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 400 | vimconn_imported[vim["type"]] = vim_conn |
| 401 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 402 | # if module_info and module_info[0]: |
| 403 | # file.close(module_info[0]) |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 404 | if ignore_errors: |
| 405 | logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 406 | vim["type"], module, type(e).__name__, str(e))) |
| 407 | continue |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 408 | raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 409 | vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 410 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 411 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 412 | if 'datacenter_tenant_id' in vim: |
| 413 | thread_id = vim["datacenter_tenant_id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 414 | if thread_id not in vim_persistent_info: |
| 415 | vim_persistent_info[thread_id] = {} |
| 416 | persistent_info = vim_persistent_info[thread_id] |
| 417 | else: |
| 418 | persistent_info = {} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 419 | #if not tenant: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 420 | # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 421 | vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector( |
| 422 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 423 | tenant_id=vim.get('vim_tenant_id',vim_tenant), |
| 424 | tenant_name=vim.get('vim_tenant_name',vim_tenant_name), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 425 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 426 | user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd), |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 427 | config=extra, persistent_info=persistent_info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 428 | ) |
| 429 | except Exception as e: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 430 | if ignore_errors: |
| 431 | logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e))) |
| 432 | continue |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 433 | http_code = httperrors.Internal_Server_Error |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 434 | if isinstance(e, vimconn.vimconnException): |
| 435 | http_code = e.http_code |
| 436 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 437 | return vim_dict |
| 438 | except db_base_Exception as e: |
| 439 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 440 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 441 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 442 | def rollback(mydb, vims, rollback_list): |
| 443 | undeleted_items=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 444 | #delete things by reverse order |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 445 | for i in range(len(rollback_list)-1, -1, -1): |
| 446 | item = rollback_list[i] |
| 447 | if item["where"]=="vim": |
| 448 | if item["vim_id"] not in vims: |
| 449 | continue |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 450 | if is_task_id(item["uuid"]): |
| 451 | continue |
| 452 | vim = vims[item["vim_id"]] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 453 | try: |
| 454 | if item["what"]=="image": |
| 455 | vim.delete_image(item["uuid"]) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 456 | mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]}) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 457 | elif item["what"]=="flavor": |
| 458 | vim.delete_flavor(item["uuid"]) |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 459 | mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]}) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 460 | elif item["what"]=="network": |
| 461 | vim.delete_network(item["uuid"]) |
| 462 | elif item["what"]=="vm": |
| 463 | vim.delete_vminstance(item["uuid"]) |
| 464 | except vimconn.vimconnException as e: |
| 465 | logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e)) |
| 466 | undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 467 | except db_base_Exception as e: |
| 468 | logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 469 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 470 | else: # where==mano |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 471 | try: |
| 472 | if item["what"]=="image": |
| 473 | mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]}) |
| 474 | elif item["what"]=="flavor": |
| 475 | mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]}) |
| 476 | except db_base_Exception as e: |
| 477 | logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e)) |
| 478 | undeleted_items.append("{} '{}'".format(item['what'], item["uuid"])) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 479 | if len(undeleted_items)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 480 | return True," Rollback successful." |
| 481 | else: |
| 482 | return False," Rollback fails to delete: " + str(undeleted_items) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 483 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 484 | |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 485 | def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 486 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 487 | #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 488 | vnfc_interfaces={} |
| 489 | for vnfc in vnf_descriptor["vnf"]["VNFC"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 490 | name_dict = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 491 | #dataplane interfaces |
| 492 | for numa in vnfc.get("numas",() ): |
| 493 | for interface in numa.get("interfaces",()): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 494 | if interface["name"] in name_dict: |
| 495 | raise NfvoException( |
| 496 | "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format( |
| 497 | vnfc["name"], interface["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 498 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 499 | name_dict[ interface["name"] ] = "underlay" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 500 | #bridge interfaces |
| 501 | for interface in vnfc.get("bridge-ifaces",() ): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 502 | if interface["name"] in name_dict: |
| 503 | raise NfvoException( |
| 504 | "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format( |
| 505 | vnfc["name"], interface["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 506 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 507 | name_dict[ interface["name"] ] = "overlay" |
| 508 | vnfc_interfaces[ vnfc["name"] ] = name_dict |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 509 | # check bood-data info |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 510 | # if "boot-data" in vnfc: |
| 511 | # # check that user-data is incompatible with users and config-files |
| 512 | # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"): |
| 513 | # raise NfvoException( |
| 514 | # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'", |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 515 | # httperrors.Bad_Request) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 516 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 517 | #check if the info in external_connections matches with the one in the vnfcs |
| 518 | name_list=[] |
| 519 | for external_connection in vnf_descriptor["vnf"].get("external-connections",() ): |
| 520 | if external_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 521 | raise NfvoException( |
| 522 | "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format( |
| 523 | external_connection["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 524 | httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 525 | name_list.append(external_connection["name"]) |
| 526 | if external_connection["VNFC"] not in vnfc_interfaces: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 527 | raise NfvoException( |
| 528 | "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format( |
| 529 | external_connection["name"], external_connection["VNFC"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 530 | httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 531 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 532 | if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 533 | raise NfvoException( |
| 534 | "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 535 | external_connection["name"], |
| 536 | external_connection["local_iface_name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 537 | httperrors.Bad_Request ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 538 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 539 | #check if the info in internal_connections matches with the one in the vnfcs |
| 540 | name_list=[] |
| 541 | for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ): |
| 542 | if internal_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 543 | raise NfvoException( |
| 544 | "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format( |
| 545 | internal_connection["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 546 | httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 547 | name_list.append(internal_connection["name"]) |
| 548 | #We should check that internal-connections of type "ptp" have only 2 elements |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 549 | |
| 550 | if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"): |
| 551 | raise NfvoException( |
| 552 | "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format( |
| 553 | internal_connection["name"], |
| 554 | 'ptp' if vnf_descriptor_version==1 else 'e-line', |
| 555 | 'data' if vnf_descriptor_version==1 else "e-lan"), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 556 | httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 557 | for port in internal_connection["elements"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 558 | vnf = port["VNFC"] |
| 559 | iface = port["local_iface_name"] |
| 560 | if vnf not in vnfc_interfaces: |
| 561 | raise NfvoException( |
| 562 | "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format( |
| 563 | internal_connection["name"], vnf), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 564 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 565 | if iface not in vnfc_interfaces[ vnf ]: |
| 566 | raise NfvoException( |
| 567 | "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 568 | internal_connection["name"], iface), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 569 | httperrors.Bad_Request) |
| 570 | return -httperrors.Bad_Request, |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 571 | if vnf_descriptor_version==1 and "type" not in internal_connection: |
| 572 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 573 | internal_connection["type"] = "bridge" |
| 574 | else: |
| 575 | internal_connection["type"] = "data" |
| 576 | if vnf_descriptor_version==2 and "implementation" not in internal_connection: |
| 577 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 578 | internal_connection["implementation"] = "overlay" |
| 579 | else: |
| 580 | internal_connection["implementation"] = "underlay" |
| 581 | if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \ |
| 582 | internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay": |
| 583 | raise NfvoException( |
| 584 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 585 | internal_connection["name"], |
| 586 | iface, 'bridge' if vnf_descriptor_version==1 else 'overlay', |
| 587 | 'data' if vnf_descriptor_version==1 else 'underlay'), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 588 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 589 | if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \ |
| 590 | vnfc_interfaces[vnf][iface] == "underlay": |
| 591 | raise NfvoException( |
| 592 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 593 | internal_connection["name"], iface, |
| 594 | 'data' if vnf_descriptor_version==1 else 'underlay', |
| 595 | 'bridge' if vnf_descriptor_version==1 else 'overlay'), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 596 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 597 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 598 | |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 599 | def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 600 | #look if image exist |
| 601 | if only_create_at_vim: |
| 602 | image_mano_id = image_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 603 | if return_on_error == None: |
| 604 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 605 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 606 | if image_dict['location']: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 607 | images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']}) |
| 608 | else: |
| 609 | images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 610 | if len(images)>=1: |
| 611 | image_mano_id = images[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 612 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 613 | #create image in MANO DB |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 614 | temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None), |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 615 | 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None), |
| 616 | 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 617 | } |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 618 | #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 619 | image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True) |
| 620 | rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 621 | #create image at every vim |
| 622 | for vim_id,vim in vims.iteritems(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 623 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 624 | image_created="false" |
| 625 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 626 | image_db = mydb.get_rows(FROM="datacenters_images", |
| 627 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 628 | #look at VIM if this image exist |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 629 | try: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 630 | if image_dict['location'] is not None: |
| 631 | image_vim_id = vim.get_image_id_from_path(image_dict['location']) |
| 632 | else: |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 633 | filter_dict = {} |
| 634 | filter_dict['name'] = image_dict['universal_name'] |
| 635 | if image_dict.get('checksum') != None: |
| 636 | filter_dict['checksum'] = image_dict['checksum'] |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 637 | #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 638 | vim_images = vim.get_image_list(filter_dict) |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 639 | #logger.debug('>>>>>>>> VIM images: %s', str(vim_images)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 640 | if len(vim_images) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 641 | raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), httperrors.Conflict) |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 642 | elif len(vim_images) == 0: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 643 | raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict))) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 644 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 645 | #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0])) |
| 646 | image_vim_id = vim_images[0]['id'] |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 647 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 648 | except vimconn.vimconnNotFoundException as e: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 649 | #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 650 | try: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 651 | #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None |
| 652 | if image_dict['location']: |
| 653 | image_vim_id = vim.new_image(image_dict) |
| 654 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id}) |
| 655 | image_created="true" |
| 656 | else: |
| garciadeblas | b6153a2 | 2017-02-06 15:38:33 +0100 | [diff] [blame] | 657 | #If we reach this point, then the image has image name, and optionally checksum, and could not be found |
| 658 | raise vimconn.vimconnException(str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 659 | except vimconn.vimconnException as e: |
| 660 | if return_on_error: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 661 | logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 662 | raise |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 663 | image_vim_id = None |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 664 | logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 665 | continue |
| 666 | except vimconn.vimconnException as e: |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 667 | if return_on_error: |
| 668 | logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e)) |
| 669 | raise |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 670 | logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e)) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 671 | image_vim_id = None |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 672 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 673 | #if we reach here, the image has been created or existed |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 674 | if len(image_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 675 | #add new vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 676 | mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id, |
| 677 | 'image_id':image_mano_id, |
| 678 | 'vim_id': image_vim_id, |
| 679 | 'created':image_created}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 680 | elif image_db[0]["vim_id"]!=image_vim_id: |
| 681 | #modify existing vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 682 | mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_vim_id':vim_id, 'image_id':image_mano_id}) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 683 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 684 | return image_vim_id if only_create_at_vim else image_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 685 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 686 | |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 687 | def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None): |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 688 | temp_flavor_dict= {'disk':flavor_dict.get('disk',0), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 689 | 'ram':flavor_dict.get('ram'), |
| 690 | 'vcpus':flavor_dict.get('vcpus'), |
| 691 | } |
| 692 | if 'extended' in flavor_dict and flavor_dict['extended']==None: |
| 693 | del flavor_dict['extended'] |
| 694 | if 'extended' in flavor_dict: |
| 695 | temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256) |
| 696 | |
| 697 | #look if flavor exist |
| 698 | if only_create_at_vim: |
| 699 | flavor_mano_id = flavor_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 700 | if return_on_error == None: |
| 701 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 702 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 703 | flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 704 | if len(flavors)>=1: |
| 705 | flavor_mano_id = flavors[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 706 | else: |
| 707 | #create flavor |
| 708 | #create one by one the images of aditional disks |
| 709 | dev_image_list=[] #list of images |
| 710 | if 'extended' in flavor_dict and flavor_dict['extended']!=None: |
| 711 | dev_nb=0 |
| 712 | for device in flavor_dict['extended'].get('devices',[]): |
| garciadeblas | 41f18be | 2016-10-04 09:09:58 +0200 | [diff] [blame] | 713 | if "image" not in device and "image name" not in device: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 714 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 715 | image_dict={} |
| 716 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 717 | image_dict['universal_name']=device.get('image name') |
| 718 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 719 | image_dict['location']=device.get('image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 720 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 721 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 722 | image_metadata_dict = device.get('image metadata', None) |
| 723 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 724 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 725 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 726 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 727 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 728 | #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 729 | dev_image_list.append(image_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 730 | dev_nb += 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 731 | temp_flavor_dict['name'] = flavor_dict['name'] |
| 732 | temp_flavor_dict['description'] = flavor_dict.get('description',None) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 733 | content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True) |
| 734 | flavor_mano_id= content |
| 735 | rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 736 | #create flavor at every vim |
| 737 | if 'uuid' in flavor_dict: |
| 738 | del flavor_dict['uuid'] |
| 739 | flavor_vim_id=None |
| 740 | for vim_id,vim in vims.items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 741 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 742 | flavor_created="false" |
| 743 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 744 | flavor_db = mydb.get_rows(FROM="datacenters_flavors", |
| 745 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 746 | #look at VIM if this flavor exist SKIPPED |
| 747 | #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location']) |
| 748 | #if res_vim < 0: |
| 749 | # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id |
| 750 | # continue |
| 751 | #elif res_vim==0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 752 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 753 | # Create the flavor in VIM |
| 754 | # Translate images at devices from MANO id to VIM id |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 755 | disk_list = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 756 | if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 757 | # make a copy of original devices |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 758 | devices_original=[] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 759 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 760 | for device in flavor_dict["extended"].get("devices",[]): |
| 761 | dev={} |
| 762 | dev.update(device) |
| 763 | devices_original.append(dev) |
| 764 | if 'image' in device: |
| 765 | del device['image'] |
| 766 | if 'image metadata' in device: |
| 767 | del device['image metadata'] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 768 | if 'image checksum' in device: |
| 769 | del device['image checksum'] |
| 770 | dev_nb = 0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 771 | for index in range(0,len(devices_original)) : |
| 772 | device=devices_original[index] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 773 | if "image" not in device and "image name" not in device: |
| tierno | ecc6839 | 2018-09-06 13:47:11 +0200 | [diff] [blame] | 774 | # if 'size' in device: |
| 775 | disk_list.append({'size': device.get('size', default_volume_size), 'name': device.get('name')}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 776 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 777 | image_dict={} |
| 778 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 779 | image_dict['universal_name']=device.get('image name') |
| 780 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 781 | image_dict['location']=device.get('image') |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 782 | # image_dict['new_location']=device.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 783 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 784 | image_metadata_dict = device.get('image metadata', None) |
| 785 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 786 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 787 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 788 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 789 | image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 790 | image_dict["uuid"]=image_mano_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 791 | image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 792 | |
| 793 | #save disk information (image must be based on and size |
| 794 | disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)}) |
| 795 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 796 | flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id |
| 797 | dev_nb += 1 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 798 | if len(flavor_db)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 799 | #check that this vim_id exist in VIM, if not create |
| 800 | flavor_vim_id=flavor_db[0]["vim_id"] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 801 | try: |
| 802 | vim.get_flavor(flavor_vim_id) |
| 803 | continue #flavor exist |
| 804 | except vimconn.vimconnException: |
| 805 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 806 | #create flavor at vim |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 807 | logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"]) |
| 808 | try: |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 809 | flavor_vim_id = None |
| 810 | flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict) |
| tierno | b7aa1bb | 2019-07-24 15:47:16 +0000 | [diff] [blame] | 811 | flavor_created="false" |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 812 | except vimconn.vimconnException as e: |
| 813 | pass |
| 814 | try: |
| 815 | if not flavor_vim_id: |
| 816 | flavor_vim_id = vim.new_flavor(flavor_dict) |
| 817 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id}) |
| 818 | flavor_created="true" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 819 | except vimconn.vimconnException as e: |
| 820 | if return_on_error: |
| 821 | logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 822 | raise |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 823 | logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 824 | flavor_vim_id = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 825 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 826 | #if reach here the flavor has been create or exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 827 | if len(flavor_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 828 | #add new vim_id at datacenters_flavors |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 829 | extended_devices_yaml = None |
| 830 | if len(disk_list) > 0: |
| 831 | extended_devices = dict() |
| 832 | extended_devices['disks'] = disk_list |
| 833 | extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256) |
| 834 | mydb.new_row('datacenters_flavors', |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 835 | {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id, |
| 836 | 'created': flavor_created, 'extended': extended_devices_yaml}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 837 | elif flavor_db[0]["vim_id"]!=flavor_vim_id: |
| 838 | #modify existing vim_id at datacenters_flavors |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 839 | mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, |
| 840 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 841 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 842 | return flavor_vim_id if only_create_at_vim else flavor_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 843 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 844 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 845 | def get_str(obj, field, length): |
| 846 | """ |
| 847 | Obtain the str value, |
| 848 | :param obj: |
| 849 | :param length: |
| 850 | :return: |
| 851 | """ |
| 852 | value = obj.get(field) |
| 853 | if value is not None: |
| 854 | value = str(value)[:length] |
| 855 | return value |
| 856 | |
| 857 | def _lookfor_or_create_image(db_image, mydb, descriptor): |
| 858 | """ |
| 859 | fill image content at db_image dictionary. Check if the image with this image and checksum exist |
| 860 | :param db_image: dictionary to insert data |
| 861 | :param mydb: database connector |
| 862 | :param descriptor: yang descriptor |
| 863 | :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image |
| 864 | """ |
| 865 | |
| 866 | db_image["name"] = get_str(descriptor, "image", 255) |
| 867 | db_image["checksum"] = get_str(descriptor, "image-checksum", 32) |
| 868 | if not db_image["checksum"]: # Ensure that if empty string, None is stored |
| 869 | db_image["checksum"] = None |
| 870 | if db_image["name"].startswith("/"): |
| 871 | db_image["location"] = db_image["name"] |
| 872 | existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]}) |
| 873 | else: |
| 874 | db_image["universal_name"] = db_image["name"] |
| 875 | existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'], |
| 876 | 'checksum': db_image['checksum']}) |
| 877 | if existing_images: |
| 878 | return existing_images[0]["uuid"] |
| 879 | else: |
| 880 | image_uuid = str(uuid4()) |
| 881 | db_image["uuid"] = image_uuid |
| 882 | return None |
| 883 | |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 884 | def get_resource_allocation_params(quota_descriptor): |
| 885 | """ |
| 886 | read the quota_descriptor from vnfd and fetch the resource allocation properties from the descriptor object |
| 887 | :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor |
| 888 | :return: quota params for limit, reserve, shares from the descriptor object |
| 889 | """ |
| 890 | quota = {} |
| 891 | if quota_descriptor.get("limit"): |
| 892 | quota["limit"] = int(quota_descriptor["limit"]) |
| 893 | if quota_descriptor.get("reserve"): |
| 894 | quota["reserve"] = int(quota_descriptor["reserve"]) |
| 895 | if quota_descriptor.get("shares"): |
| 896 | quota["shares"] = int(quota_descriptor["shares"]) |
| 897 | return quota |
| 898 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 899 | def new_vnfd_v3(mydb, tenant_id, vnf_descriptor): |
| 900 | """ |
| 901 | Parses an OSM IM vnfd_catalog and insert at DB |
| 902 | :param mydb: |
| 903 | :param tenant_id: |
| 904 | :param vnf_descriptor: |
| 905 | :return: The list of cretated vnf ids |
| 906 | """ |
| 907 | try: |
| 908 | myvnfd = vnfd_catalog.vnfd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 909 | try: |
| tierno | f6bbe22 | 2019-04-09 14:19:40 +0000 | [diff] [blame] | 910 | pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True, |
| 911 | skip_unknown=True) |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 912 | except Exception as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 913 | raise NfvoException("Error. Invalid VNF descriptor format " + str(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 914 | db_vnfs = [] |
| 915 | db_nets = [] |
| 916 | db_vms = [] |
| 917 | db_vms_index = 0 |
| 918 | db_interfaces = [] |
| 919 | db_images = [] |
| 920 | db_flavors = [] |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 921 | db_ip_profiles_index = 0 |
| 922 | db_ip_profiles = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 923 | uuid_list = [] |
| 924 | vnfd_uuid_list = [] |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 925 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog") |
| 926 | if not vnfd_catalog_descriptor: |
| 927 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog") |
| 928 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd") |
| 929 | if not vnfd_descriptor_list: |
| 930 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd") |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 931 | for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues(): |
| 932 | vnfd = vnfd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 933 | |
| 934 | # table vnf |
| 935 | vnf_uuid = str(uuid4()) |
| 936 | uuid_list.append(vnf_uuid) |
| 937 | vnfd_uuid_list.append(vnf_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 938 | vnfd_id = get_str(vnfd, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 939 | db_vnf = { |
| 940 | "uuid": vnf_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 941 | "osm_id": vnfd_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 942 | "name": get_str(vnfd, "name", 255), |
| 943 | "description": get_str(vnfd, "description", 255), |
| 944 | "tenant_id": tenant_id, |
| 945 | "vendor": get_str(vnfd, "vendor", 255), |
| 946 | "short_name": get_str(vnfd, "short-name", 255), |
| 947 | "descriptor": str(vnf_descriptor)[:60000] |
| 948 | } |
| 949 | |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 950 | for vnfd_descriptor in vnfd_descriptor_list: |
| 951 | if vnfd_descriptor["id"] == str(vnfd["id"]): |
| 952 | break |
| 953 | |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 954 | # table ip_profiles (ip-profiles) |
| 955 | ip_profile_name2db_table_index = {} |
| 956 | for ip_profile in vnfd.get("ip-profiles").itervalues(): |
| 957 | db_ip_profile = { |
| 958 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 959 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 960 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 961 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 962 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 963 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 964 | } |
| 965 | dns_list = [] |
| 966 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 967 | dns_list.append(str(dns.get("address"))) |
| 968 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 969 | if ip_profile["ip-profile-params"].get('security-group'): |
| 970 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 971 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 972 | db_ip_profiles_index += 1 |
| 973 | db_ip_profiles.append(db_ip_profile) |
| 974 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 975 | # table nets (internal-vld) |
| 976 | net_id2uuid = {} # for mapping interface with network |
| 977 | for vld in vnfd.get("internal-vld").itervalues(): |
| 978 | net_uuid = str(uuid4()) |
| 979 | uuid_list.append(net_uuid) |
| 980 | db_net = { |
| 981 | "name": get_str(vld, "name", 255), |
| 982 | "vnf_id": vnf_uuid, |
| 983 | "uuid": net_uuid, |
| 984 | "description": get_str(vld, "description", 255), |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 985 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 986 | "type": "bridge", # TODO adjust depending on connection point type |
| 987 | } |
| 988 | net_id2uuid[vld.get("id")] = net_uuid |
| 989 | db_nets.append(db_net) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 990 | # ip-profile, link db_ip_profile with db_sce_net |
| 991 | if vld.get("ip-profile-ref"): |
| 992 | ip_profile_name = vld.get("ip-profile-ref") |
| 993 | if ip_profile_name not in ip_profile_name2db_table_index: |
| 994 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':" |
| 995 | "'{}'. Reference to a non-existing 'ip_profiles'".format( |
| 996 | str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 997 | httperrors.Bad_Request) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 998 | db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid |
| 999 | else: #check no ip-address has been defined |
| tierno | 45140f5 | 2018-03-26 12:11:46 +0200 | [diff] [blame] | 1000 | for icp in vld.get("internal-connection-point").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1001 | if icp.get("ip-address"): |
| 1002 | raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' " |
| 1003 | "contains an ip-address but no ip-profile has been defined at VLD".format( |
| 1004 | str(vnfd["id"]), str(vld["id"]), str(icp["id"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1005 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1006 | |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1007 | # connection points vaiable declaration |
| 1008 | cp_name2iface_uuid = {} |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 1009 | cp_name2vdu_id = {} |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1010 | cp_name2vm_uuid = {} |
| 1011 | cp_name2db_interface = {} |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1012 | vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1013 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1014 | # table vms (vdus) |
| 1015 | vdu_id2uuid = {} |
| 1016 | vdu_id2db_table_index = {} |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 1017 | mgmt_access = {} |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1018 | for vdu in vnfd.get("vdu").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1019 | |
| 1020 | for vdu_descriptor in vnfd_descriptor["vdu"]: |
| 1021 | if vdu_descriptor["id"] == str(vdu["id"]): |
| 1022 | break |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1023 | vm_uuid = str(uuid4()) |
| 1024 | uuid_list.append(vm_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1025 | vdu_id = get_str(vdu, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1026 | db_vm = { |
| 1027 | "uuid": vm_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1028 | "osm_id": vdu_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1029 | "name": get_str(vdu, "name", 255), |
| 1030 | "description": get_str(vdu, "description", 255), |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1031 | "pdu_type": get_str(vdu, "pdu-type", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1032 | "vnf_id": vnf_uuid, |
| 1033 | } |
| 1034 | vdu_id2uuid[db_vm["osm_id"]] = vm_uuid |
| 1035 | vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index |
| 1036 | if vdu.get("count"): |
| 1037 | db_vm["count"] = int(vdu["count"]) |
| 1038 | |
| 1039 | # table image |
| 1040 | image_present = False |
| 1041 | if vdu.get("image"): |
| 1042 | image_present = True |
| 1043 | db_image = {} |
| 1044 | image_uuid = _lookfor_or_create_image(db_image, mydb, vdu) |
| 1045 | if not image_uuid: |
| 1046 | image_uuid = db_image["uuid"] |
| 1047 | db_images.append(db_image) |
| 1048 | db_vm["image_id"] = image_uuid |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 1049 | if vdu.get("alternative-images"): |
| 1050 | vm_alternative_images = [] |
| 1051 | for alt_image in vdu.get("alternative-images").itervalues(): |
| 1052 | db_image = {} |
| 1053 | image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image) |
| 1054 | if not image_uuid: |
| 1055 | image_uuid = db_image["uuid"] |
| 1056 | db_images.append(db_image) |
| 1057 | vm_alternative_images.append({ |
| 1058 | "image_id": image_uuid, |
| 1059 | "vim_type": str(alt_image["vim-type"]), |
| 1060 | # "universal_name": str(alt_image["image"]), |
| 1061 | # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None |
| 1062 | }) |
| 1063 | |
| 1064 | db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1065 | |
| 1066 | # volumes |
| 1067 | devices = [] |
| 1068 | if vdu.get("volumes"): |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1069 | for volume_key in vdu["volumes"]: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1070 | volume = vdu["volumes"][volume_key] |
| 1071 | if not image_present: |
| 1072 | # Convert the first volume to vnfc.image |
| 1073 | image_present = True |
| 1074 | db_image = {} |
| 1075 | image_uuid = _lookfor_or_create_image(db_image, mydb, volume) |
| 1076 | if not image_uuid: |
| 1077 | image_uuid = db_image["uuid"] |
| 1078 | db_images.append(db_image) |
| 1079 | db_vm["image_id"] = image_uuid |
| 1080 | else: |
| 1081 | # Add Openmano devices |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1082 | device = {"name": str(volume.get("name"))} |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1083 | device["type"] = str(volume.get("device-type")) |
| 1084 | if volume.get("size"): |
| 1085 | device["size"] = int(volume["size"]) |
| 1086 | if volume.get("image"): |
| 1087 | device["image name"] = str(volume["image"]) |
| 1088 | if volume.get("image-checksum"): |
| 1089 | device["image checksum"] = str(volume["image-checksum"]) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1090 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1091 | devices.append(device) |
| 1092 | |
| tierno | 89aada4 | 2018-12-19 16:00:25 +0000 | [diff] [blame] | 1093 | if not db_vm.get("image_id"): |
| 1094 | if not db_vm["pdu_type"]: |
| 1095 | raise NfvoException("Not defined image for VDU") |
| 1096 | # create a fake image |
| 1097 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1098 | # cloud-init |
| 1099 | boot_data = {} |
| 1100 | if vdu.get("cloud-init"): |
| 1101 | boot_data["user-data"] = str(vdu["cloud-init"]) |
| 1102 | elif vdu.get("cloud-init-file"): |
| 1103 | # TODO Where this file content is present??? |
| 1104 | # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]] |
| 1105 | boot_data["user-data"] = str(vdu["cloud-init-file"]) |
| 1106 | |
| 1107 | if vdu.get("supplemental-boot-data"): |
| 1108 | if vdu["supplemental-boot-data"].get('boot-data-drive'): |
| 1109 | boot_data['boot-data-drive'] = True |
| 1110 | if vdu["supplemental-boot-data"].get('config-file'): |
| 1111 | om_cfgfile_list = list() |
| 1112 | for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues(): |
| 1113 | # TODO Where this file content is present??? |
| 1114 | cfg_source = str(custom_config_file["source"]) |
| 1115 | om_cfgfile_list.append({"dest": custom_config_file["dest"], |
| 1116 | "content": cfg_source}) |
| 1117 | boot_data['config-files'] = om_cfgfile_list |
| 1118 | if boot_data: |
| 1119 | db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256) |
| 1120 | |
| 1121 | db_vms.append(db_vm) |
| 1122 | db_vms_index += 1 |
| 1123 | |
| 1124 | # table interfaces (internal/external interfaces) |
| 1125 | flavor_epa_interfaces = [] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1126 | # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()): |
| 1127 | for iface in vdu.get("interface").itervalues(): |
| 1128 | flavor_epa_interface = {} |
| 1129 | iface_uuid = str(uuid4()) |
| 1130 | uuid_list.append(iface_uuid) |
| 1131 | db_interface = { |
| 1132 | "uuid": iface_uuid, |
| 1133 | "internal_name": get_str(iface, "name", 255), |
| 1134 | "vm_id": vm_uuid, |
| 1135 | } |
| 1136 | flavor_epa_interface["name"] = db_interface["internal_name"] |
| 1137 | if iface.get("virtual-interface").get("vpci"): |
| 1138 | db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12) |
| 1139 | flavor_epa_interface["vpci"] = db_interface["vpci"] |
| 1140 | |
| 1141 | if iface.get("virtual-interface").get("bandwidth"): |
| 1142 | bps = int(iface.get("virtual-interface").get("bandwidth")) |
| 1143 | db_interface["bw"] = int(math.ceil(bps/1000000.0)) |
| 1144 | flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"]) |
| 1145 | |
| 1146 | if iface.get("virtual-interface").get("type") == "OM-MGMT": |
| 1147 | db_interface["type"] = "mgmt" |
| garciadeblas | 31e141b | 2018-10-25 18:33:19 +0200 | [diff] [blame] | 1148 | elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"): |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1149 | db_interface["type"] = "bridge" |
| 1150 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1151 | elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 1152 | db_interface["type"] = "data" |
| 1153 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1154 | flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \ |
| 1155 | else "yes" |
| 1156 | flavor_epa_interfaces.append(flavor_epa_interface) |
| 1157 | else: |
| 1158 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual" |
| 1159 | "-interface':'type':'{}'. Interface type is not supported".format( |
| 1160 | vnfd_id, vdu_id, iface.get("virtual-interface").get("type")), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1161 | httperrors.Bad_Request) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1162 | |
| tierno | e72710b | 2018-07-23 16:16:00 +0200 | [diff] [blame] | 1163 | if iface.get("mgmt-interface"): |
| 1164 | db_interface["type"] = "mgmt" |
| 1165 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1166 | if iface.get("external-connection-point-ref"): |
| 1167 | try: |
| 1168 | cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")] |
| 1169 | db_interface["external_name"] = get_str(cp, "name", 255) |
| 1170 | cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 1171 | cp_name2vdu_id[db_interface["external_name"]] = vdu_id |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1172 | cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid |
| 1173 | cp_name2db_interface[db_interface["external_name"]] = db_interface |
| 1174 | for cp_descriptor in vnfd_descriptor["connection-point"]: |
| 1175 | if cp_descriptor["name"] == db_interface["external_name"]: |
| 1176 | break |
| 1177 | else: |
| 1178 | raise KeyError() |
| 1179 | |
| 1180 | if vdu_id in vdu_id2cp_name: |
| 1181 | vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU |
| 1182 | else: |
| 1183 | vdu_id2cp_name[vdu_id] = db_interface["external_name"] |
| 1184 | |
| 1185 | # port security |
| 1186 | if str(cp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1187 | db_interface["port_security"] = 0 |
| 1188 | elif str(cp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1189 | db_interface["port_security"] = 1 |
| 1190 | except KeyError: |
| 1191 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| 1192 | "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present" |
| 1193 | " at connection-point".format( |
| 1194 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| 1195 | cp=iface.get("vnfd-connection-point-ref")), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1196 | httperrors.Bad_Request) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1197 | elif iface.get("internal-connection-point-ref"): |
| 1198 | try: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1199 | for icp_descriptor in vdu_descriptor["internal-connection-point"]: |
| 1200 | if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")): |
| 1201 | break |
| 1202 | else: |
| 1203 | raise KeyError("does not exist at vdu:internal-connection-point") |
| 1204 | icp = None |
| 1205 | icp_vld = None |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1206 | for vld in vnfd.get("internal-vld").itervalues(): |
| 1207 | for cp in vld.get("internal-connection-point").itervalues(): |
| 1208 | if cp.get("id-ref") == iface.get("internal-connection-point-ref"): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1209 | if icp: |
| 1210 | raise KeyError("is referenced by more than one 'internal-vld'") |
| 1211 | icp = cp |
| 1212 | icp_vld = vld |
| 1213 | if not icp: |
| 1214 | raise KeyError("is not referenced by any 'internal-vld'") |
| 1215 | |
| 1216 | db_interface["net_id"] = net_id2uuid[icp_vld.get("id")] |
| 1217 | if str(icp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1218 | db_interface["port_security"] = 0 |
| 1219 | elif str(icp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1220 | db_interface["port_security"] = 1 |
| 1221 | if icp.get("ip-address"): |
| 1222 | if not icp_vld.get("ip-profile-ref"): |
| 1223 | raise NfvoException |
| 1224 | db_interface["ip_address"] = str(icp.get("ip-address")) |
| 1225 | except KeyError as e: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1226 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1227 | "'interface[{iface}]':'internal-connection-point-ref':'{cp}'" |
| 1228 | " {msg}".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1229 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1230 | cp=iface.get("internal-connection-point-ref"), msg=str(e)), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1231 | httperrors.Bad_Request) |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 1232 | if iface.get("position"): |
| 1233 | db_interface["created_at"] = int(iface.get("position")) * 50 |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1234 | if iface.get("mac-address"): |
| 1235 | db_interface["mac"] = str(iface.get("mac-address")) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1236 | db_interfaces.append(db_interface) |
| 1237 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1238 | # table flavors |
| 1239 | db_flavor = { |
| 1240 | "name": get_str(vdu, "name", 250) + "-flv", |
| 1241 | "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)), |
| 1242 | "ram": int(vdu["vm-flavor"].get("memory-mb", 1)), |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1243 | "disk": int(vdu["vm-flavor"].get("storage-gb", 0)), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1244 | } |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1245 | # TODO revise the case of several numa-node-policy node |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1246 | extended = {} |
| 1247 | numa = {} |
| 1248 | if devices: |
| 1249 | extended["devices"] = devices |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1250 | if flavor_epa_interfaces: |
| 1251 | numa["interfaces"] = flavor_epa_interfaces |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1252 | if vdu.get("guest-epa"): # TODO or dedicated_int: |
| 1253 | epa_vcpu_set = False |
| 1254 | if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int: |
| 1255 | numa_node_policy = vdu["guest-epa"].get("numa-node-policy") |
| 1256 | if numa_node_policy.get("node"): |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1257 | numa_node = numa_node_policy["node"].values()[0] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1258 | if numa_node.get("num-cores"): |
| 1259 | numa["cores"] = numa_node["num-cores"] |
| 1260 | epa_vcpu_set = True |
| 1261 | if numa_node.get("paired-threads"): |
| 1262 | if numa_node["paired-threads"].get("num-paired-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1263 | numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1264 | epa_vcpu_set = True |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1265 | if len(numa_node["paired-threads"].get("paired-thread-ids")): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1266 | numa["paired-threads-id"] = [] |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1267 | for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues(): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1268 | numa["paired-threads-id"].append( |
| 1269 | (str(pair["thread-a"]), str(pair["thread-b"])) |
| 1270 | ) |
| 1271 | if numa_node.get("num-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1272 | numa["threads"] = int(numa_node["num-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1273 | epa_vcpu_set = True |
| 1274 | if numa_node.get("memory-mb"): |
| 1275 | numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1) |
| 1276 | if vdu["guest-epa"].get("mempage-size"): |
| 1277 | if vdu["guest-epa"]["mempage-size"] != "SMALL": |
| 1278 | numa["memory"] = max(int(db_flavor["ram"] / 1024), 1) |
| 1279 | if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set: |
| 1280 | if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED": |
| 1281 | if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \ |
| 1282 | vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER": |
| 1283 | numa["cores"] = max(db_flavor["vcpus"], 1) |
| 1284 | else: |
| 1285 | numa["threads"] = max(db_flavor["vcpus"], 1) |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1286 | epa_vcpu_set = True |
| 1287 | if vdu["guest-epa"].get("cpu-quota") and not epa_vcpu_set: |
| calvinosanch | 1d55a23 | 2019-08-05 11:03:46 +0000 | [diff] [blame] | 1288 | cpuquota = get_resource_allocation_params(vdu["guest-epa"].get("cpu-quota")) |
| 1289 | if cpuquota: |
| 1290 | extended["cpu-quota"] = cpuquota |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1291 | if vdu["guest-epa"].get("mem-quota"): |
| calvinosanch | 1d55a23 | 2019-08-05 11:03:46 +0000 | [diff] [blame] | 1292 | vduquota = get_resource_allocation_params(vdu["guest-epa"].get("mem-quota")) |
| 1293 | if vduquota: |
| 1294 | extended["mem-quota"] = vduquota |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1295 | if vdu["guest-epa"].get("disk-io-quota"): |
| calvinosanch | 1d55a23 | 2019-08-05 11:03:46 +0000 | [diff] [blame] | 1296 | diskioquota = get_resource_allocation_params(vdu["guest-epa"].get("disk-io-quota")) |
| 1297 | if diskioquota: |
| 1298 | extended["disk-io-quota"] = diskioquota |
| anwars | ae5f52c | 2019-04-22 10:35:27 +0530 | [diff] [blame] | 1299 | if vdu["guest-epa"].get("vif-quota"): |
| calvinosanch | 1d55a23 | 2019-08-05 11:03:46 +0000 | [diff] [blame] | 1300 | vifquota = get_resource_allocation_params(vdu["guest-epa"].get("vif-quota")) |
| 1301 | if vifquota: |
| 1302 | extended["vif-quota"] = vifquota |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1303 | if numa: |
| 1304 | extended["numas"] = [numa] |
| 1305 | if extended: |
| 1306 | extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256) |
| 1307 | db_flavor["extended"] = extended_text |
| 1308 | # look if flavor exist |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1309 | temp_flavor_dict = {'disk': db_flavor.get('disk', 0), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1310 | 'ram': db_flavor.get('ram'), |
| 1311 | 'vcpus': db_flavor.get('vcpus'), |
| 1312 | 'extended': db_flavor.get('extended') |
| 1313 | } |
| 1314 | existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 1315 | if existing_flavors: |
| 1316 | flavor_uuid = existing_flavors[0]["uuid"] |
| 1317 | else: |
| 1318 | flavor_uuid = str(uuid4()) |
| 1319 | uuid_list.append(flavor_uuid) |
| 1320 | db_flavor["uuid"] = flavor_uuid |
| 1321 | db_flavors.append(db_flavor) |
| 1322 | db_vm["flavor_id"] = flavor_uuid |
| 1323 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1324 | # VNF affinity and antiaffinity |
| 1325 | for pg in vnfd.get("placement-groups").itervalues(): |
| 1326 | pg_name = get_str(pg, "name", 255) |
| 1327 | for vdu in pg.get("member-vdus").itervalues(): |
| 1328 | vdu_id = get_str(vdu, "member-vdu-ref", 255) |
| 1329 | if vdu_id not in vdu_id2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1330 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':" |
| 1331 | "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1332 | vnf=vnfd_id, pg=pg_name, vdu=vdu_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1333 | httperrors.Bad_Request) |
| tierno | 55fe397 | 2019-03-29 08:50:12 +0000 | [diff] [blame] | 1334 | db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1335 | # TODO consider the case of isolation and not colocation |
| 1336 | # if pg.get("strategy") == "ISOLATION": |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1337 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1338 | # VNF mgmt configuration |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1339 | if vnfd["mgmt-interface"].get("vdu-id"): |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1340 | mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255) |
| 1341 | if mgmt_vdu_id not in vdu_id2uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1342 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':" |
| 1343 | "'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1344 | vnf=vnfd_id, vdu=mgmt_vdu_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1345 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1346 | mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]] |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 1347 | mgmt_access["vdu-id"] = vnfd["mgmt-interface"]["vdu-id"] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1348 | # if only one cp is defined by this VDU, mark this interface as of type "mgmt" |
| 1349 | if vdu_id2cp_name.get(mgmt_vdu_id): |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1350 | if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]: |
| 1351 | cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt" |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1352 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1353 | if vnfd["mgmt-interface"].get("ip-address"): |
| 1354 | mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address")) |
| 1355 | if vnfd["mgmt-interface"].get("cp"): |
| 1356 | if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid: |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1357 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. " |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1358 | "Reference to a non-existing connection-point".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1359 | vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1360 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1361 | mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]] |
| 1362 | mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]] |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 1363 | mgmt_access["vdu-id"] = cp_name2vdu_id[vnfd["mgmt-interface"]["cp"]] |
| tierno | e2ff1ce | 2017-11-02 17:01:10 +0100 | [diff] [blame] | 1364 | # mark this interface as of type mgmt |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1365 | if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]: |
| 1366 | cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt" |
| tierno | e2ff1ce | 2017-11-02 17:01:10 +0100 | [diff] [blame] | 1367 | |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 1368 | default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1369 | "default-user", 64) |
| 1370 | if default_user: |
| 1371 | mgmt_access["default_user"] = default_user |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 1372 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1373 | required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}), |
| 1374 | "required", 6) |
| 1375 | if required: |
| 1376 | mgmt_access["required"] = required |
| 1377 | |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 1378 | password_ = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}), |
| 1379 | "password", 64) |
| 1380 | if password_: |
| 1381 | mgmt_access["password"] = password_ |
| 1382 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1383 | if mgmt_access: |
| 1384 | db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256) |
| 1385 | |
| 1386 | db_vnfs.append(db_vnf) |
| 1387 | db_tables=[ |
| 1388 | {"vnfs": db_vnfs}, |
| 1389 | {"nets": db_nets}, |
| 1390 | {"images": db_images}, |
| 1391 | {"flavors": db_flavors}, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1392 | {"ip_profiles": db_ip_profiles}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1393 | {"vms": db_vms}, |
| 1394 | {"interfaces": db_interfaces}, |
| 1395 | ] |
| 1396 | |
| 1397 | logger.debug("create_vnf Deployment done vnfDict: %s", |
| 1398 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 1399 | mydb.new_rows(db_tables, uuid_list) |
| 1400 | return vnfd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1401 | except NfvoException: |
| 1402 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1403 | except Exception as e: |
| 1404 | logger.error("Exception {}".format(e)) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1405 | raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1406 | |
| 1407 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1408 | @deprecated("Use new_vnfd_v3") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1409 | def new_vnf(mydb, tenant_id, vnf_descriptor): |
| 1410 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1411 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1412 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1413 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1414 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1415 | vims = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1416 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1417 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1418 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1419 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1420 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1421 | httperrors.Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1422 | else: |
| 1423 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1424 | # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1425 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1426 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1427 | |
| 1428 | # Step 4. Review the descriptor and add missing fields |
| 1429 | #print vnf_descriptor |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1430 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1431 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1432 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1433 | if "physical" in vnf_descriptor['vnf']: |
| 1434 | del vnf_descriptor['vnf']['physical'] |
| 1435 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1436 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1437 | # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1438 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1439 | logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC']))) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1440 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1441 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1442 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1443 | rollback_list = [] # It will contain the new images created in mano. It is used for rollback |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1444 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1445 | logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1446 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1447 | VNFCitem={} |
| 1448 | VNFCitem["name"] = vnfc['name'] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1449 | VNFCitem["availability_zone"] = vnfc.get('availability_zone') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1450 | VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1451 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1452 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1453 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1454 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1455 | myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1456 | myflavorDict["description"] = VNFCitem["description"] |
| 1457 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1458 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1459 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1460 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1461 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1462 | devices = vnfc.get("devices") |
| 1463 | if devices != None: |
| 1464 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1465 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1466 | # TODO: |
| 1467 | # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1468 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1469 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1470 | # Previous code has been commented |
| 1471 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1472 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1473 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1474 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1475 | #else: |
| 1476 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1477 | # if result2: |
| 1478 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1479 | # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1480 | # else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1481 | # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1482 | myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1483 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1484 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1485 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1486 | |
| 1487 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1488 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1489 | # Step 6.2 New flavors are created in the VIM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1490 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1491 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1492 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1493 | VNFCitem["flavor_id"] = flavor_id |
| 1494 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1495 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1496 | logger.debug("Creating new images in the VIM for each VNFC") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1497 | # Step 6.3 New images are created in the VIM |
| 1498 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1499 | #This "for" loop might be integrated with the previous one |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1500 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1501 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1502 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1503 | image_dict={} |
| 1504 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1505 | image_dict['universal_name']=vnfc.get('image name') |
| 1506 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1507 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1508 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1509 | image_dict['checksum']=vnfc.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1510 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1511 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1512 | if image_metadata_dict is not None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1513 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1514 | image_dict['metadata']=image_metadata_str |
| 1515 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1516 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1517 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1518 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1519 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1520 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1521 | if vnfc.get("boot-data"): |
| 1522 | VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1523 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1524 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1525 | # Step 7. Storing the VNF descriptor in the repository |
| 1526 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1527 | vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1528 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1529 | # Step 8. Adding the VNF to the NFVO DB |
| 1530 | vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1531 | return vnf_id |
| 1532 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1533 | _, message = rollback(mydb, vims, rollback_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1534 | if isinstance(e, db_base_Exception): |
| 1535 | error_text = "Exception at database" |
| 1536 | elif isinstance(e, KeyError): |
| 1537 | error_text = "KeyError exception " |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1538 | e.http_code = httperrors.Internal_Server_Error |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1539 | else: |
| 1540 | error_text = "Exception at VIM" |
| 1541 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1542 | #logger.error("start_scenario %s", error_text) |
| 1543 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1544 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1545 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1546 | @deprecated("Use new_vnfd_v3") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1547 | def new_vnf_v02(mydb, tenant_id, vnf_descriptor): |
| 1548 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1549 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1550 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1551 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1552 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1553 | vims = {} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1554 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1555 | check_tenant(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1556 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1557 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| 1558 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1559 | httperrors.Unauthorized) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1560 | else: |
| 1561 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1562 | # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1563 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1564 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1565 | |
| 1566 | # Step 4. Review the descriptor and add missing fields |
| 1567 | #print vnf_descriptor |
| 1568 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| 1569 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1570 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1571 | if "physical" in vnf_descriptor['vnf']: |
| 1572 | del vnf_descriptor['vnf']['physical'] |
| 1573 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1574 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1575 | # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1576 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1577 | logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC']))) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1578 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1579 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1580 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1581 | rollback_list = [] # It will contain the new images created in mano. It is used for rollback |
| 1582 | try: |
| 1583 | logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC") |
| 1584 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1585 | VNFCitem={} |
| 1586 | VNFCitem["name"] = vnfc['name'] |
| 1587 | VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1588 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1589 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1590 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1591 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1592 | myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1593 | myflavorDict["description"] = VNFCitem["description"] |
| 1594 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1595 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1596 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1597 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1598 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1599 | devices = vnfc.get("devices") |
| 1600 | if devices != None: |
| 1601 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1602 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1603 | # TODO: |
| 1604 | # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1605 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1606 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1607 | # Previous code has been commented |
| 1608 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1609 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1610 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1611 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1612 | #else: |
| 1613 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1614 | # if result2: |
| 1615 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1616 | # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1617 | # else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1618 | # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1619 | myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1620 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1621 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1622 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1623 | |
| 1624 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1625 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1626 | # Step 6.2 New flavors are created in the VIM |
| 1627 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| 1628 | |
| 1629 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| 1630 | VNFCitem["flavor_id"] = flavor_id |
| 1631 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1632 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1633 | logger.debug("Creating new images in the VIM for each VNFC") |
| 1634 | # Step 6.3 New images are created in the VIM |
| 1635 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1636 | #This "for" loop might be integrated with the previous one |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1637 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1638 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1639 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1640 | image_dict={} |
| 1641 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1642 | image_dict['universal_name']=vnfc.get('image name') |
| 1643 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1644 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1645 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1646 | image_dict['checksum']=vnfc.get('image checksum') |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1647 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1648 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1649 | if image_metadata_dict is not None: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1650 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1651 | image_dict['metadata']=image_metadata_str |
| 1652 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| 1653 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1654 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| 1655 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1656 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1657 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1658 | if vnfc.get("boot-data"): |
| 1659 | VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1660 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1661 | # Step 7. Storing the VNF descriptor in the repository |
| 1662 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1663 | vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1664 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1665 | # Step 8. Adding the VNF to the NFVO DB |
| 1666 | vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1667 | return vnf_id |
| 1668 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| 1669 | _, message = rollback(mydb, vims, rollback_list) |
| 1670 | if isinstance(e, db_base_Exception): |
| 1671 | error_text = "Exception at database" |
| 1672 | elif isinstance(e, KeyError): |
| 1673 | error_text = "KeyError exception " |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1674 | e.http_code = httperrors.Internal_Server_Error |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1675 | else: |
| 1676 | error_text = "Exception at VIM" |
| 1677 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1678 | #logger.error("start_scenario %s", error_text) |
| 1679 | raise NfvoException(error_text, e.http_code) |
| 1680 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1681 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1682 | def get_vnf_id(mydb, tenant_id, vnf_id): |
| 1683 | #check valid tenant_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1684 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1685 | #obtain data |
| 1686 | where_or = {} |
| 1687 | if tenant_id != "any": |
| 1688 | where_or["tenant_id"] = tenant_id |
| 1689 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1690 | vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") |
| 1691 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1692 | vnf_id = vnf["uuid"] |
| 1693 | filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1694 | filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1695 | #change_keys_http2db(filtered_content, http2db_vnf, reverse=True) |
| 1696 | data={'vnf' : filtered_content} |
| 1697 | #GET VM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1698 | content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id', |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1699 | SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description', |
| 1700 | 'boot_data'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1701 | WHERE={'vnfs.uuid': vnf_id} ) |
| gcalvino | bfa2fd9 | 2018-11-13 18:47:28 +0100 | [diff] [blame] | 1702 | if len(content) != 0: |
| Anderson Bravalheri | c5293de | 2018-11-28 17:21:26 +0000 | [diff] [blame] | 1703 | #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1704 | # change boot_data into boot-data |
| gcalvino | 319b8a5 | 2018-11-05 15:33:23 +0100 | [diff] [blame] | 1705 | for vm in content: |
| 1706 | if vm.get("boot_data"): |
| 1707 | vm["boot-data"] = yaml.safe_load(vm["boot_data"]) |
| 1708 | del vm["boot_data"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1709 | |
| gcalvino | bfa2fd9 | 2018-11-13 18:47:28 +0100 | [diff] [blame] | 1710 | data['vnf']['VNFC'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1711 | #TODO: GET all the information from a VNFC and include it in the output. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1712 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1713 | #GET NET |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1714 | content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1715 | SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'), |
| 1716 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1717 | data['vnf']['nets'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1718 | |
| 1719 | #GET ip-profile for each net |
| 1720 | for net in data['vnf']['nets']: |
| 1721 | ipprofiles = mydb.get_rows(FROM='ip_profiles', |
| 1722 | SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'), |
| 1723 | WHERE={'net_id': net["uuid"]} ) |
| 1724 | if len(ipprofiles)==1: |
| 1725 | net["ip_profile"] = ipprofiles[0] |
| 1726 | elif len(ipprofiles)>1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1727 | raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1728 | |
| 1729 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1730 | #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1731 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1732 | #GET External Interfaces |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1733 | content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1734 | SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \ |
| 1735 | 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\ |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1736 | WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1737 | #print content |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1738 | data['vnf']['external-connections'] = content |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1739 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1740 | return data |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1741 | |
| 1742 | |
| 1743 | def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None): |
| 1744 | # Check tenant exist |
| 1745 | if tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1746 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1747 | # Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1748 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1749 | else: |
| 1750 | vims={} |
| 1751 | |
| 1752 | # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided" |
| 1753 | where_or = {} |
| 1754 | if tenant_id != "any": |
| 1755 | where_or["tenant_id"] = tenant_id |
| 1756 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1757 | vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1758 | vnf_id = vnf["uuid"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1759 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1760 | # "Getting the list of flavors and tenants of the VNF" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1761 | flavorList = get_flavorlist(mydb, vnf_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1762 | if len(flavorList)==0: |
| 1763 | logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1764 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1765 | imageList = get_imagelist(mydb, vnf_id) |
| 1766 | if len(imageList)==0: |
| 1767 | logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1768 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1769 | deleted = mydb.delete_row_by_id('vnfs', vnf_id) |
| 1770 | if deleted == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1771 | raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1772 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1773 | undeletedItems = [] |
| 1774 | for flavor in flavorList: |
| 1775 | #check if flavor is used by other vnf |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1776 | try: |
| 1777 | c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} ) |
| 1778 | if len(c) > 0: |
| 1779 | logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor) |
| 1780 | continue |
| 1781 | #flavor not used, must be deleted |
| 1782 | #delelte at VIM |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1783 | c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1784 | for flavor_vim in c: |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1785 | if not flavor_vim['created']: # skip this flavor because not created by openmano |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1786 | continue |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1787 | # look for vim |
| 1788 | myvim = None |
| 1789 | for vim in vims.values(): |
| 1790 | if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]: |
| 1791 | myvim = vim |
| 1792 | break |
| 1793 | if not myvim: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1794 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1795 | try: |
| 1796 | myvim.delete_flavor(flavor_vim["vim_id"]) |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1797 | except vimconn.vimconnNotFoundException: |
| 1798 | logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], |
| 1799 | flavor_vim["datacenter_vim_id"] ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1800 | except vimconn.vimconnException as e: |
| 1801 | logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s", |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1802 | flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e)) |
| 1803 | undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], |
| 1804 | flavor_vim["datacenter_vim_id"])) |
| 1805 | # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1806 | mydb.delete_row_by_id('flavors', flavor) |
| 1807 | except db_base_Exception as e: |
| 1808 | logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e)) |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1809 | undeletedItems.append("flavor {}".format(flavor)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1810 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1811 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1812 | for image in imageList: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1813 | try: |
| 1814 | #check if image is used by other vnf |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 1815 | c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}]) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1816 | if len(c) > 0: |
| 1817 | logger.debug("Image '%s' not deleted because it is being used by another VNF", image) |
| 1818 | continue |
| 1819 | #image not used, must be deleted |
| 1820 | #delelte at VIM |
| 1821 | c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1822 | for image_vim in c: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 1823 | if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1824 | continue |
| 1825 | if image_vim['created']=='false': #skip this image because not created by openmano |
| 1826 | continue |
| 1827 | myvim=vims[ image_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1828 | try: |
| 1829 | myvim.delete_image(image_vim["vim_id"]) |
| 1830 | except vimconn.vimconnNotFoundException as e: |
| 1831 | logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] ) |
| 1832 | except vimconn.vimconnException as e: |
| 1833 | logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s", |
| 1834 | image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1835 | undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] )) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1836 | #delete image from Database, using table images and with cascade foreign key also at datacenters_images |
| 1837 | mydb.delete_row_by_id('images', image) |
| 1838 | except db_base_Exception as e: |
| 1839 | logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1840 | undeletedItems.append("image %s" % image) |
| 1841 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1842 | return vnf_id + " " + vnf["name"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1843 | #if undeletedItems: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1844 | # return "delete_vnf. Undeleted: %s" %(undeletedItems) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1845 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1846 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1847 | @deprecated("Not used") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1848 | def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None): |
| 1849 | result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name) |
| 1850 | if result < 0: |
| 1851 | return result, vims |
| 1852 | elif result == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1853 | return -httperrors.Not_Found, "datacenter '%s' not found" % datacenter_name |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1854 | myvim = vims.values()[0] |
| 1855 | result,servers = myvim.get_hosts_info() |
| 1856 | if result < 0: |
| 1857 | return result, servers |
| 1858 | topology = {'name':myvim['name'] , 'servers': servers} |
| 1859 | return result, topology |
| 1860 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1861 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1862 | def get_hosts(mydb, nfvo_tenant_id): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1863 | vims = get_vim(mydb, nfvo_tenant_id) |
| 1864 | if len(vims) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1865 | raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), httperrors.Not_Found) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1866 | elif len(vims)>1: |
| 1867 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1868 | raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1869 | myvim = vims.values()[0] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1870 | try: |
| 1871 | hosts = myvim.get_hosts() |
| 1872 | logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1873 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1874 | datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] } |
| 1875 | for host in hosts: |
| 1876 | server={'name':host['name'], 'vms':[]} |
| 1877 | for vm in host['instances']: |
| 1878 | #get internal name and model |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1879 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1880 | c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\ |
| 1881 | WHERE={'vim_vm_id':vm['id']} ) |
| 1882 | if len(c) == 0: |
| 1883 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id'])) |
| 1884 | continue |
| 1885 | server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1886 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1887 | except db_base_Exception as e: |
| 1888 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e))) |
| 1889 | datacenter['Datacenters'][0]['servers'].append(server) |
| 1890 | #return -400, "en construccion" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1891 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1892 | #print 'datacenters '+ json.dumps(datacenter, indent=4) |
| 1893 | return datacenter |
| 1894 | except vimconn.vimconnException as e: |
| 1895 | raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1896 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1897 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1898 | @deprecated("Use new_nsd_v3") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1899 | def new_scenario(mydb, tenant_id, topo): |
| 1900 | |
| 1901 | # result, vims = get_vim(mydb, tenant_id) |
| 1902 | # if result < 0: |
| 1903 | # return result, vims |
| 1904 | #1: parse input |
| 1905 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1906 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1907 | if "tenant_id" in topo: |
| 1908 | if topo["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1909 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1910 | httperrors.Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1911 | else: |
| 1912 | tenant_id=None |
| 1913 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1914 | #1.1: get VNFs and external_networks (other_nets). |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1915 | vnfs={} |
| 1916 | other_nets={} #external_networks, bridge_networks and data_networkds |
| 1917 | nodes = topo['topology']['nodes'] |
| 1918 | for k in nodes.keys(): |
| 1919 | if nodes[k]['type'] == 'VNF': |
| 1920 | vnfs[k] = nodes[k] |
| 1921 | vnfs[k]['ifaces'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1922 | elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1923 | other_nets[k] = nodes[k] |
| 1924 | other_nets[k]['external']=True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1925 | elif nodes[k]['type'] == 'network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1926 | other_nets[k] = nodes[k] |
| 1927 | other_nets[k]['external']=False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1928 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1929 | |
| 1930 | #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces |
| 1931 | for name,vnf in vnfs.items(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1932 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1933 | error_text = "" |
| 1934 | error_pos = "'topology':'nodes':'" + name + "'" |
| 1935 | if 'vnf_id' in vnf: |
| 1936 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1937 | where['uuid'] = vnf['vnf_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1938 | if 'VNF model' in vnf: |
| 1939 | error_text += " 'VNF model' " + vnf['VNF model'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1940 | where['name'] = vnf['VNF model'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1941 | if len(where) == 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1942 | raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1943 | |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1944 | vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), |
| 1945 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1946 | WHERE=where) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1947 | if len(vnf_db)==0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1948 | raise NfvoException("unknown" + error_text + " at " + error_pos, httperrors.Not_Found) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1949 | elif len(vnf_db)>1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1950 | raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1951 | vnf['uuid']=vnf_db[0]['uuid'] |
| 1952 | vnf['description']=vnf_db[0]['description'] |
| 1953 | #get external interfaces |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1954 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), |
| 1955 | FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1956 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1957 | for ext_iface in ext_ifaces: |
| 1958 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']} |
| 1959 | |
| 1960 | #1.4 get list of connections |
| 1961 | conections = topo['topology']['connections'] |
| 1962 | conections_list = [] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1963 | conections_list_name = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1964 | for k in conections.keys(): |
| 1965 | if type(conections[k]['nodes'])==dict: #dict with node:iface pairs |
| 1966 | ifaces_list = conections[k]['nodes'].items() |
| 1967 | elif type(conections[k]['nodes'])==list: #list with dictionary |
| 1968 | ifaces_list=[] |
| 1969 | conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] ) |
| 1970 | for k2 in conection_pair_list: |
| 1971 | ifaces_list += k2 |
| 1972 | |
| 1973 | con_type = conections[k].get("type", "link") |
| 1974 | if con_type != "link": |
| 1975 | if k in other_nets: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1976 | raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1977 | other_nets[k] = {'external': False} |
| 1978 | if conections[k].get("graph"): |
| 1979 | other_nets[k]["graph"] = conections[k]["graph"] |
| 1980 | ifaces_list.append( (k, None) ) |
| 1981 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1982 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1983 | if con_type == "external_network": |
| 1984 | other_nets[k]['external'] = True |
| 1985 | if conections[k].get("model"): |
| 1986 | other_nets[k]["model"] = conections[k]["model"] |
| 1987 | else: |
| 1988 | other_nets[k]["model"] = k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1989 | if con_type == "dataplane_net" or con_type == "bridge_net": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1990 | other_nets[k]["model"] = con_type |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1991 | |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1992 | conections_list_name.append(k) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1993 | conections_list.append(set(ifaces_list)) #from list to set to operate as a set (this conversion removes elements that are repeated in a list) |
| 1994 | #print set(ifaces_list) |
| 1995 | #check valid VNF and iface names |
| 1996 | for iface in ifaces_list: |
| 1997 | if iface[0] not in vnfs and iface[0] not in other_nets : |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1998 | raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1999 | str(k), iface[0]), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2000 | if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2001 | raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2002 | str(k), iface[0], iface[1]), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2003 | |
| 2004 | #1.5 unify connections from the pair list to a consolidated list |
| 2005 | index=0 |
| 2006 | while index < len(conections_list): |
| 2007 | index2 = index+1 |
| 2008 | while index2 < len(conections_list): |
| 2009 | if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets |
| 2010 | conections_list[index] |= conections_list[index2] |
| 2011 | del conections_list[index2] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 2012 | del conections_list_name[index2] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2013 | else: |
| 2014 | index2 += 1 |
| 2015 | conections_list[index] = list(conections_list[index]) # from set to list again |
| 2016 | index += 1 |
| 2017 | #for k in conections_list: |
| 2018 | # print k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2019 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2020 | |
| 2021 | |
| 2022 | #1.6 Delete non external nets |
| 2023 | # for k in other_nets.keys(): |
| 2024 | # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net': |
| 2025 | # for con in conections_list: |
| 2026 | # delete_indexes=[] |
| 2027 | # for index in range(0,len(con)): |
| 2028 | # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower |
| 2029 | # for index in delete_indexes: |
| 2030 | # del con[index] |
| 2031 | # del other_nets[k] |
| 2032 | #1.7: Check external_ports are present at database table datacenter_nets |
| 2033 | for k,net in other_nets.items(): |
| 2034 | error_pos = "'topology':'nodes':'" + k + "'" |
| 2035 | if net['external']==False: |
| 2036 | if 'name' not in net: |
| 2037 | net['name']=k |
| 2038 | if 'model' not in net: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2039 | raise NfvoException("needed a 'model' at " + error_pos, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2040 | if net['model']=='bridge_net': |
| 2041 | net['type']='bridge'; |
| 2042 | elif net['model']=='dataplane_net': |
| 2043 | net['type']='data'; |
| 2044 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2045 | raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2046 | else: #external |
| 2047 | #IF we do not want to check that external network exist at datacenter |
| 2048 | pass |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2049 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2050 | # error_text = "" |
| 2051 | # WHERE_={} |
| 2052 | # if 'net_id' in net: |
| 2053 | # error_text += " 'net_id' " + net['net_id'] |
| 2054 | # WHERE_['uuid'] = net['net_id'] |
| 2055 | # if 'model' in net: |
| 2056 | # error_text += " 'model' " + net['model'] |
| 2057 | # WHERE_['name'] = net['model'] |
| 2058 | # if len(WHERE_) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2059 | # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2060 | # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'), |
| 2061 | # FROM='datacenter_nets', WHERE=WHERE_ ) |
| 2062 | # if r<0: |
| 2063 | # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db |
| 2064 | # elif r==0: |
| 2065 | # print "nfvo.new_scenario Error" +error_text+ " is not present at database" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2066 | # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2067 | # elif r>1: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2068 | # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2069 | # return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2070 | # other_nets[k].update(net_db[0]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2071 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2072 | net_list={} |
| 2073 | net_nb=0 #Number of nets |
| 2074 | for con in conections_list: |
| 2075 | #check if this is connected to a external net |
| 2076 | other_net_index=-1 |
| 2077 | #print |
| 2078 | #print "con", con |
| 2079 | for index in range(0,len(con)): |
| 2080 | #check if this is connected to a external net |
| 2081 | for net_key in other_nets.keys(): |
| 2082 | if con[index][0]==net_key: |
| 2083 | if other_net_index>=0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2084 | error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2085 | #print "nfvo.new_scenario " + error_text |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2086 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2087 | else: |
| 2088 | other_net_index = index |
| 2089 | net_target = net_key |
| 2090 | break |
| 2091 | #print "other_net_index", other_net_index |
| 2092 | try: |
| 2093 | if other_net_index>=0: |
| 2094 | del con[other_net_index] |
| 2095 | #IF we do not want to check that external network exist at datacenter |
| 2096 | if other_nets[net_target]['external'] : |
| 2097 | if "name" not in other_nets[net_target]: |
| 2098 | other_nets[net_target]['name'] = other_nets[net_target]['model'] |
| 2099 | if other_nets[net_target]["type"] == "external_network": |
| 2100 | if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data": |
| 2101 | other_nets[net_target]["type"] = "data" |
| 2102 | else: |
| 2103 | other_nets[net_target]["type"] = "bridge" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2104 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2105 | # if other_nets[net_target]['external'] : |
| 2106 | # type_='data' if len(con)>1 else 'ptp' #an external net is connected to a external port, so it is ptp if only one connection is done to this net |
| 2107 | # if type_=='data' and other_nets[net_target]['type']=="ptp": |
| 2108 | # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target) |
| 2109 | # print "nfvo.new_scenario " + error_text |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2110 | # return -httperrors.Bad_Request, error_text |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2111 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2112 | for iface in con: |
| 2113 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2114 | else: |
| 2115 | #create a net |
| 2116 | net_type_bridge=False |
| 2117 | net_type_data=False |
| 2118 | net_target = "__-__net"+str(net_nb) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2119 | net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb), |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 2120 | 'description':"net-%s in scenario %s" %(net_nb,topo['name']), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2121 | 'external':False} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2122 | for iface in con: |
| 2123 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2124 | iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type'] |
| 2125 | if iface_type=='mgmt' or iface_type=='bridge': |
| 2126 | net_type_bridge = True |
| 2127 | else: |
| 2128 | net_type_data = True |
| 2129 | if net_type_bridge and net_type_data: |
| 2130 | error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1]) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2131 | #print "nfvo.new_scenario " + error_text |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2132 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2133 | elif net_type_bridge: |
| 2134 | type_='bridge' |
| 2135 | else: |
| 2136 | type_='data' if len(con)>2 else 'ptp' |
| 2137 | net_list[net_target]['type'] = type_ |
| 2138 | net_nb+=1 |
| 2139 | except Exception: |
| 2140 | error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1]) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2141 | #print "nfvo.new_scenario " + error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2142 | #raise e |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2143 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2144 | |
| 2145 | #1.8: Connect to management net all not already connected interfaces of type 'mgmt' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2146 | #1.8.1 obtain management net |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2147 | mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2148 | FROM='datacenter_nets', WHERE={'name':'mgmt'} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2149 | #1.8.2 check all interfaces from all vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2150 | if len(mgmt_net)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2151 | add_mgmt_net = False |
| 2152 | for vnf in vnfs.values(): |
| 2153 | for iface in vnf['ifaces'].values(): |
| 2154 | if iface['type']=='mgmt' and 'net_key' not in iface: |
| 2155 | #iface not connected |
| 2156 | iface['net_key'] = 'mgmt' |
| 2157 | add_mgmt_net = True |
| 2158 | if add_mgmt_net and 'mgmt' not in net_list: |
| 2159 | net_list['mgmt']=mgmt_net[0] |
| 2160 | net_list['mgmt']['external']=True |
| 2161 | net_list['mgmt']['graph']={'visible':False} |
| 2162 | |
| 2163 | net_list.update(other_nets) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2164 | #print |
| 2165 | #print 'net_list', net_list |
| 2166 | #print |
| 2167 | #print 'vnfs', vnfs |
| 2168 | #print |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2169 | |
| 2170 | #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2171 | c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list, |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2172 | 'tenant_id':tenant_id, 'name':topo['name'], |
| 2173 | 'description':topo.get('description',topo['name']), |
| 2174 | 'public': topo.get('public', False) |
| 2175 | }) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2176 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2177 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2178 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2179 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 2180 | @deprecated("Use new_nsd_v3") |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2181 | def new_scenario_v02(mydb, tenant_id, scenario_dict, version): |
| 2182 | """ This creates a new scenario for version 0.2 and 0.3""" |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2183 | scenario = scenario_dict["scenario"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2184 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2185 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2186 | if "tenant_id" in scenario: |
| 2187 | if scenario["tenant_id"] != tenant_id: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2188 | # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2189 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2190 | scenario["tenant_id"], tenant_id), httperrors.Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2191 | else: |
| 2192 | tenant_id=None |
| 2193 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2194 | # 1: Check that VNF are present at database table vnfs and update content into scenario dict |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2195 | for name,vnf in scenario["vnfs"].iteritems(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2196 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2197 | error_text = "" |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 2198 | error_pos = "'scenario':'vnfs':'" + name + "'" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2199 | if 'vnf_id' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2200 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2201 | where['uuid'] = vnf['vnf_id'] |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2202 | if 'vnf_name' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2203 | error_text += " 'vnf_name' " + vnf['vnf_name'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2204 | where['name'] = vnf['vnf_name'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2205 | if len(where) == 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2206 | raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, httperrors.Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2207 | vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'), |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2208 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2209 | WHERE=where) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2210 | if len(vnf_db) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2211 | raise NfvoException("Unknown" + error_text + " at " + error_pos, httperrors.Not_Found) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2212 | elif len(vnf_db) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2213 | raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2214 | vnf['uuid'] = vnf_db[0]['uuid'] |
| 2215 | vnf['description'] = vnf_db[0]['description'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2216 | vnf['ifaces'] = {} |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2217 | # get external interfaces |
| 2218 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'), |
| 2219 | FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2220 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2221 | for ext_iface in ext_ifaces: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2222 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']} |
| 2223 | # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2224 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2225 | # 2: Insert net_key and ip_address at every vnf interface |
| 2226 | for net_name, net in scenario["networks"].items(): |
| 2227 | net_type_bridge = False |
| 2228 | net_type_data = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2229 | for iface_dict in net["interfaces"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2230 | if version == "0.2": |
| 2231 | temp_dict = iface_dict |
| 2232 | ip_address = None |
| 2233 | elif version == "0.3": |
| 2234 | temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]} |
| 2235 | ip_address = iface_dict.get('ip_address', None) |
| 2236 | for vnf, iface in temp_dict.items(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2237 | if vnf not in scenario["vnfs"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2238 | error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format( |
| 2239 | net_name, vnf) |
| 2240 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2241 | raise NfvoException(error_text, httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2242 | if iface not in scenario["vnfs"][vnf]['ifaces']: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2243 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\ |
| 2244 | .format(net_name, iface) |
| 2245 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2246 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2247 | if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2248 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\ |
| 2249 | "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key']) |
| 2250 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2251 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2252 | scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2253 | scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2254 | iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type'] |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2255 | if iface_type == 'mgmt' or iface_type == 'bridge': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2256 | net_type_bridge = True |
| 2257 | else: |
| 2258 | net_type_data = True |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2259 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2260 | if net_type_bridge and net_type_data: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2261 | error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\ |
| 2262 | .format(net_name) |
| 2263 | # logger.debug("nfvo.new_scenario " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2264 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2265 | elif net_type_bridge: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2266 | type_ = 'bridge' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2267 | else: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2268 | type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp' |
| 2269 | |
| 2270 | if net.get("implementation"): # for v0.3 |
| 2271 | if type_ == "bridge" and net["implementation"] == "underlay": |
| 2272 | error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\ |
| 2273 | "'network':'{}'".format(net_name) |
| 2274 | # logger.debug(error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2275 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2276 | elif type_ != "bridge" and net["implementation"] == "overlay": |
| 2277 | error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\ |
| 2278 | "'network':'{}'".format(net_name) |
| 2279 | # logger.debug(error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2280 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2281 | net.pop("implementation") |
| 2282 | if "type" in net and version == "0.3": # for v0.3 |
| 2283 | if type_ == "data" and net["type"] == "e-line": |
| 2284 | error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\ |
| 2285 | "'e-line' at 'network':'{}'".format(net_name) |
| 2286 | # logger.debug(error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2287 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2288 | elif type_ == "ptp" and net["type"] == "e-lan": |
| 2289 | type_ = "data" |
| 2290 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2291 | net['type'] = type_ |
| 2292 | net['name'] = net_name |
| 2293 | net['external'] = net.get('external', False) |
| 2294 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2295 | # 3: insert at database |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2296 | scenario["nets"] = scenario["networks"] |
| 2297 | scenario['tenant_id'] = tenant_id |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2298 | scenario_id = mydb.new_scenario(scenario) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2299 | return scenario_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2300 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2301 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2302 | def new_nsd_v3(mydb, tenant_id, nsd_descriptor): |
| 2303 | """ |
| 2304 | Parses an OSM IM nsd_catalog and insert at DB |
| 2305 | :param mydb: |
| 2306 | :param tenant_id: |
| 2307 | :param nsd_descriptor: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2308 | :return: The list of created NSD ids |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2309 | """ |
| 2310 | try: |
| 2311 | mynsd = nsd_catalog.nsd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 2312 | try: |
| tierno | f6bbe22 | 2019-04-09 14:19:40 +0000 | [diff] [blame] | 2313 | pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd, skip_unknown=True) |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 2314 | except Exception as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2315 | raise NfvoException("Error. Invalid NS descriptor format: " + str(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2316 | db_scenarios = [] |
| 2317 | db_sce_nets = [] |
| 2318 | db_sce_vnfs = [] |
| 2319 | db_sce_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2320 | db_sce_vnffgs = [] |
| 2321 | db_sce_rsps = [] |
| 2322 | db_sce_rsp_hops = [] |
| 2323 | db_sce_classifiers = [] |
| 2324 | db_sce_classifier_matches = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2325 | db_ip_profiles = [] |
| 2326 | db_ip_profiles_index = 0 |
| 2327 | uuid_list = [] |
| 2328 | nsd_uuid_list = [] |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2329 | for nsd_yang in mynsd.nsd_catalog.nsd.itervalues(): |
| 2330 | nsd = nsd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2331 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2332 | # table scenarios |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2333 | scenario_uuid = str(uuid4()) |
| 2334 | uuid_list.append(scenario_uuid) |
| 2335 | nsd_uuid_list.append(scenario_uuid) |
| 2336 | db_scenario = { |
| 2337 | "uuid": scenario_uuid, |
| 2338 | "osm_id": get_str(nsd, "id", 255), |
| 2339 | "name": get_str(nsd, "name", 255), |
| 2340 | "description": get_str(nsd, "description", 255), |
| 2341 | "tenant_id": tenant_id, |
| 2342 | "vendor": get_str(nsd, "vendor", 255), |
| 2343 | "short_name": get_str(nsd, "short-name", 255), |
| 2344 | "descriptor": str(nsd_descriptor)[:60000], |
| 2345 | } |
| 2346 | db_scenarios.append(db_scenario) |
| 2347 | |
| 2348 | # table sce_vnfs (constituent-vnfd) |
| 2349 | vnf_index2scevnf_uuid = {} |
| 2350 | vnf_index2vnf_uuid = {} |
| 2351 | for vnf in nsd.get("constituent-vnfd").itervalues(): |
| 2352 | existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255], |
| 2353 | 'tenant_id': tenant_id}) |
| 2354 | if not existing_vnf: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2355 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':" |
| 2356 | "'{}'. Reference to a non-existing VNFD in the catalog".format( |
| 2357 | str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2358 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2359 | sce_vnf_uuid = str(uuid4()) |
| 2360 | uuid_list.append(sce_vnf_uuid) |
| 2361 | db_sce_vnf = { |
| 2362 | "uuid": sce_vnf_uuid, |
| 2363 | "scenario_id": scenario_uuid, |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 2364 | # "name": get_str(vnf, "member-vnf-index", 255), |
| 2365 | "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2366 | "vnf_id": existing_vnf[0]["uuid"], |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2367 | "member_vnf_index": str(vnf["member-vnf-index"]), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2368 | # TODO 'start-by-default': True |
| 2369 | } |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2370 | vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid |
| 2371 | vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2372 | db_sce_vnfs.append(db_sce_vnf) |
| 2373 | |
| 2374 | # table ip_profiles (ip-profiles) |
| 2375 | ip_profile_name2db_table_index = {} |
| 2376 | for ip_profile in nsd.get("ip-profiles").itervalues(): |
| 2377 | db_ip_profile = { |
| 2378 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 2379 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 2380 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 2381 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 2382 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 2383 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 2384 | } |
| 2385 | dns_list = [] |
| 2386 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 2387 | dns_list.append(str(dns.get("address"))) |
| 2388 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 2389 | if ip_profile["ip-profile-params"].get('security-group'): |
| 2390 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 2391 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 2392 | db_ip_profiles_index += 1 |
| 2393 | db_ip_profiles.append(db_ip_profile) |
| 2394 | |
| 2395 | # table sce_nets (internal-vld) |
| 2396 | for vld in nsd.get("vld").itervalues(): |
| 2397 | sce_net_uuid = str(uuid4()) |
| 2398 | uuid_list.append(sce_net_uuid) |
| 2399 | db_sce_net = { |
| 2400 | "uuid": sce_net_uuid, |
| 2401 | "name": get_str(vld, "name", 255), |
| 2402 | "scenario_id": scenario_uuid, |
| 2403 | # "type": #TODO |
| 2404 | "multipoint": not vld.get("type") == "ELINE", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 2405 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2406 | # "external": #TODO |
| 2407 | "description": get_str(vld, "description", 255), |
| 2408 | } |
| 2409 | # guess type of network |
| 2410 | if vld.get("mgmt-network"): |
| 2411 | db_sce_net["type"] = "bridge" |
| 2412 | db_sce_net["external"] = True |
| 2413 | elif vld.get("provider-network").get("overlay-type") == "VLAN": |
| 2414 | db_sce_net["type"] = "data" |
| 2415 | else: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2416 | # later on it will be fixed to bridge or data depending on the type of interfaces attached to it |
| 2417 | db_sce_net["type"] = None |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2418 | db_sce_nets.append(db_sce_net) |
| 2419 | |
| 2420 | # ip-profile, link db_ip_profile with db_sce_net |
| 2421 | if vld.get("ip-profile-ref"): |
| 2422 | ip_profile_name = vld.get("ip-profile-ref") |
| 2423 | if ip_profile_name not in ip_profile_name2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2424 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'." |
| 2425 | " Reference to a non-existing 'ip_profiles'".format( |
| 2426 | str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2427 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2428 | db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 2429 | elif vld.get("vim-network-name"): |
| 2430 | db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2431 | |
| 2432 | # table sce_interfaces (vld:vnfd-connection-point-ref) |
| 2433 | for iface in vld.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2434 | vnf_index = str(iface['member-vnf-index-ref']) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2435 | # check correct parameters |
| 2436 | if vnf_index not in vnf_index2vnf_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2437 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2438 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2439 | "'nsd':'constituent-vnfd'".format( |
| 2440 | str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2441 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2442 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2443 | existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2444 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2445 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2446 | 'external_name': get_str(iface, "vnfd-connection-point-ref", |
| 2447 | 255)}) |
| 2448 | if not existing_ifaces: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2449 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2450 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2451 | "connection-point name at VNFD '{}'".format( |
| 2452 | str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2453 | str(iface.get("vnfd-id-ref"))[:255]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2454 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2455 | interface_uuid = existing_ifaces[0]["uuid"] |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2456 | if existing_ifaces[0]["iface_type"] == "data": |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2457 | db_sce_net["type"] = "data" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2458 | sce_interface_uuid = str(uuid4()) |
| 2459 | uuid_list.append(sce_net_uuid) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2460 | iface_ip_address = None |
| 2461 | if iface.get("ip-address"): |
| 2462 | iface_ip_address = str(iface.get("ip-address")) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2463 | db_sce_interface = { |
| 2464 | "uuid": sce_interface_uuid, |
| 2465 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2466 | "sce_net_id": sce_net_uuid, |
| 2467 | "interface_id": interface_uuid, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2468 | "ip_address": iface_ip_address, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2469 | } |
| 2470 | db_sce_interfaces.append(db_sce_interface) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2471 | if not db_sce_net["type"]: |
| 2472 | db_sce_net["type"] = "bridge" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2473 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2474 | # table sce_vnffgs (vnffgd) |
| 2475 | for vnffg in nsd.get("vnffgd").itervalues(): |
| 2476 | sce_vnffg_uuid = str(uuid4()) |
| 2477 | uuid_list.append(sce_vnffg_uuid) |
| 2478 | db_sce_vnffg = { |
| 2479 | "uuid": sce_vnffg_uuid, |
| 2480 | "name": get_str(vnffg, "name", 255), |
| 2481 | "scenario_id": scenario_uuid, |
| 2482 | "vendor": get_str(vnffg, "vendor", 255), |
| 2483 | "description": get_str(vld, "description", 255), |
| 2484 | } |
| 2485 | db_sce_vnffgs.append(db_sce_vnffg) |
| 2486 | |
| 2487 | # deal with rsps |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2488 | for rsp in vnffg.get("rsp").itervalues(): |
| 2489 | sce_rsp_uuid = str(uuid4()) |
| 2490 | uuid_list.append(sce_rsp_uuid) |
| 2491 | db_sce_rsp = { |
| 2492 | "uuid": sce_rsp_uuid, |
| 2493 | "name": get_str(rsp, "name", 255), |
| 2494 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2495 | "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code |
| 2496 | } |
| 2497 | db_sce_rsps.append(db_sce_rsp) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2498 | for iface in rsp.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2499 | vnf_index = str(iface['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2500 | if_order = int(iface['order']) |
| 2501 | # check correct parameters |
| 2502 | if vnf_index not in vnf_index2vnf_uuid: |
| 2503 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2504 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2505 | "'nsd':'constituent-vnfd'".format( |
| 2506 | str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2507 | httperrors.Bad_Request) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2508 | |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2509 | ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2510 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2511 | WHERE={ |
| 2512 | 'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2513 | 'external_name': get_str(iface, "vnfd-ingress-connection-point-ref", |
| 2514 | 255)}) |
| 2515 | if not ingress_existing_ifaces: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2516 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2517 | "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing " |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2518 | "connection-point name at VNFD '{}'".format( |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2519 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]), |
| 2520 | str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request) |
| 2521 | |
| 2522 | egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2523 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2524 | WHERE={ |
| 2525 | 'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2526 | 'external_name': get_str(iface, "vnfd-egress-connection-point-ref", |
| 2527 | 255)}) |
| 2528 | if not egress_existing_ifaces: |
| 2529 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2530 | "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing " |
| 2531 | "connection-point name at VNFD '{}'".format( |
| 2532 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]), |
| 2533 | str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request) |
| 2534 | |
| 2535 | ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"] |
| 2536 | egress_interface_uuid = egress_existing_ifaces[0]["uuid"] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2537 | sce_rsp_hop_uuid = str(uuid4()) |
| 2538 | uuid_list.append(sce_rsp_hop_uuid) |
| 2539 | db_sce_rsp_hop = { |
| 2540 | "uuid": sce_rsp_hop_uuid, |
| 2541 | "if_order": if_order, |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2542 | "ingress_interface_id": ingress_interface_uuid, |
| 2543 | "egress_interface_id": egress_interface_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2544 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2545 | "sce_rsp_id": sce_rsp_uuid, |
| 2546 | } |
| 2547 | db_sce_rsp_hops.append(db_sce_rsp_hop) |
| 2548 | |
| 2549 | # deal with classifiers |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2550 | for classifier in vnffg.get("classifier").itervalues(): |
| 2551 | sce_classifier_uuid = str(uuid4()) |
| 2552 | uuid_list.append(sce_classifier_uuid) |
| 2553 | |
| 2554 | # source VNF |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2555 | vnf_index = str(classifier['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2556 | if vnf_index not in vnf_index2vnf_uuid: |
| 2557 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point" |
| 2558 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2559 | "'nsd':'constituent-vnfd'".format( |
| 2560 | str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2561 | httperrors.Bad_Request) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2562 | existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2563 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2564 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2565 | 'external_name': get_str(classifier, "vnfd-connection-point-ref", |
| 2566 | 255)}) |
| 2567 | if not existing_ifaces: |
| 2568 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2569 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2570 | "connection-point name at VNFD '{}'".format( |
| 2571 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2572 | str(iface.get("vnfd-id-ref"))[:255]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2573 | httperrors.Bad_Request) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2574 | interface_uuid = existing_ifaces[0]["uuid"] |
| 2575 | |
| 2576 | db_sce_classifier = { |
| 2577 | "uuid": sce_classifier_uuid, |
| 2578 | "name": get_str(classifier, "name", 255), |
| 2579 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2580 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2581 | "interface_id": interface_uuid, |
| 2582 | } |
| 2583 | rsp_id = get_str(classifier, "rsp-id-ref", 255) |
| 2584 | rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None) |
| 2585 | db_sce_classifier["sce_rsp_id"] = rsp["uuid"] |
| 2586 | db_sce_classifiers.append(db_sce_classifier) |
| 2587 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2588 | for match in classifier.get("match-attributes").itervalues(): |
| 2589 | sce_classifier_match_uuid = str(uuid4()) |
| 2590 | uuid_list.append(sce_classifier_match_uuid) |
| 2591 | db_sce_classifier_match = { |
| 2592 | "uuid": sce_classifier_match_uuid, |
| 2593 | "ip_proto": get_str(match, "ip-proto", 2), |
| 2594 | "source_ip": get_str(match, "source-ip-address", 16), |
| 2595 | "destination_ip": get_str(match, "destination-ip-address", 16), |
| 2596 | "source_port": get_str(match, "source-port", 5), |
| 2597 | "destination_port": get_str(match, "destination-port", 5), |
| 2598 | "sce_classifier_id": sce_classifier_uuid, |
| 2599 | } |
| 2600 | db_sce_classifier_matches.append(db_sce_classifier_match) |
| 2601 | # TODO: vnf/cp keys |
| 2602 | |
| 2603 | # remove unneeded id's in sce_rsps |
| 2604 | for rsp in db_sce_rsps: |
| 2605 | rsp.pop('id') |
| 2606 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2607 | db_tables = [ |
| 2608 | {"scenarios": db_scenarios}, |
| 2609 | {"sce_nets": db_sce_nets}, |
| 2610 | {"ip_profiles": db_ip_profiles}, |
| 2611 | {"sce_vnfs": db_sce_vnfs}, |
| 2612 | {"sce_interfaces": db_sce_interfaces}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2613 | {"sce_vnffgs": db_sce_vnffgs}, |
| 2614 | {"sce_rsps": db_sce_rsps}, |
| 2615 | {"sce_rsp_hops": db_sce_rsp_hops}, |
| 2616 | {"sce_classifiers": db_sce_classifiers}, |
| 2617 | {"sce_classifier_matches": db_sce_classifier_matches}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2618 | ] |
| 2619 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2620 | logger.debug("new_nsd_v3 done: %s", |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2621 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 2622 | mydb.new_rows(db_tables, uuid_list) |
| 2623 | return nsd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2624 | except NfvoException: |
| 2625 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2626 | except Exception as e: |
| 2627 | logger.error("Exception {}".format(e)) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2628 | raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2629 | |
| 2630 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2631 | def edit_scenario(mydb, tenant_id, scenario_id, data): |
| 2632 | data["uuid"] = scenario_id |
| 2633 | data["tenant_id"] = tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2634 | c = mydb.edit_scenario( data ) |
| 2635 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2636 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2637 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 2638 | @deprecated("Use create_instance") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2639 | def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2640 | #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2641 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant) |
| 2642 | vims = {datacenter_id: myvim} |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2643 | myvim_tenant = myvim['tenant_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2644 | datacenter_name = myvim['name'] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2645 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2646 | rollbackList=[] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2647 | try: |
| 2648 | #print "Checking that the scenario_id exists and getting the scenario dictionary" |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2649 | scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2650 | scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] } |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2651 | scenarioDict['datacenter_id'] = datacenter_id |
| 2652 | #print '================scenarioDict=======================' |
| 2653 | #print json.dumps(scenarioDict, indent=4) |
| 2654 | #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2655 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2656 | logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs'])) |
| 2657 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2658 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2659 | auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id |
| 2660 | auxNetDict['scenario'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2661 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2662 | logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM") |
| 2663 | for sce_net in scenarioDict['nets']: |
| 2664 | #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2665 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2666 | myNetName = "%s.%s" % (instance_scenario_name, sce_net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2667 | myNetName = myNetName[0:255] #limit length |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2668 | myNetType = sce_net['type'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2669 | myNetDict = {} |
| 2670 | myNetDict["name"] = myNetName |
| 2671 | myNetDict["type"] = myNetType |
| 2672 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2673 | myNetIPProfile = sce_net.get('ip_profile', None) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2674 | #TODO: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2675 | #We should use the dictionary as input parameter for new_network |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2676 | #print myNetDict |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2677 | if not sce_net["external"]: |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2678 | network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2679 | #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id) |
| 2680 | sce_net['vim_id'] = network_id |
| 2681 | auxNetDict['scenario'][sce_net['uuid']] = network_id |
| 2682 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2683 | sce_net["created"] = True |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2684 | else: |
| 2685 | if sce_net['vim_id'] == None: |
| 2686 | error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name']) |
| 2687 | _, message = rollback(mydb, vims, rollbackList) |
| 2688 | logger.error("nfvo.start_scenario: %s", error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2689 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2690 | logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id']) |
| 2691 | auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2692 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2693 | logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM") |
| 2694 | #For each vnf net, we create it and we add it to instanceNetlist. |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2695 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2696 | for sce_vnf in scenarioDict['vnfs']: |
| 2697 | for net in sce_vnf['nets']: |
| 2698 | #print "Net name: %s. Description: %s" % (net["name"], net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2699 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2700 | myNetName = "%s.%s" % (instance_scenario_name,net['name']) |
| 2701 | myNetName = myNetName[0:255] #limit length |
| 2702 | myNetType = net['type'] |
| 2703 | myNetDict = {} |
| 2704 | myNetDict["name"] = myNetName |
| 2705 | myNetDict["type"] = myNetType |
| 2706 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2707 | myNetIPProfile = net.get('ip_profile', None) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2708 | #print myNetDict |
| 2709 | #TODO: |
| 2710 | #We should use the dictionary as input parameter for new_network |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2711 | network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2712 | #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id) |
| 2713 | net['vim_id'] = network_id |
| 2714 | if sce_vnf['uuid'] not in auxNetDict: |
| 2715 | auxNetDict[sce_vnf['uuid']] = {} |
| 2716 | auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id |
| 2717 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2718 | net["created"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2719 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2720 | #print "auxNetDict:" |
| 2721 | #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2722 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2723 | logger.debug("start_scenario 3. Creating new vm instances in the VIM") |
| 2724 | #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 2725 | i = 0 |
| 2726 | for sce_vnf in scenarioDict['vnfs']: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2727 | vnf_availability_zones = [] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2728 | for vm in sce_vnf['vms']: |
| 2729 | vm_av = vm.get('availability_zone') |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2730 | if vm_av and vm_av not in vnf_availability_zones: |
| 2731 | vnf_availability_zones.append(vm_av) |
| 2732 | |
| 2733 | # check if there is enough availability zones available at vim level. |
| 2734 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 2735 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2736 | raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request) |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2737 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2738 | for vm in sce_vnf['vms']: |
| 2739 | i += 1 |
| 2740 | myVMDict = {} |
| 2741 | #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name']) |
| tierno | ae65a48 | 2016-11-24 16:20:05 +0100 | [diff] [blame] | 2742 | myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2743 | #myVMDict['description'] = vm['description'] |
| 2744 | myVMDict['description'] = myVMDict['name'][0:99] |
| 2745 | if not startvms: |
| 2746 | myVMDict['start'] = "no" |
| 2747 | myVMDict['name'] = myVMDict['name'][0:255] #limit name length |
| 2748 | #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2749 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2750 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2751 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2752 | image_id = create_or_use_image(mydb, vims, image_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2753 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2754 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2755 | #create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2756 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2757 | if flavor_dict['extended']!=None: |
| 2758 | flavor_dict['extended']= yaml.load(flavor_dict['extended']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2759 | flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2760 | vm['vim_flavor_id'] = flavor_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2761 | |
| 2762 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2763 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 2764 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 2765 | myVMDict['networks'] = [] |
| 2766 | for iface in vm['interfaces']: |
| 2767 | netDict = {} |
| 2768 | if iface['type']=="data": |
| 2769 | netDict['type'] = iface['model'] |
| 2770 | elif "model" in iface and iface["model"]!=None: |
| 2771 | netDict['model']=iface['model'] |
| 2772 | #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model |
| 2773 | #discover type of interface looking at flavor |
| 2774 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 2775 | for flavor_iface in numa.get('interfaces',[]): |
| 2776 | if flavor_iface.get('name') == iface['internal_name']: |
| 2777 | if flavor_iface['dedicated'] == 'yes': |
| 2778 | netDict['type']="PF" #passthrough |
| 2779 | elif flavor_iface['dedicated'] == 'no': |
| 2780 | netDict['type']="VF" #siov |
| 2781 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 2782 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 2783 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 2784 | break; |
| 2785 | netDict["use"]=iface['type'] |
| 2786 | if netDict["use"]=="data" and not netDict.get("type"): |
| 2787 | #print "netDict", netDict |
| 2788 | #print "iface", iface |
| 2789 | 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']) |
| 2790 | if flavor_dict.get('extended')==None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2791 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2792 | Try to delete and create the scenarios and VNFs again", httperrors.Conflict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2793 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2794 | raise NfvoException(e_text, httperrors.Internal_Server_Error) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2795 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 2796 | netDict["type"]="virtual" |
| 2797 | if "vpci" in iface and iface["vpci"] is not None: |
| 2798 | netDict['vpci'] = iface['vpci'] |
| 2799 | if "mac" in iface and iface["mac"] is not None: |
| 2800 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 2801 | if "port-security" in iface and iface["port-security"] is not None: |
| 2802 | netDict['port_security'] = iface['port-security'] |
| 2803 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 2804 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2805 | netDict['name'] = iface['internal_name'] |
| 2806 | if iface['net_id'] is None: |
| 2807 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2808 | #print iface |
| 2809 | #print vnf_iface |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2810 | if vnf_iface['interface_id']==iface['uuid']: |
| 2811 | netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ] |
| 2812 | break |
| 2813 | else: |
| 2814 | netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| 2815 | #skip bridge ifaces not connected to any net |
| 2816 | #if 'net_id' not in netDict or netDict['net_id']==None: |
| 2817 | # continue |
| 2818 | myVMDict['networks'].append(netDict) |
| 2819 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 2820 | #print myVMDict['name'] |
| 2821 | #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 2822 | #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 2823 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2824 | |
| 2825 | if 'availability_zone' in myVMDict: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2826 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2827 | else: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2828 | av_index = None |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2829 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2830 | vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None), |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2831 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2832 | availability_zone_index=av_index, |
| 2833 | availability_zone_list=vnf_availability_zones) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2834 | #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id) |
| 2835 | vm['vim_id'] = vm_id |
| 2836 | rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id}) |
| 2837 | #put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 2838 | for net in myVMDict['networks']: |
| 2839 | if "vim_id" in net: |
| 2840 | for iface in vm['interfaces']: |
| 2841 | if net["name"]==iface["internal_name"]: |
| 2842 | iface["vim_id"]=net["vim_id"] |
| 2843 | break |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2844 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2845 | logger.debug("start scenario Deployment done") |
| 2846 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| 2847 | #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2848 | instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict) |
| 2849 | return mydb.get_instance_scenario(instance_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2850 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2851 | except (db_base_Exception, vimconn.vimconnException) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2852 | _, message = rollback(mydb, vims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2853 | if isinstance(e, db_base_Exception): |
| 2854 | error_text = "Exception at database" |
| 2855 | else: |
| 2856 | error_text = "Exception at VIM" |
| 2857 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 2858 | #logger.error("start_scenario %s", error_text) |
| 2859 | raise NfvoException(error_text, e.http_code) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2860 | |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2861 | def unify_cloud_config(cloud_config_preserve, cloud_config): |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2862 | """ join the cloud config information into cloud_config_preserve. |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2863 | In case of conflict cloud_config_preserve preserves |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2864 | None is allowed |
| 2865 | """ |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2866 | if not cloud_config_preserve and not cloud_config: |
| 2867 | return None |
| 2868 | |
| 2869 | new_cloud_config = {"key-pairs":[], "users":[]} |
| 2870 | # key-pairs |
| 2871 | if cloud_config_preserve: |
| 2872 | for key in cloud_config_preserve.get("key-pairs", () ): |
| 2873 | if key not in new_cloud_config["key-pairs"]: |
| 2874 | new_cloud_config["key-pairs"].append(key) |
| 2875 | if cloud_config: |
| 2876 | for key in cloud_config.get("key-pairs", () ): |
| 2877 | if key not in new_cloud_config["key-pairs"]: |
| 2878 | new_cloud_config["key-pairs"].append(key) |
| 2879 | if not new_cloud_config["key-pairs"]: |
| 2880 | del new_cloud_config["key-pairs"] |
| 2881 | |
| 2882 | # users |
| 2883 | if cloud_config: |
| 2884 | new_cloud_config["users"] += cloud_config.get("users", () ) |
| 2885 | if cloud_config_preserve: |
| 2886 | new_cloud_config["users"] += cloud_config_preserve.get("users", () ) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2887 | index_to_delete = [] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2888 | users = new_cloud_config.get("users", []) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2889 | for index0 in range(0,len(users)): |
| 2890 | if index0 in index_to_delete: |
| 2891 | continue |
| 2892 | for index1 in range(index0+1,len(users)): |
| 2893 | if index1 in index_to_delete: |
| 2894 | continue |
| 2895 | if users[index0]["name"] == users[index1]["name"]: |
| 2896 | index_to_delete.append(index1) |
| 2897 | for key in users[index1].get("key-pairs",()): |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2898 | if "key-pairs" not in users[index0]: |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2899 | users[index0]["key-pairs"] = [key] |
| 2900 | elif key not in users[index0]["key-pairs"]: |
| 2901 | users[index0]["key-pairs"].append(key) |
| 2902 | index_to_delete.sort(reverse=True) |
| 2903 | for index in index_to_delete: |
| 2904 | del users[index] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2905 | if not new_cloud_config["users"]: |
| 2906 | del new_cloud_config["users"] |
| 2907 | |
| 2908 | #boot-data-drive |
| 2909 | if cloud_config and cloud_config.get("boot-data-drive") != None: |
| 2910 | new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"] |
| 2911 | if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None: |
| 2912 | new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"] |
| 2913 | |
| 2914 | # user-data |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2915 | new_cloud_config["user-data"] = [] |
| 2916 | if cloud_config and cloud_config.get("user-data"): |
| 2917 | if isinstance(cloud_config["user-data"], list): |
| 2918 | new_cloud_config["user-data"] += cloud_config["user-data"] |
| 2919 | else: |
| 2920 | new_cloud_config["user-data"].append(cloud_config["user-data"]) |
| 2921 | if cloud_config_preserve and cloud_config_preserve.get("user-data"): |
| 2922 | if isinstance(cloud_config_preserve["user-data"], list): |
| 2923 | new_cloud_config["user-data"] += cloud_config_preserve["user-data"] |
| 2924 | else: |
| 2925 | new_cloud_config["user-data"].append(cloud_config_preserve["user-data"]) |
| 2926 | if not new_cloud_config["user-data"]: |
| 2927 | del new_cloud_config["user-data"] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2928 | |
| 2929 | # config files |
| 2930 | new_cloud_config["config-files"] = [] |
| 2931 | if cloud_config and cloud_config.get("config-files") != None: |
| 2932 | new_cloud_config["config-files"] += cloud_config["config-files"] |
| 2933 | if cloud_config_preserve: |
| 2934 | for file in cloud_config_preserve.get("config-files", ()): |
| 2935 | for index in range(0, len(new_cloud_config["config-files"])): |
| 2936 | if new_cloud_config["config-files"][index]["dest"] == file["dest"]: |
| 2937 | new_cloud_config["config-files"][index] = file |
| 2938 | break |
| 2939 | else: |
| 2940 | new_cloud_config["config-files"].append(file) |
| 2941 | if not new_cloud_config["config-files"]: |
| 2942 | del new_cloud_config["config-files"] |
| 2943 | return new_cloud_config |
| 2944 | |
| 2945 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2946 | def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2947 | datacenter_id = None |
| 2948 | datacenter_name = None |
| 2949 | thread = None |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2950 | try: |
| 2951 | if datacenter_tenant_id: |
| 2952 | thread_id = datacenter_tenant_id |
| 2953 | thread = vim_threads["running"].get(datacenter_tenant_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2954 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2955 | where_={"td.nfvo_tenant_id": tenant_id} |
| 2956 | if datacenter_id_name: |
| 2957 | if utils.check_valid_uuid(datacenter_id_name): |
| 2958 | datacenter_id = datacenter_id_name |
| 2959 | where_["dt.datacenter_id"] = datacenter_id |
| 2960 | else: |
| 2961 | datacenter_name = datacenter_id_name |
| 2962 | where_["d.name"] = datacenter_name |
| 2963 | if datacenter_tenant_id: |
| 2964 | where_["dt.uuid"] = datacenter_tenant_id |
| 2965 | datacenters = mydb.get_rows( |
| 2966 | SELECT=("dt.uuid as datacenter_tenant_id",), |
| 2967 | FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id " |
| 2968 | "join datacenters as d on d.uuid=dt.datacenter_id", |
| 2969 | WHERE=where_) |
| 2970 | if len(datacenters) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2971 | raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2972 | elif datacenters: |
| 2973 | thread_id = datacenters[0]["datacenter_tenant_id"] |
| 2974 | thread = vim_threads["running"].get(thread_id) |
| 2975 | if not thread: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2976 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2977 | return thread_id, thread |
| 2978 | except db_base_Exception as e: |
| 2979 | raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2980 | |
| tierno | f575596 | 2017-07-13 15:44:34 +0200 | [diff] [blame] | 2981 | |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2982 | def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name): |
| 2983 | WHERE_dict={} |
| 2984 | if utils.check_valid_uuid(datacenter_id_name): |
| 2985 | WHERE_dict['d.uuid'] = datacenter_id_name |
| 2986 | else: |
| 2987 | WHERE_dict['d.name'] = datacenter_id_name |
| 2988 | |
| 2989 | if tenant_id: |
| 2990 | WHERE_dict['nfvo_tenant_id'] = tenant_id |
| 2991 | from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \ |
| 2992 | " dt on td.datacenter_tenant_id=dt.uuid" |
| 2993 | else: |
| 2994 | from_ = 'datacenters as d' |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 2995 | vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid, d.name as name",), WHERE=WHERE_dict ) |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2996 | if len(vimaccounts) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2997 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found) |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2998 | elif len(vimaccounts)>1: |
| 2999 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3000 | raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3001 | return vimaccounts[0]["uuid"], vimaccounts[0]["name"] |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 3002 | |
| 3003 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3004 | def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3005 | datacenter_id = None |
| 3006 | datacenter_name = None |
| 3007 | if datacenter_id_name: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3008 | if utils.check_valid_uuid(datacenter_id_name): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3009 | datacenter_id = datacenter_id_name |
| 3010 | else: |
| 3011 | datacenter_name = datacenter_id_name |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3012 | vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3013 | if len(vims) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3014 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3015 | elif len(vims)>1: |
| 3016 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3017 | raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3018 | return vims.keys()[0], vims.values()[0] |
| 3019 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3020 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3021 | def update(d, u): |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 3022 | """Takes dict d and updates it with the values in dict u. |
| 3023 | It merges all depth levels""" |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3024 | for k, v in u.iteritems(): |
| 3025 | if isinstance(v, collections.Mapping): |
| 3026 | r = update(d.get(k, {}), v) |
| 3027 | d[k] = r |
| 3028 | else: |
| 3029 | d[k] = u[k] |
| 3030 | return d |
| 3031 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3032 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3033 | def create_instance(mydb, tenant_id, instance_dict): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3034 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| 3035 | # logger.debug("Creating instance...") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3036 | scenario = instance_dict["scenario"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3037 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3038 | # find main datacenter |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3039 | myvims = {} |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3040 | myvim_threads_id = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3041 | datacenter = instance_dict.get("datacenter") |
| tierno | fc7cfbf | 2019-03-20 17:23:45 +0000 | [diff] [blame] | 3042 | default_wim_account = instance_dict.get("wim_account") |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3043 | default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 3044 | myvims[default_datacenter_id] = vim |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3045 | myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3046 | tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3047 | # myvim_tenant = myvim['tenant_id'] |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3048 | rollbackList = [] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3049 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3050 | # print "Checking that the scenario exists and getting the scenario dictionary" |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3051 | if isinstance(scenario, str): |
| 3052 | scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id], |
| 3053 | datacenter_id=default_datacenter_id) |
| 3054 | else: |
| 3055 | scenarioDict = scenario |
| 3056 | scenarioDict["uuid"] = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3057 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3058 | # logger.debug(">>>>>> Dictionaries before merging") |
| 3059 | # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256))) |
| 3060 | # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256))) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3061 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3062 | db_instance_vnfs = [] |
| 3063 | db_instance_vms = [] |
| 3064 | db_instance_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3065 | db_instance_sfis = [] |
| 3066 | db_instance_sfs = [] |
| 3067 | db_instance_classifications = [] |
| 3068 | db_instance_sfps = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3069 | db_ip_profiles = [] |
| 3070 | db_vim_actions = [] |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3071 | uuid_list = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3072 | task_index = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3073 | instance_name = instance_dict["name"] |
| 3074 | instance_uuid = str(uuid4()) |
| 3075 | uuid_list.append(instance_uuid) |
| 3076 | db_instance_scenario = { |
| 3077 | "uuid": instance_uuid, |
| 3078 | "name": instance_name, |
| 3079 | "tenant_id": tenant_id, |
| 3080 | "scenario_id": scenarioDict['uuid'], |
| 3081 | "datacenter_id": default_datacenter_id, |
| 3082 | # filled bellow 'datacenter_tenant_id' |
| 3083 | "description": instance_dict.get("description"), |
| 3084 | } |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3085 | if scenarioDict.get("cloud-config"): |
| 3086 | db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], |
| 3087 | default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3088 | instance_action_id = get_task_id() |
| 3089 | db_instance_action = { |
| 3090 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 3091 | "tenant_id": tenant_id, |
| 3092 | "instance_id": instance_uuid, |
| 3093 | "description": "CREATE", |
| 3094 | } |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3095 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3096 | # Auxiliary dictionaries from x to y |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3097 | sce_net2instance = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3098 | net2task_id = {'scenario': {}} |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3099 | # Mapping between local networks and WIMs |
| 3100 | wim_usage = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3101 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3102 | def ip_profile_IM2RO(ip_profile_im): |
| 3103 | # translate from input format to database format |
| 3104 | ip_profile_ro = {} |
| 3105 | if 'subnet-address' in ip_profile_im: |
| 3106 | ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address'] |
| 3107 | if 'ip-version' in ip_profile_im: |
| 3108 | ip_profile_ro['ip_version'] = ip_profile_im['ip-version'] |
| 3109 | if 'gateway-address' in ip_profile_im: |
| 3110 | ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address'] |
| 3111 | if 'dns-address' in ip_profile_im: |
| 3112 | ip_profile_ro['dns_address'] = ip_profile_im['dns-address'] |
| 3113 | if isinstance(ip_profile_ro['dns_address'], (list, tuple)): |
| 3114 | ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address']) |
| 3115 | if 'dhcp' in ip_profile_im: |
| 3116 | ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address') |
| 3117 | ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True) |
| 3118 | ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count') |
| 3119 | return ip_profile_ro |
| 3120 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3121 | # logger.debug("Creating instance from scenario-dict:\n%s", |
| 3122 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3123 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3124 | # 0 check correct parameters |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3125 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3126 | for scenario_net in scenarioDict['nets']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3127 | if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3128 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3129 | else: |
| 3130 | raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3131 | httperrors.Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3132 | if "sites" not in net_instance_desc: |
| 3133 | net_instance_desc["sites"] = [ {} ] |
| 3134 | site_without_datacenter_field = False |
| 3135 | for site in net_instance_desc["sites"]: |
| 3136 | if site.get("datacenter"): |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3137 | site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"]) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3138 | if site["datacenter"] not in myvims: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3139 | # Add this datacenter to myvims |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3140 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"]) |
| 3141 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3142 | myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"]) |
| 3143 | site["datacenter"] = d # change name to id |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3144 | else: |
| 3145 | if site_without_datacenter_field: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3146 | raise NfvoException("Found more than one entries without datacenter field at " |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3147 | "instance:networks:{}:sites".format(net_name), httperrors.Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3148 | site_without_datacenter_field = True |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3149 | site["datacenter"] = default_datacenter_id # change name to id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3150 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3151 | for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3152 | for scenario_vnf in scenarioDict['vnfs']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3153 | if vnf_name == scenario_vnf['member_vnf_index'] or vnf_name == scenario_vnf['uuid'] or vnf_name == scenario_vnf['name']: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3154 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3155 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3156 | raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), httperrors.Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3157 | if "datacenter" in vnf_instance_desc: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3158 | # Add this datacenter to myvims |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3159 | vnf_instance_desc["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3160 | if vnf_instance_desc["datacenter"] not in myvims: |
| 3161 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| 3162 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3163 | myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3164 | scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"] |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 3165 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3166 | for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems(): |
| 3167 | for scenario_net in scenario_vnf['nets']: |
| 3168 | if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]: |
| 3169 | break |
| 3170 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3171 | raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), httperrors.Bad_Request) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3172 | if net_instance_desc.get("vim-network-name"): |
| 3173 | scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"] |
| gcalvino | 0a48054 | 2018-12-17 16:19:33 +0100 | [diff] [blame] | 3174 | if net_instance_desc.get("vim-network-id"): |
| 3175 | scenario_net["vim-network-id"] = net_instance_desc["vim-network-id"] |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3176 | if net_instance_desc.get("name"): |
| 3177 | scenario_net["name"] = net_instance_desc["name"] |
| 3178 | if 'ip-profile' in net_instance_desc: |
| 3179 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| 3180 | if 'ip_profile' not in scenario_net: |
| 3181 | scenario_net['ip_profile'] = ipprofile_db |
| 3182 | else: |
| 3183 | update(scenario_net['ip_profile'], ipprofile_db) |
| 3184 | |
| 3185 | for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems(): |
| 3186 | for scenario_vm in scenario_vnf['vms']: |
| 3187 | if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]: |
| 3188 | break |
| 3189 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3190 | raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3191 | scenario_vm["instance_parameters"] = vdu_instance_desc |
| 3192 | for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems(): |
| 3193 | for scenario_interface in scenario_vm['interfaces']: |
| 3194 | if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]: |
| 3195 | scenario_interface.update(iface_instance_desc) |
| 3196 | break |
| 3197 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3198 | raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3199 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3200 | # 0.1 parse cloud-config parameters |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 3201 | cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config")) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3202 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3203 | # 0.2 merge instance information into scenario |
| 3204 | # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict) |
| 3205 | # However, this is not possible yet. |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3206 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3207 | for scenario_net in scenarioDict['nets']: |
| tierno | fc7cfbf | 2019-03-20 17:23:45 +0000 | [diff] [blame] | 3208 | if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"): |
| 3209 | if "wim_account" in net_instance_desc and net_instance_desc["wim_account"] is not None: |
| 3210 | scenario_net["wim_account"] = net_instance_desc["wim_account"] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3211 | if 'ip-profile' in net_instance_desc: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3212 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3213 | if 'ip_profile' not in scenario_net: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3214 | scenario_net['ip_profile'] = ipprofile_db |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3215 | else: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3216 | update(scenario_net['ip_profile'], ipprofile_db) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3217 | for interface in net_instance_desc.get('interfaces', ()): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3218 | if 'ip_address' in interface: |
| 3219 | for vnf in scenarioDict['vnfs']: |
| 3220 | if interface['vnf'] == vnf['name']: |
| 3221 | for vnf_interface in vnf['interfaces']: |
| 3222 | if interface['vnf_interface'] == vnf_interface['external_name']: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3223 | vnf_interface['ip_address'] = interface['ip_address'] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3224 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3225 | # logger.debug(">>>>>>>> Merged dictionary") |
| 3226 | # logger.debug("Creating instance scenario-dict MERGED:\n%s", |
| 3227 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3228 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3229 | # 1. Creating new nets (sce_nets) in the VIM" |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3230 | number_mgmt_networks = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3231 | db_instance_nets = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3232 | for sce_net in scenarioDict['nets']: |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3233 | sce_net_uuid = sce_net.get('uuid', sce_net["name"]) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3234 | # get involved datacenters where this network need to be created |
| 3235 | involved_datacenters = [] |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3236 | for sce_vnf in scenarioDict.get("vnfs", ()): |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3237 | vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id) |
| 3238 | if vnf_datacenter in involved_datacenters: |
| 3239 | continue |
| 3240 | if sce_vnf.get("interfaces"): |
| 3241 | for sce_vnf_ifaces in sce_vnf["interfaces"]: |
| 3242 | if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]: |
| 3243 | involved_datacenters.append(vnf_datacenter) |
| 3244 | break |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 3245 | if not involved_datacenters: |
| 3246 | involved_datacenters.append(default_datacenter_id) |
| tierno | 8039182 | 2019-03-21 22:12:14 +0000 | [diff] [blame] | 3247 | target_wim_account = sce_net.get("wim_account", default_wim_account) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3248 | |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3249 | # --> WIM |
| 3250 | # TODO: use this information during network creation |
| tierno | 4070e44 | 2019-01-23 10:19:23 +0000 | [diff] [blame] | 3251 | wim_account_id = wim_account_name = None |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3252 | if len(involved_datacenters) > 1 and 'uuid' in sce_net: |
| tierno | fc7cfbf | 2019-03-20 17:23:45 +0000 | [diff] [blame] | 3253 | if target_wim_account is None or target_wim_account is True: # automatic selection of WIM |
| 3254 | # OBS: sce_net without uuid are used internally to VNFs |
| 3255 | # and the assumption is that VNFs will not be split among |
| 3256 | # different datacenters |
| 3257 | wim_account = wim_engine.find_suitable_wim_account( |
| 3258 | involved_datacenters, tenant_id) |
| 3259 | wim_account_id = wim_account['uuid'] |
| 3260 | wim_account_name = wim_account['name'] |
| 3261 | wim_usage[sce_net['uuid']] = wim_account_id |
| 3262 | elif isinstance(target_wim_account, str): # manual selection of WIM |
| 3263 | wim_account.persist.get_wim_account_by(target_wim_account, tenant_id) |
| 3264 | wim_account_id = wim_account['uuid'] |
| 3265 | wim_account_name = wim_account['name'] |
| 3266 | wim_usage[sce_net['uuid']] = wim_account_id |
| 3267 | else: # not WIM usage |
| 3268 | wim_usage[sce_net['uuid']] = False |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3269 | # <-- WIM |
| 3270 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3271 | descriptor_net = {} |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3272 | if instance_dict.get("networks"): |
| 3273 | if sce_net.get("uuid") in instance_dict["networks"]: |
| 3274 | descriptor_net = instance_dict["networks"][sce_net["uuid"]] |
| 3275 | descriptor_net_name = sce_net["uuid"] |
| 3276 | elif sce_net.get("osm_id") in instance_dict["networks"]: |
| 3277 | descriptor_net = instance_dict["networks"][sce_net["osm_id"]] |
| 3278 | descriptor_net_name = sce_net["osm_id"] |
| 3279 | elif sce_net["name"] in instance_dict["networks"]: |
| 3280 | descriptor_net = instance_dict["networks"][sce_net["name"]] |
| 3281 | descriptor_net_name = sce_net["name"] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3282 | net_name = descriptor_net.get("vim-network-name") |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3283 | # add datacenters from instantiation parameters |
| 3284 | if descriptor_net.get("sites"): |
| 3285 | for site in descriptor_net["sites"]: |
| 3286 | if site.get("datacenter") and site["datacenter"] not in involved_datacenters: |
| 3287 | involved_datacenters.append(site["datacenter"]) |
| 3288 | sce_net2instance[sce_net_uuid] = {} |
| 3289 | net2task_id['scenario'][sce_net_uuid] = {} |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3290 | |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3291 | use_network = None |
| 3292 | related_network = None |
| 3293 | if descriptor_net.get("use-network"): |
| 3294 | target_instance_nets = mydb.get_rows( |
| 3295 | SELECT="related", |
| 3296 | FROM="instance_nets", |
| 3297 | WHERE={"instance_scenario_id": descriptor_net["use-network"]["instance_scenario_id"], |
| 3298 | "osm_id": descriptor_net["use-network"]["osm_id"]}, |
| 3299 | ) |
| 3300 | if not target_instance_nets: |
| 3301 | raise NfvoException( |
| 3302 | "Cannot find the target network at instance:networks[{}]:use-network".format(descriptor_net_name), |
| 3303 | httperrors.Bad_Request) |
| 3304 | else: |
| 3305 | use_network = target_instance_nets[0]["related"] |
| 3306 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3307 | if sce_net["external"]: |
| 3308 | number_mgmt_networks += 1 |
| 3309 | |
| 3310 | for datacenter_id in involved_datacenters: |
| 3311 | netmap_use = None |
| 3312 | netmap_create = None |
| 3313 | if descriptor_net.get("sites"): |
| 3314 | for site in descriptor_net["sites"]: |
| 3315 | if site.get("datacenter") == datacenter_id: |
| 3316 | netmap_use = site.get("netmap-use") |
| 3317 | netmap_create = site.get("netmap-create") |
| 3318 | break |
| 3319 | |
| 3320 | vim = myvims[datacenter_id] |
| 3321 | myvim_thread_id = myvim_threads_id[datacenter_id] |
| 3322 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3323 | net_type = sce_net['type'] |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3324 | net_vim_name = None |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3325 | lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3326 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3327 | if not net_name: |
| 3328 | if sce_net["external"]: |
| 3329 | net_name = sce_net["name"] |
| 3330 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3331 | net_name = "{}-{}".format(instance_name, sce_net["name"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3332 | net_name = net_name[:255] # limit length |
| 3333 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3334 | if netmap_use or netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3335 | create_network = False |
| 3336 | lookfor_network = False |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3337 | if netmap_use: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3338 | lookfor_network = True |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3339 | if utils.check_valid_uuid(netmap_use): |
| 3340 | lookfor_filter["id"] = netmap_use |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3341 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3342 | lookfor_filter["name"] = netmap_use |
| 3343 | if netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3344 | create_network = True |
| 3345 | net_vim_name = net_name |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3346 | if isinstance(netmap_create, str): |
| 3347 | net_vim_name = netmap_create |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3348 | elif sce_net.get("vim_network_name"): |
| 3349 | create_network = False |
| 3350 | lookfor_network = True |
| 3351 | lookfor_filter["name"] = sce_net.get("vim_network_name") |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3352 | elif sce_net["external"]: |
| tierno | d108c41 | 2018-12-18 15:19:27 +0000 | [diff] [blame] | 3353 | if sce_net.get('vim_id'): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3354 | # there is a netmap at datacenter_nets database # TODO REVISE!!!! |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3355 | create_network = False |
| 3356 | lookfor_network = True |
| 3357 | lookfor_filter["id"] = sce_net['vim_id'] |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3358 | elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"): |
| 3359 | if number_mgmt_networks > 1: |
| 3360 | raise NfvoException("Found several VLD of type mgmt. " |
| 3361 | "You must concrete what vim-network must be use for each one", |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3362 | httperrors.Bad_Request) |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3363 | create_network = False |
| 3364 | lookfor_network = True |
| 3365 | if vim["config"].get("management_network_id"): |
| 3366 | lookfor_filter["id"] = vim["config"]["management_network_id"] |
| 3367 | else: |
| 3368 | lookfor_filter["name"] = vim["config"]["management_network_name"] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3369 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3370 | # There is not a netmap, look at datacenter for a net with this name and create if not found |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3371 | create_network = True |
| 3372 | lookfor_network = True |
| 3373 | lookfor_filter["name"] = sce_net["name"] |
| 3374 | net_vim_name = sce_net["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3375 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3376 | net_vim_name = net_name |
| 3377 | create_network = True |
| 3378 | lookfor_network = False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3379 | |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3380 | task_extra = {} |
| 3381 | if create_network: |
| 3382 | task_action = "CREATE" |
| tierno | 4070e44 | 2019-01-23 10:19:23 +0000 | [diff] [blame] | 3383 | task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None), wim_account_name) |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3384 | if lookfor_network: |
| 3385 | task_extra["find"] = (lookfor_filter,) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3386 | elif lookfor_network: |
| 3387 | task_action = "FIND" |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3388 | task_extra["params"] = (lookfor_filter,) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3389 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3390 | # fill database content |
| 3391 | net_uuid = str(uuid4()) |
| 3392 | uuid_list.append(net_uuid) |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3393 | sce_net2instance[sce_net_uuid][datacenter_id] = net_uuid |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3394 | if not related_network: # all db_instance_nets will have same related |
| 3395 | related_network = use_network or net_uuid |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3396 | db_net = { |
| 3397 | "uuid": net_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3398 | "osm_id": sce_net.get("osm_id") or sce_net["name"], |
| 3399 | "related": related_network, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3400 | 'vim_net_id': None, |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3401 | "vim_name": net_vim_name, |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3402 | "instance_scenario_id": instance_uuid, |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3403 | "sce_net_id": sce_net.get("uuid"), |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3404 | "created": create_network, |
| 3405 | 'datacenter_id': datacenter_id, |
| 3406 | 'datacenter_tenant_id': myvim_thread_id, |
| tierno | d2836fc | 2018-05-30 15:03:27 +0200 | [diff] [blame] | 3407 | 'status': 'BUILD' # if create_network else "ACTIVE" |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3408 | } |
| 3409 | db_instance_nets.append(db_net) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3410 | db_vim_action = { |
| 3411 | "instance_action_id": instance_action_id, |
| 3412 | "status": "SCHEDULED", |
| 3413 | "task_index": task_index, |
| 3414 | "datacenter_vim_id": myvim_thread_id, |
| 3415 | "action": task_action, |
| 3416 | "item": "instance_nets", |
| 3417 | "item_id": net_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3418 | "related": related_network, |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3419 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3420 | } |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3421 | net2task_id['scenario'][sce_net_uuid][datacenter_id] = task_index |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3422 | task_index += 1 |
| 3423 | db_vim_actions.append(db_vim_action) |
| 3424 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3425 | if 'ip_profile' in sce_net: |
| 3426 | db_ip_profile={ |
| 3427 | 'instance_net_id': net_uuid, |
| 3428 | 'ip_version': sce_net['ip_profile']['ip_version'], |
| 3429 | 'subnet_address': sce_net['ip_profile']['subnet_address'], |
| 3430 | 'gateway_address': sce_net['ip_profile']['gateway_address'], |
| 3431 | 'dns_address': sce_net['ip_profile']['dns_address'], |
| 3432 | 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'], |
| 3433 | 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'], |
| 3434 | 'dhcp_count': sce_net['ip_profile']['dhcp_count'], |
| 3435 | } |
| 3436 | db_ip_profiles.append(db_ip_profile) |
| 3437 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3438 | # Create VNFs |
| 3439 | vnf_params = { |
| 3440 | "default_datacenter_id": default_datacenter_id, |
| 3441 | "myvim_threads_id": myvim_threads_id, |
| 3442 | "instance_uuid": instance_uuid, |
| 3443 | "instance_name": instance_name, |
| 3444 | "instance_action_id": instance_action_id, |
| 3445 | "myvims": myvims, |
| 3446 | "cloud_config": cloud_config, |
| 3447 | "RO_pub_key": tenant[0].get('RO_pub_key'), |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3448 | "instance_parameters": instance_dict, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3449 | } |
| 3450 | vnf_params_out = { |
| 3451 | "task_index": task_index, |
| 3452 | "uuid_list": uuid_list, |
| 3453 | "db_instance_nets": db_instance_nets, |
| 3454 | "db_vim_actions": db_vim_actions, |
| 3455 | "db_ip_profiles": db_ip_profiles, |
| 3456 | "db_instance_vnfs": db_instance_vnfs, |
| 3457 | "db_instance_vms": db_instance_vms, |
| 3458 | "db_instance_interfaces": db_instance_interfaces, |
| 3459 | "net2task_id": net2task_id, |
| 3460 | "sce_net2instance": sce_net2instance, |
| 3461 | } |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 3462 | # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3463 | for sce_vnf in scenarioDict.get('vnfs', ()): # sce_vnf_list: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3464 | instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList) |
| 3465 | task_index = vnf_params_out["task_index"] |
| 3466 | uuid_list = vnf_params_out["uuid_list"] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3467 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3468 | # Create VNFFGs |
| 3469 | # task_depends_on = [] |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3470 | for vnffg in scenarioDict.get('vnffgs', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3471 | for rsp in vnffg['rsps']: |
| 3472 | sfs_created = [] |
| 3473 | for cp in rsp['connection_points']: |
| 3474 | count = mydb.get_rows( |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 3475 | SELECT='vms.count', |
| 3476 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h " |
| 3477 | "on interfaces.uuid=h.ingress_interface_id", |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3478 | WHERE={'h.uuid': cp['uuid']})[0]['count'] |
| 3479 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None) |
| 3480 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3481 | dependencies = [] |
| 3482 | for instance_vm in instance_vms: |
| 3483 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3484 | if action: |
| 3485 | dependencies.append(action['task_index']) |
| 3486 | # TODO: throw exception if count != len(instance_vms) |
| 3487 | # TODO: and action shouldn't ever be None |
| 3488 | sfis_created = [] |
| 3489 | for i in range(count): |
| 3490 | # create sfis |
| 3491 | sfi_uuid = str(uuid4()) |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 3492 | extra_params = { |
| 3493 | "ingress_interface_id": cp["ingress_interface_id"], |
| 3494 | "egress_interface_id": cp["egress_interface_id"] |
| 3495 | } |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3496 | uuid_list.append(sfi_uuid) |
| 3497 | db_sfi = { |
| 3498 | "uuid": sfi_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3499 | "related": sfi_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3500 | "instance_scenario_id": instance_uuid, |
| 3501 | 'sce_rsp_hop_id': cp['uuid'], |
| 3502 | 'datacenter_id': datacenter_id, |
| 3503 | 'datacenter_tenant_id': myvim_thread_id, |
| 3504 | "vim_sfi_id": None, # vim thread will populate |
| 3505 | } |
| 3506 | db_instance_sfis.append(db_sfi) |
| 3507 | db_vim_action = { |
| 3508 | "instance_action_id": instance_action_id, |
| 3509 | "task_index": task_index, |
| 3510 | "datacenter_vim_id": myvim_thread_id, |
| 3511 | "action": "CREATE", |
| 3512 | "status": "SCHEDULED", |
| 3513 | "item": "instance_sfis", |
| 3514 | "item_id": sfi_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3515 | "related": sfi_uuid, |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 3516 | "extra": yaml.safe_dump({"params": extra_params, "depends_on": [dependencies[i]]}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3517 | default_flow_style=True, width=256) |
| 3518 | } |
| 3519 | sfis_created.append(task_index) |
| 3520 | task_index += 1 |
| 3521 | db_vim_actions.append(db_vim_action) |
| 3522 | # create sfs |
| 3523 | sf_uuid = str(uuid4()) |
| 3524 | uuid_list.append(sf_uuid) |
| 3525 | db_sf = { |
| 3526 | "uuid": sf_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3527 | "related": sf_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3528 | "instance_scenario_id": instance_uuid, |
| 3529 | 'sce_rsp_hop_id': cp['uuid'], |
| 3530 | 'datacenter_id': datacenter_id, |
| 3531 | 'datacenter_tenant_id': myvim_thread_id, |
| 3532 | "vim_sf_id": None, # vim thread will populate |
| 3533 | } |
| 3534 | db_instance_sfs.append(db_sf) |
| 3535 | db_vim_action = { |
| 3536 | "instance_action_id": instance_action_id, |
| 3537 | "task_index": task_index, |
| 3538 | "datacenter_vim_id": myvim_thread_id, |
| 3539 | "action": "CREATE", |
| 3540 | "status": "SCHEDULED", |
| 3541 | "item": "instance_sfs", |
| 3542 | "item_id": sf_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3543 | "related": sf_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3544 | "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created}, |
| 3545 | default_flow_style=True, width=256) |
| 3546 | } |
| 3547 | sfs_created.append(task_index) |
| 3548 | task_index += 1 |
| 3549 | db_vim_actions.append(db_vim_action) |
| 3550 | classifier = rsp['classifier'] |
| 3551 | |
| 3552 | # TODO the following ~13 lines can be reused for the sfi case |
| 3553 | count = mydb.get_rows( |
| 3554 | SELECT=('vms.count'), |
| 3555 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id", |
| 3556 | WHERE={'c.uuid': classifier['uuid']})[0]['count'] |
| 3557 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None) |
| 3558 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3559 | dependencies = [] |
| 3560 | for instance_vm in instance_vms: |
| 3561 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3562 | if action: |
| 3563 | dependencies.append(action['task_index']) |
| 3564 | # TODO: throw exception if count != len(instance_vms) |
| 3565 | # TODO: and action shouldn't ever be None |
| 3566 | classifications_created = [] |
| 3567 | for i in range(count): |
| 3568 | for match in classifier['matches']: |
| 3569 | # create classifications |
| 3570 | classification_uuid = str(uuid4()) |
| 3571 | uuid_list.append(classification_uuid) |
| 3572 | db_classification = { |
| 3573 | "uuid": classification_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3574 | "related": classification_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3575 | "instance_scenario_id": instance_uuid, |
| 3576 | 'sce_classifier_match_id': match['uuid'], |
| 3577 | 'datacenter_id': datacenter_id, |
| 3578 | 'datacenter_tenant_id': myvim_thread_id, |
| 3579 | "vim_classification_id": None, # vim thread will populate |
| 3580 | } |
| 3581 | db_instance_classifications.append(db_classification) |
| 3582 | classification_params = { |
| 3583 | "ip_proto": match["ip_proto"], |
| 3584 | "source_ip": match["source_ip"], |
| 3585 | "destination_ip": match["destination_ip"], |
| 3586 | "source_port": match["source_port"], |
| 3587 | "destination_port": match["destination_port"] |
| 3588 | } |
| 3589 | db_vim_action = { |
| 3590 | "instance_action_id": instance_action_id, |
| 3591 | "task_index": task_index, |
| 3592 | "datacenter_vim_id": myvim_thread_id, |
| 3593 | "action": "CREATE", |
| 3594 | "status": "SCHEDULED", |
| 3595 | "item": "instance_classifications", |
| 3596 | "item_id": classification_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3597 | "related": classification_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3598 | "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]}, |
| 3599 | default_flow_style=True, width=256) |
| 3600 | } |
| 3601 | classifications_created.append(task_index) |
| 3602 | task_index += 1 |
| 3603 | db_vim_actions.append(db_vim_action) |
| 3604 | |
| 3605 | # create sfps |
| 3606 | sfp_uuid = str(uuid4()) |
| 3607 | uuid_list.append(sfp_uuid) |
| 3608 | db_sfp = { |
| 3609 | "uuid": sfp_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3610 | "related": sfp_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3611 | "instance_scenario_id": instance_uuid, |
| 3612 | 'sce_rsp_id': rsp['uuid'], |
| 3613 | 'datacenter_id': datacenter_id, |
| 3614 | 'datacenter_tenant_id': myvim_thread_id, |
| 3615 | "vim_sfp_id": None, # vim thread will populate |
| 3616 | } |
| 3617 | db_instance_sfps.append(db_sfp) |
| 3618 | db_vim_action = { |
| 3619 | "instance_action_id": instance_action_id, |
| 3620 | "task_index": task_index, |
| 3621 | "datacenter_vim_id": myvim_thread_id, |
| 3622 | "action": "CREATE", |
| 3623 | "status": "SCHEDULED", |
| 3624 | "item": "instance_sfps", |
| 3625 | "item_id": sfp_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3626 | "related": sfp_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3627 | "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created}, |
| 3628 | default_flow_style=True, width=256) |
| 3629 | } |
| 3630 | task_index += 1 |
| 3631 | db_vim_actions.append(db_vim_action) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3632 | db_instance_action["number_tasks"] = task_index |
| 3633 | |
| 3634 | # --> WIM |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3635 | logger.debug('wim_usage:\n%s\n\n', pformat(wim_usage)) |
| 3636 | wan_links = wim_engine.derive_wan_links(wim_usage, db_instance_nets, tenant_id) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3637 | wim_actions = wim_engine.create_actions(wan_links) |
| 3638 | wim_actions, db_instance_action = ( |
| 3639 | wim_engine.incorporate_actions(wim_actions, db_instance_action)) |
| 3640 | # <-- WIM |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3641 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3642 | scenarioDict["datacenter2tenant"] = myvim_threads_id |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3643 | |
| 3644 | db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id] |
| 3645 | db_instance_scenario['datacenter_id'] = default_datacenter_id |
| 3646 | db_tables=[ |
| 3647 | {"instance_scenarios": db_instance_scenario}, |
| 3648 | {"instance_vnfs": db_instance_vnfs}, |
| 3649 | {"instance_nets": db_instance_nets}, |
| 3650 | {"ip_profiles": db_ip_profiles}, |
| 3651 | {"instance_vms": db_instance_vms}, |
| 3652 | {"instance_interfaces": db_instance_interfaces}, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3653 | {"instance_actions": db_instance_action}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3654 | {"instance_sfis": db_instance_sfis}, |
| 3655 | {"instance_sfs": db_instance_sfs}, |
| 3656 | {"instance_classifications": db_instance_classifications}, |
| 3657 | {"instance_sfps": db_instance_sfps}, |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3658 | {"instance_wim_nets": wan_links}, |
| 3659 | {"vim_wim_actions": db_vim_actions + wim_actions} |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3660 | ] |
| 3661 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3662 | logger.debug("create_instance done DB tables: %s", |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3663 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 3664 | mydb.new_rows(db_tables, uuid_list) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3665 | for myvim_thread_id in myvim_threads_id.values(): |
| 3666 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3667 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3668 | wim_engine.dispatch(wim_actions) |
| 3669 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3670 | returned_instance = mydb.get_instance_scenario(instance_uuid) |
| 3671 | returned_instance["action_id"] = instance_action_id |
| 3672 | return returned_instance |
| tierno | 4491ba9 | 2019-03-25 15:00:02 +0000 | [diff] [blame] | 3673 | except (NfvoException, vimconn.vimconnException, wimconn.WimConnectorError, db_base_Exception) as e: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3674 | message = rollback(mydb, myvims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3675 | if isinstance(e, db_base_Exception): |
| 3676 | error_text = "database Exception" |
| 3677 | elif isinstance(e, vimconn.vimconnException): |
| 3678 | error_text = "VIM Exception" |
| tierno | 4491ba9 | 2019-03-25 15:00:02 +0000 | [diff] [blame] | 3679 | elif isinstance(e, wimconn.WimConnectorError): |
| 3680 | error_text = "WIM Exception" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3681 | else: |
| 3682 | error_text = "Exception" |
| 3683 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3684 | # logger.error("create_instance: %s", error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3685 | logger.exception(e) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3686 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3687 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3688 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3689 | def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): |
| 3690 | default_datacenter_id = params["default_datacenter_id"] |
| 3691 | myvim_threads_id = params["myvim_threads_id"] |
| 3692 | instance_uuid = params["instance_uuid"] |
| 3693 | instance_name = params["instance_name"] |
| 3694 | instance_action_id = params["instance_action_id"] |
| 3695 | myvims = params["myvims"] |
| 3696 | cloud_config = params["cloud_config"] |
| 3697 | RO_pub_key = params["RO_pub_key"] |
| 3698 | |
| 3699 | task_index = params_out["task_index"] |
| 3700 | uuid_list = params_out["uuid_list"] |
| 3701 | db_instance_nets = params_out["db_instance_nets"] |
| 3702 | db_vim_actions = params_out["db_vim_actions"] |
| 3703 | db_ip_profiles = params_out["db_ip_profiles"] |
| 3704 | db_instance_vnfs = params_out["db_instance_vnfs"] |
| 3705 | db_instance_vms = params_out["db_instance_vms"] |
| 3706 | db_instance_interfaces = params_out["db_instance_interfaces"] |
| 3707 | net2task_id = params_out["net2task_id"] |
| 3708 | sce_net2instance = params_out["sce_net2instance"] |
| 3709 | |
| 3710 | vnf_net2instance = {} |
| 3711 | |
| 3712 | # 2. Creating new nets (vnf internal nets) in the VIM" |
| 3713 | # For each vnf net, we create it and we add it to instanceNetlist. |
| 3714 | if sce_vnf.get("datacenter"): |
| 3715 | datacenter_id = sce_vnf["datacenter"] |
| 3716 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3717 | else: |
| 3718 | datacenter_id = default_datacenter_id |
| 3719 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3720 | for net in sce_vnf['nets']: |
| 3721 | # TODO revis |
| 3722 | # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {}) |
| 3723 | # net_name = descriptor_net.get("name") |
| 3724 | net_name = None |
| 3725 | if not net_name: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3726 | net_name = "{}-{}".format(instance_name, net["name"]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3727 | net_name = net_name[:255] # limit length |
| 3728 | net_type = net['type'] |
| 3729 | |
| 3730 | if sce_vnf['uuid'] not in vnf_net2instance: |
| 3731 | vnf_net2instance[sce_vnf['uuid']] = {} |
| 3732 | if sce_vnf['uuid'] not in net2task_id: |
| 3733 | net2task_id[sce_vnf['uuid']] = {} |
| 3734 | net2task_id[sce_vnf['uuid']][net['uuid']] = task_index |
| 3735 | |
| 3736 | # fill database content |
| 3737 | net_uuid = str(uuid4()) |
| 3738 | uuid_list.append(net_uuid) |
| 3739 | vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid |
| 3740 | db_net = { |
| 3741 | "uuid": net_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3742 | "related": net_uuid, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3743 | 'vim_net_id': None, |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3744 | "vim_name": net_name, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3745 | "instance_scenario_id": instance_uuid, |
| 3746 | "net_id": net["uuid"], |
| 3747 | "created": True, |
| 3748 | 'datacenter_id': datacenter_id, |
| 3749 | 'datacenter_tenant_id': myvim_thread_id, |
| 3750 | } |
| 3751 | db_instance_nets.append(db_net) |
| 3752 | |
| gcalvino | 0a48054 | 2018-12-17 16:19:33 +0100 | [diff] [blame] | 3753 | lookfor_filter = {} |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3754 | if net.get("vim-network-name"): |
| gcalvino | 0a48054 | 2018-12-17 16:19:33 +0100 | [diff] [blame] | 3755 | lookfor_filter["name"] = net["vim-network-name"] |
| 3756 | if net.get("vim-network-id"): |
| 3757 | lookfor_filter["id"] = net["vim-network-id"] |
| 3758 | if lookfor_filter: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3759 | task_action = "FIND" |
| 3760 | task_extra = {"params": (lookfor_filter,)} |
| 3761 | else: |
| 3762 | task_action = "CREATE" |
| 3763 | task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))} |
| 3764 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3765 | db_vim_action = { |
| 3766 | "instance_action_id": instance_action_id, |
| 3767 | "task_index": task_index, |
| 3768 | "datacenter_vim_id": myvim_thread_id, |
| 3769 | "status": "SCHEDULED", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3770 | "action": task_action, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3771 | "item": "instance_nets", |
| 3772 | "item_id": net_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 3773 | "related": net_uuid, |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3774 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3775 | } |
| 3776 | task_index += 1 |
| 3777 | db_vim_actions.append(db_vim_action) |
| 3778 | |
| 3779 | if 'ip_profile' in net: |
| 3780 | db_ip_profile = { |
| 3781 | 'instance_net_id': net_uuid, |
| 3782 | 'ip_version': net['ip_profile']['ip_version'], |
| 3783 | 'subnet_address': net['ip_profile']['subnet_address'], |
| 3784 | 'gateway_address': net['ip_profile']['gateway_address'], |
| 3785 | 'dns_address': net['ip_profile']['dns_address'], |
| 3786 | 'dhcp_enabled': net['ip_profile']['dhcp_enabled'], |
| 3787 | 'dhcp_start_address': net['ip_profile']['dhcp_start_address'], |
| 3788 | 'dhcp_count': net['ip_profile']['dhcp_count'], |
| 3789 | } |
| 3790 | db_ip_profiles.append(db_ip_profile) |
| 3791 | |
| 3792 | # print "vnf_net2instance:" |
| 3793 | # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False) |
| 3794 | |
| 3795 | # 3. Creating new vm instances in the VIM |
| 3796 | # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 3797 | ssh_access = None |
| 3798 | if sce_vnf.get('mgmt_access'): |
| 3799 | ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access') |
| 3800 | vnf_availability_zones = [] |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 3801 | for vm in sce_vnf.get('vms'): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3802 | vm_av = vm.get('availability_zone') |
| 3803 | if vm_av and vm_av not in vnf_availability_zones: |
| 3804 | vnf_availability_zones.append(vm_av) |
| 3805 | |
| 3806 | # check if there is enough availability zones available at vim level. |
| 3807 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 3808 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3809 | raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3810 | |
| 3811 | if sce_vnf.get("datacenter"): |
| 3812 | vim = myvims[sce_vnf["datacenter"]] |
| 3813 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3814 | datacenter_id = sce_vnf["datacenter"] |
| 3815 | else: |
| 3816 | vim = myvims[default_datacenter_id] |
| 3817 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3818 | datacenter_id = default_datacenter_id |
| 3819 | sce_vnf["datacenter_id"] = datacenter_id |
| 3820 | i = 0 |
| 3821 | |
| 3822 | vnf_uuid = str(uuid4()) |
| 3823 | uuid_list.append(vnf_uuid) |
| 3824 | db_instance_vnf = { |
| 3825 | 'uuid': vnf_uuid, |
| 3826 | 'instance_scenario_id': instance_uuid, |
| 3827 | 'vnf_id': sce_vnf['vnf_id'], |
| 3828 | 'sce_vnf_id': sce_vnf['uuid'], |
| 3829 | 'datacenter_id': datacenter_id, |
| 3830 | 'datacenter_tenant_id': myvim_thread_id, |
| 3831 | } |
| 3832 | db_instance_vnfs.append(db_instance_vnf) |
| 3833 | |
| 3834 | for vm in sce_vnf['vms']: |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3835 | # skip PDUs |
| 3836 | if vm.get("pdu_type"): |
| 3837 | continue |
| 3838 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3839 | myVMDict = {} |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 3840 | sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name'] |
| 3841 | myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3842 | myVMDict['description'] = myVMDict['name'][0:99] |
| 3843 | # if not startvms: |
| 3844 | # myVMDict['start'] = "no" |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3845 | if vm.get("instance_parameters") and vm["instance_parameters"].get("name"): |
| 3846 | myVMDict['name'] = vm["instance_parameters"].get("name") |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3847 | myVMDict['name'] = myVMDict['name'][0:255] # limit name length |
| 3848 | # create image at vim in case it not exist |
| 3849 | image_uuid = vm['image_id'] |
| 3850 | if vm.get("image_list"): |
| 3851 | for alternative_image in vm["image_list"]: |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 3852 | if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3853 | image_uuid = alternative_image['image_id'] |
| 3854 | break |
| 3855 | image_dict = mydb.get_table_by_uuid_name("images", image_uuid) |
| 3856 | image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True) |
| 3857 | vm['vim_image_id'] = image_id |
| 3858 | |
| 3859 | # create flavor at vim in case it not exist |
| 3860 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| 3861 | if flavor_dict['extended'] != None: |
| 3862 | flavor_dict['extended'] = yaml.load(flavor_dict['extended']) |
| 3863 | flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True) |
| 3864 | |
| 3865 | # Obtain information for additional disks |
| 3866 | extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), |
| 3867 | WHERE={'vim_id': flavor_id}) |
| 3868 | if not extended_flavor_dict: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3869 | raise NfvoException("flavor '{}' not found".format(flavor_id), httperrors.Not_Found) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3870 | |
| 3871 | # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0]) |
| 3872 | myVMDict['disks'] = None |
| 3873 | extended_info = extended_flavor_dict[0]['extended'] |
| 3874 | if extended_info != None: |
| 3875 | extended_flavor_dict_yaml = yaml.load(extended_info) |
| 3876 | if 'disks' in extended_flavor_dict_yaml: |
| 3877 | myVMDict['disks'] = extended_flavor_dict_yaml['disks'] |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3878 | if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"): |
| 3879 | for disk in myVMDict['disks']: |
| 3880 | if disk.get("name") in vm["instance_parameters"]["devices"]: |
| 3881 | disk.update(vm["instance_parameters"]["devices"][disk.get("name")]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3882 | |
| 3883 | vm['vim_flavor_id'] = flavor_id |
| 3884 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 3885 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 3886 | myVMDict['availability_zone'] = vm.get('availability_zone') |
| 3887 | myVMDict['networks'] = [] |
| 3888 | task_depends_on = [] |
| 3889 | # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3890 | is_management_vm = False |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3891 | db_vm_ifaces = [] |
| 3892 | for iface in vm['interfaces']: |
| 3893 | netDict = {} |
| 3894 | if iface['type'] == "data": |
| 3895 | netDict['type'] = iface['model'] |
| 3896 | elif "model" in iface and iface["model"] != None: |
| 3897 | netDict['model'] = iface['model'] |
| 3898 | # TODO in future, remove this because mac_address will not be set, and the type of PV,VF |
| 3899 | # is obtained from iterface table model |
| 3900 | # discover type of interface looking at flavor |
| 3901 | for numa in flavor_dict.get('extended', {}).get('numas', []): |
| 3902 | for flavor_iface in numa.get('interfaces', []): |
| 3903 | if flavor_iface.get('name') == iface['internal_name']: |
| 3904 | if flavor_iface['dedicated'] == 'yes': |
| 3905 | netDict['type'] = "PF" # passthrough |
| 3906 | elif flavor_iface['dedicated'] == 'no': |
| 3907 | netDict['type'] = "VF" # siov |
| 3908 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 3909 | netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF |
| 3910 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 3911 | break |
| 3912 | netDict["use"] = iface['type'] |
| 3913 | if netDict["use"] == "data" and not netDict.get("type"): |
| 3914 | # print "netDict", netDict |
| 3915 | # print "iface", iface |
| 3916 | e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat( |
| 3917 | sce_vnf['name'], vm['name'], iface['internal_name']) |
| 3918 | if flavor_dict.get('extended') == None: |
| 3919 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3920 | Try to delete and create the scenarios and VNFs again", httperrors.Conflict) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3921 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3922 | raise NfvoException(e_text, httperrors.Internal_Server_Error) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3923 | if netDict["use"] == "mgmt": |
| 3924 | is_management_vm = True |
| 3925 | netDict["type"] = "virtual" |
| 3926 | if netDict["use"] == "bridge": |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3927 | netDict["type"] = "virtual" |
| 3928 | if iface.get("vpci"): |
| 3929 | netDict['vpci'] = iface['vpci'] |
| 3930 | if iface.get("mac"): |
| 3931 | netDict['mac_address'] = iface['mac'] |
| tierno | 6082b7d | 2018-08-31 11:24:08 +0000 | [diff] [blame] | 3932 | if iface.get("mac_address"): |
| 3933 | netDict['mac_address'] = iface['mac_address'] |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3934 | if iface.get("ip_address"): |
| 3935 | netDict['ip_address'] = iface['ip_address'] |
| 3936 | if iface.get("port-security") is not None: |
| 3937 | netDict['port_security'] = iface['port-security'] |
| 3938 | if iface.get("floating-ip") is not None: |
| 3939 | netDict['floating_ip'] = iface['floating-ip'] |
| 3940 | netDict['name'] = iface['internal_name'] |
| 3941 | if iface['net_id'] is None: |
| 3942 | for vnf_iface in sce_vnf["interfaces"]: |
| 3943 | # print iface |
| 3944 | # print vnf_iface |
| 3945 | if vnf_iface['interface_id'] == iface['uuid']: |
| 3946 | netDict['net_id'] = "TASK-{}".format( |
| 3947 | net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3948 | instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id] |
| 3949 | task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3950 | break |
| 3951 | else: |
| 3952 | netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3953 | instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']] |
| 3954 | task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3955 | # skip bridge ifaces not connected to any net |
| 3956 | if 'net_id' not in netDict or netDict['net_id'] == None: |
| 3957 | continue |
| 3958 | myVMDict['networks'].append(netDict) |
| 3959 | db_vm_iface = { |
| 3960 | # "uuid" |
| 3961 | # 'instance_vm_id': instance_vm_uuid, |
| 3962 | "instance_net_id": instance_net_id, |
| 3963 | 'interface_id': iface['uuid'], |
| 3964 | # 'vim_interface_id': , |
| 3965 | 'type': 'external' if iface['external_name'] is not None else 'internal', |
| 3966 | 'ip_address': iface.get('ip_address'), |
| 3967 | 'mac_address': iface.get('mac'), |
| 3968 | 'floating_ip': int(iface.get('floating-ip', False)), |
| 3969 | 'port_security': int(iface.get('port-security', True)) |
| 3970 | } |
| 3971 | db_vm_ifaces.append(db_vm_iface) |
| 3972 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3973 | # print myVMDict['name'] |
| 3974 | # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 3975 | # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 3976 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3977 | |
| 3978 | # We add the RO key to cloud_config if vnf will need ssh access |
| 3979 | cloud_config_vm = cloud_config |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3980 | if is_management_vm and params["instance_parameters"].get("mgmt_keys"): |
| 3981 | cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]}, |
| 3982 | cloud_config_vm) |
| 3983 | |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 3984 | if vm.get("instance_parameters") and "mgmt_keys" in vm["instance_parameters"]: |
| 3985 | if vm["instance_parameters"]["mgmt_keys"]: |
| 3986 | cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]}, |
| 3987 | cloud_config_vm) |
| 3988 | if RO_pub_key: |
| calvinosanch | 5db670b | 2019-10-13 15:52:33 +0200 | [diff] [blame] | 3989 | cloud_config_vm = unify_cloud_config(cloud_config_vm, {"key-pairs": [RO_pub_key]}) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3990 | if vm.get("boot_data"): |
| 3991 | cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm) |
| 3992 | |
| 3993 | if myVMDict.get('availability_zone'): |
| 3994 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| 3995 | else: |
| 3996 | av_index = None |
| 3997 | for vm_index in range(0, vm.get('count', 1)): |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 3998 | vm_name = myVMDict['name'] + "-" + str(vm_index+1) |
| 3999 | task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4000 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm, |
| 4001 | myVMDict['disks'], av_index, vnf_availability_zones) |
| 4002 | # put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 4003 | for net in myVMDict['networks']: |
| 4004 | if "vim_id" in net: |
| 4005 | for iface in vm['interfaces']: |
| 4006 | if net["name"] == iface["internal_name"]: |
| 4007 | iface["vim_id"] = net["vim_id"] |
| 4008 | break |
| 4009 | vm_uuid = str(uuid4()) |
| 4010 | uuid_list.append(vm_uuid) |
| 4011 | db_vm = { |
| 4012 | "uuid": vm_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4013 | "related": vm_uuid, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4014 | 'instance_vnf_id': vnf_uuid, |
| 4015 | # TODO delete "vim_vm_id": vm_id, |
| 4016 | "vm_id": vm["uuid"], |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4017 | "vim_name": vm_name, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4018 | # "status": |
| 4019 | } |
| 4020 | db_instance_vms.append(db_vm) |
| 4021 | |
| 4022 | iface_index = 0 |
| 4023 | for db_vm_iface in db_vm_ifaces: |
| 4024 | iface_uuid = str(uuid4()) |
| 4025 | uuid_list.append(iface_uuid) |
| 4026 | db_vm_iface_instance = { |
| 4027 | "uuid": iface_uuid, |
| 4028 | "instance_vm_id": vm_uuid |
| 4029 | } |
| 4030 | db_vm_iface_instance.update(db_vm_iface) |
| 4031 | if db_vm_iface_instance.get("ip_address"): # increment ip_address |
| 4032 | ip = db_vm_iface_instance.get("ip_address") |
| 4033 | i = ip.rfind(".") |
| 4034 | if i > 0: |
| 4035 | try: |
| 4036 | i += 1 |
| 4037 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 4038 | db_vm_iface_instance["ip_address"] = ip |
| 4039 | except: |
| 4040 | db_vm_iface_instance["ip_address"] = None |
| 4041 | db_instance_interfaces.append(db_vm_iface_instance) |
| 4042 | myVMDict['networks'][iface_index]["uuid"] = iface_uuid |
| 4043 | iface_index += 1 |
| 4044 | |
| 4045 | db_vim_action = { |
| 4046 | "instance_action_id": instance_action_id, |
| 4047 | "task_index": task_index, |
| 4048 | "datacenter_vim_id": myvim_thread_id, |
| 4049 | "action": "CREATE", |
| 4050 | "status": "SCHEDULED", |
| 4051 | "item": "instance_vms", |
| 4052 | "item_id": vm_uuid, |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4053 | "related": vm_uuid, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4054 | "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on}, |
| 4055 | default_flow_style=True, width=256) |
| 4056 | } |
| 4057 | task_index += 1 |
| 4058 | db_vim_actions.append(db_vim_action) |
| 4059 | params_out["task_index"] = task_index |
| 4060 | params_out["uuid_list"] = uuid_list |
| 4061 | |
| 4062 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4063 | def delete_instance(mydb, tenant_id, instance_id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4064 | # print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4065 | instanceDict = mydb.get_instance_scenario(instance_id, tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4066 | # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4067 | tenant_id = instanceDict["tenant_id"] |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4068 | |
| 4069 | # --> WIM |
| 4070 | # We need to retrieve the WIM Actions now, before the instance_scenario is |
| 4071 | # deleted. The reason for that is that: ON CASCADE rules will delete the |
| 4072 | # instance_wim_nets record in the database |
| 4073 | wim_actions = wim_engine.delete_actions(instance_scenario_id=instance_id) |
| 4074 | # <-- WIM |
| 4075 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4076 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4077 | # 1. Delete from Database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4078 | message = mydb.delete_instance_scenario(instance_id, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4079 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4080 | # 2. delete from VIM |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4081 | error_msg = "" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4082 | myvims = {} |
| 4083 | myvim_threads = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4084 | vimthread_affected = {} |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 4085 | net2vm_dependencies = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4086 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4087 | task_index = 0 |
| 4088 | instance_action_id = get_task_id() |
| 4089 | db_vim_actions = [] |
| 4090 | db_instance_action = { |
| 4091 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 4092 | "tenant_id": tenant_id, |
| 4093 | "instance_id": instance_id, |
| 4094 | "description": "DELETE", |
| 4095 | # "number_tasks": 0 # filled bellow |
| 4096 | } |
| 4097 | |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4098 | # 2.1 deleting VNFFGs |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 4099 | for sfp in instanceDict.get('sfps', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4100 | vimthread_affected[sfp["datacenter_tenant_id"]] = None |
| 4101 | datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"]) |
| 4102 | if datacenter_key not in myvims: |
| 4103 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4104 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4105 | except NfvoException as e: |
| 4106 | logger.error(str(e)) |
| 4107 | myvim_thread = None |
| 4108 | myvim_threads[datacenter_key] = myvim_thread |
| 4109 | vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"], |
| 4110 | datacenter_tenant_id=sfp["datacenter_tenant_id"]) |
| 4111 | if len(vims) == 0: |
| 4112 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"])) |
| 4113 | myvims[datacenter_key] = None |
| 4114 | else: |
| 4115 | myvims[datacenter_key] = vims.values()[0] |
| 4116 | myvim = myvims[datacenter_key] |
| 4117 | myvim_thread = myvim_threads[datacenter_key] |
| 4118 | |
| 4119 | if not myvim: |
| 4120 | error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"]) |
| 4121 | continue |
| 4122 | extra = {"params": (sfp['vim_sfp_id'])} |
| 4123 | db_vim_action = { |
| 4124 | "instance_action_id": instance_action_id, |
| 4125 | "task_index": task_index, |
| 4126 | "datacenter_vim_id": sfp["datacenter_tenant_id"], |
| 4127 | "action": "DELETE", |
| 4128 | "status": "SCHEDULED", |
| 4129 | "item": "instance_sfps", |
| 4130 | "item_id": sfp["uuid"], |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4131 | "related": sfp["related"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4132 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4133 | } |
| 4134 | task_index += 1 |
| 4135 | db_vim_actions.append(db_vim_action) |
| 4136 | |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4137 | for classification in instanceDict['classifications']: |
| 4138 | vimthread_affected[classification["datacenter_tenant_id"]] = None |
| 4139 | datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 4140 | if datacenter_key not in myvims: |
| 4141 | try: |
| 4142 | _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 4143 | except NfvoException as e: |
| 4144 | logger.error(str(e)) |
| 4145 | myvim_thread = None |
| 4146 | myvim_threads[datacenter_key] = myvim_thread |
| 4147 | vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"], |
| 4148 | datacenter_tenant_id=classification["datacenter_tenant_id"]) |
| 4149 | if len(vims) == 0: |
| 4150 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"], |
| 4151 | classification["datacenter_tenant_id"])) |
| 4152 | myvims[datacenter_key] = None |
| 4153 | else: |
| 4154 | myvims[datacenter_key] = vims.values()[0] |
| 4155 | myvim = myvims[datacenter_key] |
| 4156 | myvim_thread = myvim_threads[datacenter_key] |
| 4157 | |
| 4158 | if not myvim: |
| 4159 | error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'], |
| 4160 | classification["datacenter_id"]) |
| 4161 | continue |
| 4162 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"] |
| 4163 | extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on} |
| 4164 | db_vim_action = { |
| 4165 | "instance_action_id": instance_action_id, |
| 4166 | "task_index": task_index, |
| 4167 | "datacenter_vim_id": classification["datacenter_tenant_id"], |
| 4168 | "action": "DELETE", |
| 4169 | "status": "SCHEDULED", |
| 4170 | "item": "instance_classifications", |
| 4171 | "item_id": classification["uuid"], |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4172 | "related": classification["related"], |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4173 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4174 | } |
| 4175 | task_index += 1 |
| 4176 | db_vim_actions.append(db_vim_action) |
| 4177 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 4178 | for sf in instanceDict.get('sfs', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4179 | vimthread_affected[sf["datacenter_tenant_id"]] = None |
| 4180 | datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"]) |
| 4181 | if datacenter_key not in myvims: |
| 4182 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4183 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4184 | except NfvoException as e: |
| 4185 | logger.error(str(e)) |
| 4186 | myvim_thread = None |
| 4187 | myvim_threads[datacenter_key] = myvim_thread |
| 4188 | vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"], |
| 4189 | datacenter_tenant_id=sf["datacenter_tenant_id"]) |
| 4190 | if len(vims) == 0: |
| 4191 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"])) |
| 4192 | myvims[datacenter_key] = None |
| 4193 | else: |
| 4194 | myvims[datacenter_key] = vims.values()[0] |
| 4195 | myvim = myvims[datacenter_key] |
| 4196 | myvim_thread = myvim_threads[datacenter_key] |
| 4197 | |
| 4198 | if not myvim: |
| 4199 | error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"]) |
| 4200 | continue |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4201 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"] |
| 4202 | extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4203 | db_vim_action = { |
| 4204 | "instance_action_id": instance_action_id, |
| 4205 | "task_index": task_index, |
| 4206 | "datacenter_vim_id": sf["datacenter_tenant_id"], |
| 4207 | "action": "DELETE", |
| 4208 | "status": "SCHEDULED", |
| 4209 | "item": "instance_sfs", |
| 4210 | "item_id": sf["uuid"], |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4211 | "related": sf["related"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4212 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4213 | } |
| 4214 | task_index += 1 |
| 4215 | db_vim_actions.append(db_vim_action) |
| 4216 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 4217 | for sfi in instanceDict.get('sfis', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4218 | vimthread_affected[sfi["datacenter_tenant_id"]] = None |
| 4219 | datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"]) |
| 4220 | if datacenter_key not in myvims: |
| 4221 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4222 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4223 | except NfvoException as e: |
| 4224 | logger.error(str(e)) |
| 4225 | myvim_thread = None |
| 4226 | myvim_threads[datacenter_key] = myvim_thread |
| 4227 | vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"], |
| 4228 | datacenter_tenant_id=sfi["datacenter_tenant_id"]) |
| 4229 | if len(vims) == 0: |
| 4230 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["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 | if not myvim: |
| 4238 | error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"]) |
| 4239 | continue |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4240 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"] |
| 4241 | extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4242 | db_vim_action = { |
| 4243 | "instance_action_id": instance_action_id, |
| 4244 | "task_index": task_index, |
| 4245 | "datacenter_vim_id": sfi["datacenter_tenant_id"], |
| 4246 | "action": "DELETE", |
| 4247 | "status": "SCHEDULED", |
| 4248 | "item": "instance_sfis", |
| 4249 | "item_id": sfi["uuid"], |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4250 | "related": sfi["related"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4251 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4252 | } |
| 4253 | task_index += 1 |
| 4254 | db_vim_actions.append(db_vim_action) |
| 4255 | |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4256 | # 2.2 deleting VMs |
| 4257 | # vm_fail_list=[] |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 4258 | for sce_vnf in instanceDict.get('vnfs', ()): |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4259 | datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4260 | vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4261 | if datacenter_key not in myvims: |
| 4262 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4263 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4264 | except NfvoException as e: |
| 4265 | logger.error(str(e)) |
| 4266 | myvim_thread = None |
| 4267 | myvim_threads[datacenter_key] = myvim_thread |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4268 | vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"], |
| 4269 | datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4270 | if len(vims) == 0: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4271 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], |
| 4272 | sce_vnf["datacenter_tenant_id"])) |
| 4273 | myvims[datacenter_key] = None |
| 4274 | else: |
| 4275 | myvims[datacenter_key] = vims.values()[0] |
| 4276 | myvim = myvims[datacenter_key] |
| 4277 | myvim_thread = myvim_threads[datacenter_key] |
| 4278 | |
| 4279 | for vm in sce_vnf['vms']: |
| 4280 | if not myvim: |
| 4281 | error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"]) |
| 4282 | continue |
| 4283 | sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"] |
| 4284 | db_vim_action = { |
| 4285 | "instance_action_id": instance_action_id, |
| 4286 | "task_index": task_index, |
| 4287 | "datacenter_vim_id": sce_vnf["datacenter_tenant_id"], |
| 4288 | "action": "DELETE", |
| 4289 | "status": "SCHEDULED", |
| 4290 | "item": "instance_vms", |
| 4291 | "item_id": vm["uuid"], |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4292 | "related": vm["related"], |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4293 | "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies}, |
| 4294 | default_flow_style=True, width=256) |
| 4295 | } |
| 4296 | db_vim_actions.append(db_vim_action) |
| 4297 | for interface in vm["interfaces"]: |
| 4298 | if not interface.get("instance_net_id"): |
| 4299 | continue |
| 4300 | if interface["instance_net_id"] not in net2vm_dependencies: |
| 4301 | net2vm_dependencies[interface["instance_net_id"]] = [] |
| 4302 | net2vm_dependencies[interface["instance_net_id"]].append(task_index) |
| 4303 | task_index += 1 |
| 4304 | |
| 4305 | # 2.3 deleting NETS |
| 4306 | # net_fail_list=[] |
| 4307 | for net in instanceDict['nets']: |
| 4308 | vimthread_affected[net["datacenter_tenant_id"]] = None |
| 4309 | datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4310 | if datacenter_key not in myvims: |
| 4311 | try: |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 4312 | _,myvim_thread = get_vim_thread(mydb, tenant_id, net["datacenter_id"], net["datacenter_tenant_id"]) |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4313 | except NfvoException as e: |
| 4314 | logger.error(str(e)) |
| 4315 | myvim_thread = None |
| 4316 | myvim_threads[datacenter_key] = myvim_thread |
| 4317 | vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"], |
| 4318 | datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 4319 | if len(vims) == 0: |
| 4320 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4321 | myvims[datacenter_key] = None |
| 4322 | else: |
| 4323 | myvims[datacenter_key] = vims.values()[0] |
| 4324 | myvim = myvims[datacenter_key] |
| 4325 | myvim_thread = myvim_threads[datacenter_key] |
| 4326 | |
| 4327 | if not myvim: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4328 | error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4329 | continue |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4330 | extra = {"params": (net['vim_net_id'], net['sdn_net_id'])} |
| 4331 | if net2vm_dependencies.get(net["uuid"]): |
| 4332 | extra["depends_on"] = net2vm_dependencies[net["uuid"]] |
| 4333 | sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"] |
| 4334 | if len(sfi_dependencies) > 0: |
| 4335 | if "depends_on" in extra: |
| 4336 | extra["depends_on"] += sfi_dependencies |
| 4337 | else: |
| 4338 | extra["depends_on"] = sfi_dependencies |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4339 | db_vim_action = { |
| 4340 | "instance_action_id": instance_action_id, |
| 4341 | "task_index": task_index, |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4342 | "datacenter_vim_id": net["datacenter_tenant_id"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4343 | "action": "DELETE", |
| 4344 | "status": "SCHEDULED", |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4345 | "item": "instance_nets", |
| 4346 | "item_id": net["uuid"], |
| tierno | 3c44e7b | 2019-03-04 17:32:01 +0000 | [diff] [blame] | 4347 | "related": net["related"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4348 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4349 | } |
| 4350 | task_index += 1 |
| 4351 | db_vim_actions.append(db_vim_action) |
| 4352 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4353 | db_instance_action["number_tasks"] = task_index |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4354 | |
| 4355 | # --> WIM |
| 4356 | wim_actions, db_instance_action = ( |
| 4357 | wim_engine.incorporate_actions(wim_actions, db_instance_action)) |
| 4358 | # <-- WIM |
| 4359 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4360 | db_tables = [ |
| 4361 | {"instance_actions": db_instance_action}, |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4362 | {"vim_wim_actions": db_vim_actions + wim_actions} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4363 | ] |
| 4364 | |
| 4365 | logger.debug("delete_instance done DB tables: %s", |
| 4366 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4367 | mydb.new_rows(db_tables, ()) |
| 4368 | for myvim_thread_id in vimthread_affected.keys(): |
| 4369 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| 4370 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4371 | wim_engine.dispatch(wim_actions) |
| 4372 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4373 | if len(error_msg) > 0: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4374 | return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\ |
| 4375 | '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4376 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4377 | return "action_id={} instance {} deleted".format(instance_action_id, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4378 | |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 4379 | def get_instance_id(mydb, tenant_id, instance_id): |
| 4380 | global ovim |
| 4381 | #check valid tenant_id |
| 4382 | check_tenant(mydb, tenant_id) |
| 4383 | #obtain data |
| 4384 | |
| 4385 | instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True) |
| 4386 | for net in instance_dict["nets"]: |
| 4387 | if net.get("sdn_net_id"): |
| 4388 | net_sdn = ovim.show_network(net["sdn_net_id"]) |
| 4389 | net["sdn_info"] = { |
| 4390 | "admin_state_up": net_sdn.get("admin_state_up"), |
| 4391 | "flows": net_sdn.get("flows"), |
| 4392 | "last_error": net_sdn.get("last_error"), |
| 4393 | "ports": net_sdn.get("ports"), |
| 4394 | "type": net_sdn.get("type"), |
| 4395 | "status": net_sdn.get("status"), |
| 4396 | "vlan": net_sdn.get("vlan"), |
| 4397 | } |
| 4398 | return instance_dict |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4399 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 4400 | @deprecated("Instance is automatically refreshed by vim_threads") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4401 | def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None): |
| 4402 | '''Refreshes a scenario instance. It modifies instanceDict''' |
| 4403 | '''Returns: |
| 4404 | - 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 |
| 4405 | - error_msg |
| 4406 | ''' |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4407 | # # Assumption: nfvo_tenant and instance_id were checked before entering into this function |
| 4408 | # #print "nfvo.refresh_instance begins" |
| 4409 | # #print json.dumps(instanceDict, indent=4) |
| 4410 | # |
| 4411 | # #print "Getting the VIM URL and the VIM tenant_id" |
| 4412 | # myvims={} |
| 4413 | # |
| 4414 | # # 1. Getting VIM vm and net list |
| 4415 | # vms_updated = [] #List of VM instance uuids in openmano that were updated |
| 4416 | # vms_notupdated=[] |
| 4417 | # vm_list = {} |
| 4418 | # for sce_vnf in instanceDict['vnfs']: |
| 4419 | # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4420 | # if datacenter_key not in vm_list: |
| 4421 | # vm_list[datacenter_key] = [] |
| 4422 | # if datacenter_key not in myvims: |
| 4423 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"], |
| 4424 | # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 4425 | # if len(vims) == 0: |
| 4426 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])) |
| 4427 | # myvims[datacenter_key] = None |
| 4428 | # else: |
| 4429 | # myvims[datacenter_key] = vims.values()[0] |
| 4430 | # for vm in sce_vnf['vms']: |
| 4431 | # vm_list[datacenter_key].append(vm['vim_vm_id']) |
| 4432 | # vms_notupdated.append(vm["uuid"]) |
| 4433 | # |
| 4434 | # nets_updated = [] #List of VM instance uuids in openmano that were updated |
| 4435 | # nets_notupdated=[] |
| 4436 | # net_list = {} |
| 4437 | # for net in instanceDict['nets']: |
| 4438 | # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4439 | # if datacenter_key not in net_list: |
| 4440 | # net_list[datacenter_key] = [] |
| 4441 | # if datacenter_key not in myvims: |
| 4442 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"], |
| 4443 | # datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 4444 | # if len(vims) == 0: |
| 4445 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 4446 | # myvims[datacenter_key] = None |
| 4447 | # else: |
| 4448 | # myvims[datacenter_key] = vims.values()[0] |
| 4449 | # |
| 4450 | # net_list[datacenter_key].append(net['vim_net_id']) |
| 4451 | # nets_notupdated.append(net["uuid"]) |
| 4452 | # |
| 4453 | # # 1. Getting the status of all VMs |
| 4454 | # vm_dict={} |
| 4455 | # for datacenter_key in myvims: |
| 4456 | # if not vm_list.get(datacenter_key): |
| 4457 | # continue |
| 4458 | # failed = True |
| 4459 | # failed_message="" |
| 4460 | # if not myvims[datacenter_key]: |
| 4461 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4462 | # else: |
| 4463 | # try: |
| 4464 | # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) ) |
| 4465 | # failed = False |
| 4466 | # except vimconn.vimconnException as e: |
| 4467 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4468 | # failed_message = str(e) |
| 4469 | # if failed: |
| 4470 | # for vm in vm_list[datacenter_key]: |
| 4471 | # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4472 | # |
| 4473 | # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed |
| 4474 | # for sce_vnf in instanceDict['vnfs']: |
| 4475 | # for vm in sce_vnf['vms']: |
| 4476 | # vm_id = vm['vim_vm_id'] |
| 4477 | # interfaces = vm_dict[vm_id].pop('interfaces', []) |
| 4478 | # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE |
| 4479 | # has_mgmt_iface = False |
| 4480 | # for iface in vm["interfaces"]: |
| 4481 | # if iface["type"]=="mgmt": |
| 4482 | # has_mgmt_iface = True |
| 4483 | # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface: |
| 4484 | # vm_dict[vm_id]['status'] = "ACTIVE" |
| 4485 | # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024: |
| 4486 | # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:] |
| 4487 | # 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'): |
| 4488 | # vm['status'] = vm_dict[vm_id]['status'] |
| 4489 | # vm['error_msg'] = vm_dict[vm_id].get('error_msg') |
| 4490 | # vm['vim_info'] = vm_dict[vm_id].get('vim_info') |
| 4491 | # # 2.1. Update in openmano DB the VMs whose status changed |
| 4492 | # try: |
| 4493 | # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]}) |
| 4494 | # vms_notupdated.remove(vm["uuid"]) |
| 4495 | # if updates>0: |
| 4496 | # vms_updated.append(vm["uuid"]) |
| 4497 | # except db_base_Exception as e: |
| 4498 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4499 | # # 2.2. Update in openmano DB the interface VMs |
| 4500 | # for interface in interfaces: |
| 4501 | # #translate from vim_net_id to instance_net_id |
| 4502 | # network_id_list=[] |
| 4503 | # for net in instanceDict['nets']: |
| 4504 | # if net["vim_net_id"] == interface["vim_net_id"]: |
| 4505 | # network_id_list.append(net["uuid"]) |
| 4506 | # if not network_id_list: |
| 4507 | # continue |
| 4508 | # del interface["vim_net_id"] |
| 4509 | # try: |
| 4510 | # for network_id in network_id_list: |
| 4511 | # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id}) |
| 4512 | # except db_base_Exception as e: |
| 4513 | # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id) |
| 4514 | # |
| 4515 | # # 3. Getting the status of all nets |
| 4516 | # net_dict = {} |
| 4517 | # for datacenter_key in myvims: |
| 4518 | # if not net_list.get(datacenter_key): |
| 4519 | # continue |
| 4520 | # failed = True |
| 4521 | # failed_message = "" |
| 4522 | # if not myvims[datacenter_key]: |
| 4523 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4524 | # else: |
| 4525 | # try: |
| 4526 | # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) ) |
| 4527 | # failed = False |
| 4528 | # except vimconn.vimconnException as e: |
| 4529 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4530 | # failed_message = str(e) |
| 4531 | # if failed: |
| 4532 | # for net in net_list[datacenter_key]: |
| 4533 | # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4534 | # |
| 4535 | # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed |
| 4536 | # # TODO: update nets inside a vnf |
| 4537 | # for net in instanceDict['nets']: |
| 4538 | # net_id = net['vim_net_id'] |
| 4539 | # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024: |
| 4540 | # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:] |
| 4541 | # 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'): |
| 4542 | # net['status'] = net_dict[net_id]['status'] |
| 4543 | # net['error_msg'] = net_dict[net_id].get('error_msg') |
| 4544 | # net['vim_info'] = net_dict[net_id].get('vim_info') |
| 4545 | # # 5.1. Update in openmano DB the nets whose status changed |
| 4546 | # try: |
| 4547 | # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]}) |
| 4548 | # nets_notupdated.remove(net["uuid"]) |
| 4549 | # if updated>0: |
| 4550 | # nets_updated.append(net["uuid"]) |
| 4551 | # except db_base_Exception as e: |
| 4552 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4553 | # |
| 4554 | # # Returns appropriate output |
| 4555 | # #print "nfvo.refresh_instance finishes" |
| 4556 | # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s", |
| 4557 | # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4558 | instance_id = instanceDict['uuid'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4559 | # if len(vms_notupdated)+len(nets_notupdated)>0: |
| 4560 | # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated) |
| 4561 | # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4562 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4563 | return 0, 'Scenario instance ' + instance_id + ' refreshed.' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4564 | |
| 4565 | def instance_action(mydb,nfvo_tenant,instance_id, action_dict): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4566 | #print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4567 | instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4568 | #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| 4569 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4570 | #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4571 | vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id']) |
| 4572 | if len(vims) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4573 | raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4574 | myvim = vims.values()[0] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4575 | vm_result = {} |
| 4576 | vm_error = 0 |
| 4577 | vm_ok = 0 |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4578 | |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4579 | myvim_threads_id = {} |
| 4580 | if action_dict.get("vdu-scaling"): |
| 4581 | db_instance_vms = [] |
| 4582 | db_vim_actions = [] |
| 4583 | db_instance_interfaces = [] |
| 4584 | instance_action_id = get_task_id() |
| 4585 | db_instance_action = { |
| 4586 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 4587 | "tenant_id": nfvo_tenant, |
| 4588 | "instance_id": instance_id, |
| 4589 | "description": "SCALE", |
| 4590 | } |
| 4591 | vm_result["instance_action_id"] = instance_action_id |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4592 | vm_result["created"] = [] |
| 4593 | vm_result["deleted"] = [] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4594 | task_index = 0 |
| 4595 | for vdu in action_dict["vdu-scaling"]: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4596 | vdu_id = vdu.get("vdu-id") |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4597 | osm_vdu_id = vdu.get("osm_vdu_id") |
| 4598 | member_vnf_index = vdu.get("member-vnf-index") |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4599 | vdu_count = vdu.get("count", 1) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4600 | if vdu_id: |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4601 | target_vms = mydb.get_rows( |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4602 | FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid", |
| 4603 | WHERE={"vms.uuid": vdu_id}, |
| 4604 | ORDER_BY="vms.created_at" |
| 4605 | ) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4606 | if not target_vms: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4607 | raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), httperrors.Not_Found) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4608 | else: |
| 4609 | if not osm_vdu_id and not member_vnf_index: |
| tierno | a43bd9e | 2018-11-26 09:28:58 +0000 | [diff] [blame] | 4610 | raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'") |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4611 | target_vms = mydb.get_rows( |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4612 | # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"), |
| 4613 | FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\ |
| 4614 | " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\ |
| 4615 | " join vms on ivms.vm_id=vms.uuid", |
| tierno | a43bd9e | 2018-11-26 09:28:58 +0000 | [diff] [blame] | 4616 | WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index, |
| 4617 | "ivnfs.instance_scenario_id": instance_id}, |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4618 | ORDER_BY="ivms.created_at" |
| 4619 | ) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4620 | if not target_vms: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4621 | raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), httperrors.Not_Found) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4622 | vdu_id = target_vms[-1]["uuid"] |
| 4623 | target_vm = target_vms[-1] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4624 | datacenter = target_vm["datacenter_id"] |
| 4625 | myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4626 | |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4627 | if vdu["type"] == "delete": |
| 4628 | for index in range(0, vdu_count): |
| 4629 | target_vm = target_vms[-1-index] |
| 4630 | vdu_id = target_vm["uuid"] |
| 4631 | # look for nm |
| 4632 | vm_interfaces = None |
| 4633 | for sce_vnf in instanceDict['vnfs']: |
| 4634 | for vm in sce_vnf['vms']: |
| 4635 | if vm["uuid"] == vdu_id: |
| tierno | b5091bd | 2019-05-22 16:45:09 +0000 | [diff] [blame] | 4636 | # TODO revise this should not be vm["uuid"] instance_vms["vm_id"] |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4637 | vm_interfaces = vm["interfaces"] |
| 4638 | break |
| 4639 | |
| 4640 | db_vim_action = { |
| 4641 | "instance_action_id": instance_action_id, |
| 4642 | "task_index": task_index, |
| 4643 | "datacenter_vim_id": target_vm["datacenter_tenant_id"], |
| 4644 | "action": "DELETE", |
| 4645 | "status": "SCHEDULED", |
| 4646 | "item": "instance_vms", |
| 4647 | "item_id": vdu_id, |
| tierno | b5091bd | 2019-05-22 16:45:09 +0000 | [diff] [blame] | 4648 | "related": target_vm["related"], |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4649 | "extra": yaml.safe_dump({"params": vm_interfaces}, |
| 4650 | default_flow_style=True, width=256) |
| 4651 | } |
| 4652 | task_index += 1 |
| 4653 | db_vim_actions.append(db_vim_action) |
| 4654 | vm_result["deleted"].append(vdu_id) |
| 4655 | # delete from database |
| 4656 | db_instance_vms.append({"TO-DELETE": vdu_id}) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4657 | |
| 4658 | else: # vdu["type"] == "create": |
| 4659 | iface2iface = {} |
| 4660 | where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"} |
| 4661 | |
| garciadeblas | 72cd59f | 2018-12-05 10:59:40 +0100 | [diff] [blame] | 4662 | vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4663 | if not vim_action_to_clone: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4664 | raise NfvoException("Cannot find the vim_action at database with {}".format(where), httperrors.Internal_Server_Error) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4665 | vim_action_to_clone = vim_action_to_clone[0] |
| 4666 | extra = yaml.safe_load(vim_action_to_clone["extra"]) |
| 4667 | |
| 4668 | # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y |
| 4669 | # TODO do the same for flavor and image when available |
| 4670 | task_depends_on = [] |
| 4671 | task_params = extra["params"] |
| 4672 | task_params_networks = deepcopy(task_params[5]) |
| 4673 | for iface in task_params[5]: |
| 4674 | if iface["net_id"].startswith("TASK-"): |
| 4675 | if "." not in iface["net_id"]: |
| 4676 | task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4677 | iface["net_id"][5:])) |
| 4678 | iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4679 | iface["net_id"][5:]) |
| 4680 | else: |
| 4681 | task_depends_on.append(iface["net_id"][5:]) |
| 4682 | if "mac_address" in iface: |
| 4683 | del iface["mac_address"] |
| 4684 | |
| 4685 | vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]}) |
| 4686 | for index in range(0, vdu_count): |
| 4687 | vm_uuid = str(uuid4()) |
| 4688 | vm_name = target_vm.get('vim_name') |
| 4689 | try: |
| 4690 | suffix = vm_name.rfind("-") |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4691 | vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:])) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4692 | except Exception: |
| 4693 | pass |
| 4694 | db_instance_vm = { |
| 4695 | "uuid": vm_uuid, |
| tierno | b5091bd | 2019-05-22 16:45:09 +0000 | [diff] [blame] | 4696 | 'related': vm_uuid, |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4697 | 'instance_vnf_id': target_vm['instance_vnf_id'], |
| 4698 | 'vm_id': target_vm['vm_id'], |
| tierno | b5091bd | 2019-05-22 16:45:09 +0000 | [diff] [blame] | 4699 | 'vim_name': vm_name, |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4700 | } |
| 4701 | db_instance_vms.append(db_instance_vm) |
| 4702 | |
| 4703 | for vm_iface in vm_ifaces_to_clone: |
| 4704 | iface_uuid = str(uuid4()) |
| 4705 | iface2iface[vm_iface["uuid"]] = iface_uuid |
| 4706 | db_vm_iface = { |
| 4707 | "uuid": iface_uuid, |
| 4708 | 'instance_vm_id': vm_uuid, |
| 4709 | "instance_net_id": vm_iface["instance_net_id"], |
| 4710 | 'interface_id': vm_iface['interface_id'], |
| 4711 | 'type': vm_iface['type'], |
| 4712 | 'floating_ip': vm_iface['floating_ip'], |
| 4713 | 'port_security': vm_iface['port_security'] |
| 4714 | } |
| 4715 | db_instance_interfaces.append(db_vm_iface) |
| 4716 | task_params_copy = deepcopy(task_params) |
| 4717 | for iface in task_params_copy[5]: |
| 4718 | iface["uuid"] = iface2iface[iface["uuid"]] |
| 4719 | # increment ip_address |
| 4720 | if "ip_address" in iface: |
| 4721 | ip = iface.get("ip_address") |
| 4722 | i = ip.rfind(".") |
| 4723 | if i > 0: |
| 4724 | try: |
| 4725 | i += 1 |
| 4726 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 4727 | iface["ip_address"] = ip |
| 4728 | except: |
| 4729 | iface["ip_address"] = None |
| 4730 | if vm_name: |
| 4731 | task_params_copy[0] = vm_name |
| 4732 | db_vim_action = { |
| 4733 | "instance_action_id": instance_action_id, |
| 4734 | "task_index": task_index, |
| 4735 | "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"], |
| 4736 | "action": "CREATE", |
| 4737 | "status": "SCHEDULED", |
| 4738 | "item": "instance_vms", |
| 4739 | "item_id": vm_uuid, |
| tierno | b5091bd | 2019-05-22 16:45:09 +0000 | [diff] [blame] | 4740 | "related": vm_uuid, |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4741 | # ALF |
| 4742 | # ALF |
| 4743 | # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd. |
| 4744 | # ALF |
| 4745 | # ALF |
| 4746 | "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256) |
| 4747 | } |
| 4748 | task_index += 1 |
| 4749 | db_vim_actions.append(db_vim_action) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4750 | vm_result["created"].append(vm_uuid) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4751 | |
| 4752 | db_instance_action["number_tasks"] = task_index |
| 4753 | db_tables = [ |
| 4754 | {"instance_vms": db_instance_vms}, |
| 4755 | {"instance_interfaces": db_instance_interfaces}, |
| 4756 | {"instance_actions": db_instance_action}, |
| 4757 | # TODO revise sfps |
| 4758 | # {"instance_sfis": db_instance_sfis}, |
| 4759 | # {"instance_sfs": db_instance_sfs}, |
| 4760 | # {"instance_classifications": db_instance_classifications}, |
| 4761 | # {"instance_sfps": db_instance_sfps}, |
| garciadeblas | aba7a0d | 2018-12-05 12:42:35 +0100 | [diff] [blame] | 4762 | {"vim_wim_actions": db_vim_actions} |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4763 | ] |
| 4764 | logger.debug("create_vdu done DB tables: %s", |
| 4765 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4766 | mydb.new_rows(db_tables, []) |
| 4767 | for myvim_thread in myvim_threads_id.values(): |
| 4768 | vim_threads["running"][myvim_thread].insert_task(db_vim_actions) |
| 4769 | |
| 4770 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4771 | |
| 4772 | input_vnfs = action_dict.pop("vnfs", []) |
| 4773 | input_vms = action_dict.pop("vms", []) |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4774 | action_over_all = True if not input_vnfs and not input_vms else False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4775 | for sce_vnf in instanceDict['vnfs']: |
| 4776 | for vm in sce_vnf['vms']: |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4777 | if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \ |
| 4778 | sce_vnf['member_vnf_index'] not in input_vnfs and \ |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 4779 | vm['uuid'] not in input_vms and vm['name'] not in input_vms and \ |
| 4780 | sce_vnf['member_vnf_index'] + "-" + vm['vdu_osm_id'] not in input_vms: # TODO conside vm_count_index |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4781 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4782 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4783 | if "add_public_key" in action_dict: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4784 | if sce_vnf.get('mgmt_access'): |
| 4785 | mgmt_access = yaml.load(sce_vnf['mgmt_access']) |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 4786 | if not input_vms and mgmt_access.get("vdu-id") != vm['vdu_osm_id']: |
| 4787 | continue |
| 4788 | default_user = mgmt_access.get("default-user") |
| 4789 | password = mgmt_access.get("password") |
| 4790 | if mgmt_access.get(vm['vdu_osm_id']): |
| 4791 | default_user = mgmt_access[vm['vdu_osm_id']].get("default-user", default_user) |
| 4792 | password = mgmt_access[vm['vdu_osm_id']].get("password", password) |
| 4793 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4794 | tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4795 | try: |
| tierno | 7e51005 | 2019-09-10 16:16:13 +0000 | [diff] [blame] | 4796 | if 'ip_address' in vm: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4797 | mgmt_ip = vm['ip_address'].split(';') |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4798 | priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid']) |
| calvinosanch | 5db670b | 2019-10-13 15:52:33 +0200 | [diff] [blame] | 4799 | data = myvim.inject_user_key(mgmt_ip[0], action_dict.get('user', default_user), |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4800 | action_dict['add_public_key'], |
| 4801 | password=password, ro_key=priv_RO_key) |
| calvinosanch | 5db670b | 2019-10-13 15:52:33 +0200 | [diff] [blame] | 4802 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4803 | "description": "Public key injected", |
| 4804 | "name":vm['name'] |
| 4805 | } |
| 4806 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4807 | except KeyError: |
| 4808 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4809 | httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4810 | else: |
| 4811 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4812 | httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4813 | else: |
| 4814 | data = myvim.action_vminstance(vm['vim_vm_id'], action_dict) |
| 4815 | if "console" in action_dict: |
| 4816 | if not global_config["http_console_proxy"]: |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4817 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4818 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4819 | protocol=data["protocol"], |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4820 | ip = data["server"], |
| 4821 | port = data["port"], |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4822 | suffix = data["suffix"]), |
| 4823 | "name":vm['name'] |
| 4824 | } |
| 4825 | vm_ok +=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4826 | elif data["server"]=="127.0.0.1" or data["server"]=="localhost": |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4827 | vm_result[ vm['uuid'] ] = {"vim_result": -httperrors.Unauthorized, |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4828 | "description": "this console is only reachable by local interface", |
| 4829 | "name":vm['name'] |
| 4830 | } |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4831 | vm_error+=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4832 | else: |
| 4833 | #print "console data", data |
| 4834 | try: |
| 4835 | console_thread = create_or_use_console_proxy_thread(data["server"], data["port"]) |
| 4836 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4837 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4838 | protocol=data["protocol"], |
| 4839 | ip = global_config["http_console_host"], |
| 4840 | port = console_thread.port, |
| 4841 | suffix = data["suffix"]), |
| 4842 | "name":vm['name'] |
| 4843 | } |
| 4844 | vm_ok +=1 |
| 4845 | except NfvoException as e: |
| 4846 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4847 | vm_error+=1 |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4848 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4849 | else: |
| 4850 | vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']} |
| 4851 | vm_ok +=1 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4852 | except vimconn.vimconnException as e: |
| 4853 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4854 | vm_error+=1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4855 | |
| 4856 | if vm_ok==0: #all goes wrong |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4857 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4858 | else: |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4859 | return vm_result |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4860 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4861 | def instance_action_get(mydb, nfvo_tenant, instance_id, action_id): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4862 | filter = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4863 | if nfvo_tenant and nfvo_tenant != "any": |
| 4864 | filter["tenant_id"] = nfvo_tenant |
| 4865 | if instance_id and instance_id != "any": |
| 4866 | filter["instance_id"] = instance_id |
| 4867 | if action_id: |
| 4868 | filter["uuid"] = action_id |
| 4869 | rows = mydb.get_rows(FROM="instance_actions", WHERE=filter) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4870 | if action_id: |
| 4871 | if not rows: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4872 | raise NfvoException("Not found any action with this criteria", httperrors.Not_Found) |
| 4873 | vim_wim_actions = mydb.get_rows(FROM="vim_wim_actions", WHERE={"instance_action_id": action_id}) |
| 4874 | rows[0]["vim_wim_actions"] = vim_wim_actions |
| tierno | 31e121f | 2018-12-03 12:04:48 +0000 | [diff] [blame] | 4875 | # for backward compatibility set vim_actions = vim_wim_actions |
| 4876 | rows[0]["vim_actions"] = vim_wim_actions |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4877 | return {"actions": rows} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4878 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4879 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4880 | def create_or_use_console_proxy_thread(console_server, console_port): |
| 4881 | #look for a non-used port |
| 4882 | console_thread_key = console_server + ":" + str(console_port) |
| 4883 | if console_thread_key in global_config["console_thread"]: |
| 4884 | #global_config["console_thread"][console_thread_key].start_timeout() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4885 | return global_config["console_thread"][console_thread_key] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4886 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4887 | for port in global_config["console_port_iterator"](): |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4888 | #print "create_or_use_console_proxy_thread() port:", port |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4889 | if port in global_config["console_ports"]: |
| 4890 | continue |
| 4891 | try: |
| 4892 | clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port) |
| 4893 | clithread.start() |
| 4894 | global_config["console_thread"][console_thread_key] = clithread |
| 4895 | global_config["console_ports"][port] = console_thread_key |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4896 | return clithread |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4897 | except cli.ConsoleProxyExceptionPortUsed as e: |
| 4898 | #port used, try with onoher |
| 4899 | continue |
| 4900 | except cli.ConsoleProxyException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4901 | raise NfvoException(str(e), httperrors.Bad_Request) |
| 4902 | raise NfvoException("Not found any free 'http_console_ports'", httperrors.Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4903 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4904 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4905 | def check_tenant(mydb, tenant_id): |
| 4906 | '''check that tenant exists at database''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4907 | tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id}) |
| 4908 | if not tenant: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4909 | raise NfvoException("tenant '{}' not found".format(tenant_id), httperrors.Not_Found) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4910 | return |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4911 | |
| 4912 | def new_tenant(mydb, tenant_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4913 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4914 | tenant_uuid = str(uuid4()) |
| 4915 | tenant_dict['uuid'] = tenant_uuid |
| 4916 | try: |
| 4917 | pub_key, priv_key = create_RO_keypair(tenant_uuid) |
| 4918 | tenant_dict['RO_pub_key'] = pub_key |
| 4919 | tenant_dict['encrypted_RO_priv_key'] = priv_key |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4920 | mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4921 | except db_base_Exception as e: |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 4922 | raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), e.http_code) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4923 | return tenant_uuid |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4924 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4925 | def delete_tenant(mydb, tenant): |
| 4926 | #get nfvo_tenant info |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4927 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4928 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant') |
| 4929 | mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid']) |
| 4930 | return tenant_dict['uuid'] + " " + tenant_dict["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4931 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4932 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4933 | def new_datacenter(mydb, datacenter_descriptor): |
| tierno | 1c848c0 | 2018-05-21 16:40:33 +0200 | [diff] [blame] | 4934 | sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4935 | if "config" in datacenter_descriptor: |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4936 | sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None) |
| 4937 | datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True, |
| 4938 | width=256) |
| 4939 | # Check that datacenter-type is correct |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4940 | datacenter_type = datacenter_descriptor.get("type", "openvim"); |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4941 | # module_info = None |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4942 | try: |
| 4943 | module = "vimconn_" + datacenter_type |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4944 | pkg = __import__("osm_ro." + module) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4945 | # vim_conn = getattr(pkg, module) |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4946 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4947 | except (IOError, ImportError): |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4948 | # if module_info and module_info[0]: |
| 4949 | # file.close(module_info[0]) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4950 | raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type, |
| 4951 | module), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4952 | httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4953 | |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4954 | datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4955 | if sdn_port_mapping: |
| 4956 | try: |
| 4957 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping) |
| 4958 | except Exception as e: |
| 4959 | mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback |
| 4960 | raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4961 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4962 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4963 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4964 | def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4965 | # obtain data, check that only one exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4966 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4967 | |
| 4968 | # edit data |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4969 | datacenter_id = datacenter['uuid'] |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4970 | where = {'uuid': datacenter['uuid']} |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4971 | remove_port_mapping = False |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4972 | new_sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4973 | if "config" in datacenter_descriptor: |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4974 | if datacenter_descriptor['config'] != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4975 | try: |
| 4976 | new_config_dict = datacenter_descriptor["config"] |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4977 | if "sdn-port-mapping" in new_config_dict: |
| 4978 | remove_port_mapping = True |
| 4979 | new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping") |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4980 | # delete null fields |
| 4981 | to_delete = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4982 | for k in new_config_dict: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4983 | if new_config_dict[k] is None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4984 | to_delete.append(k) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4985 | if k == 'sdn-controller': |
| 4986 | remove_port_mapping = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4987 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4988 | config_text = datacenter.get("config") |
| 4989 | if not config_text: |
| 4990 | config_text = '{}' |
| 4991 | config_dict = yaml.load(config_text) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4992 | config_dict.update(new_config_dict) |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4993 | # delete null fields |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4994 | for k in to_delete: |
| 4995 | del config_dict[k] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4996 | except Exception as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4997 | raise NfvoException("Bad format at datacenter:config " + str(e), httperrors.Bad_Request) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4998 | if config_dict: |
| 4999 | datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256) |
| 5000 | else: |
| 5001 | datacenter_descriptor["config"] = None |
| 5002 | if remove_port_mapping: |
| 5003 | try: |
| 5004 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id) |
| 5005 | except ovimException as e: |
| Anderson Bravalheri | c5293de | 2018-11-28 17:21:26 +0000 | [diff] [blame] | 5006 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e), httperrors.Conflict) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 5007 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5008 | mydb.update_rows('datacenters', datacenter_descriptor, where) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 5009 | if new_sdn_port_mapping: |
| 5010 | try: |
| 5011 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping) |
| 5012 | except ovimException as e: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 5013 | # Rollback |
| 5014 | mydb.update_rows('datacenters', datacenter, where) |
| Anderson Bravalheri | c5293de | 2018-11-28 17:21:26 +0000 | [diff] [blame] | 5015 | raise NfvoException("Error adding datacenter-port-mapping " + str(e), httperrors.Conflict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5016 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5017 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5018 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5019 | def delete_datacenter(mydb, datacenter): |
| 5020 | #get nfvo_tenant info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5021 | datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter') |
| 5022 | mydb.delete_row_by_id("datacenters", datacenter_dict['uuid']) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 5023 | try: |
| 5024 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid']) |
| 5025 | except ovimException as e: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 5026 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5027 | return datacenter_dict['uuid'] + " " + datacenter_dict['name'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5028 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5029 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5030 | def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None, |
| 5031 | vim_username=None, vim_password=None, config=None): |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 5032 | # get datacenter info |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5033 | try: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5034 | if not datacenter_id: |
| 5035 | if not vim_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5036 | raise NfvoException("You must provide 'vim_id", http_code=httperrors.Bad_Request) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5037 | datacenter_id = vim_id |
| 5038 | datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5039 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5040 | create_vim_tenant = True if not vim_tenant and not vim_tenant_name else False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5041 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5042 | # get nfvo_tenant info |
| 5043 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant) |
| 5044 | if vim_tenant_name==None: |
| 5045 | vim_tenant_name=tenant_dict['name'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5046 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5047 | tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id } |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5048 | # #check that this association does not exist before |
| 5049 | # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 5050 | # if len(tenants_datacenters)>0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5051 | # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5052 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5053 | vim_tenant_id_exist_atdb=False |
| 5054 | if not create_vim_tenant: |
| 5055 | where_={"datacenter_id": datacenter_id} |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5056 | if vim_tenant!=None: |
| 5057 | where_["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5058 | if vim_tenant_name!=None: |
| 5059 | where_["vim_tenant_name"] = vim_tenant_name |
| 5060 | #check if vim_tenant_id is already at database |
| 5061 | datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_) |
| 5062 | if len(datacenter_tenants_dict)>=1: |
| 5063 | datacenter_tenants_dict = datacenter_tenants_dict[0] |
| 5064 | vim_tenant_id_exist_atdb=True |
| 5065 | #TODO check if a field has changed and edit entry at datacenter_tenants at DB |
| 5066 | else: #result=0 |
| 5067 | datacenter_tenants_dict = {} |
| 5068 | #insert at table datacenter_tenants |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5069 | else: #if vim_tenant==None: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5070 | #create tenant at VIM if not provided |
| 5071 | try: |
| 5072 | _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, |
| 5073 | vim_passwd=vim_password) |
| 5074 | datacenter_name = myvim["name"] |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5075 | vim_tenant = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name) |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5076 | except vimconn.vimconnException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5077 | raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), httperrors.Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5078 | datacenter_tenants_dict = {} |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5079 | datacenter_tenants_dict["created"]="true" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5080 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5081 | #fill datacenter_tenants table |
| 5082 | if not vim_tenant_id_exist_atdb: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5083 | datacenter_tenants_dict["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5084 | datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name |
| 5085 | datacenter_tenants_dict["user"] = vim_username |
| 5086 | datacenter_tenants_dict["passwd"] = vim_password |
| 5087 | datacenter_tenants_dict["datacenter_id"] = datacenter_id |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5088 | if name: |
| 5089 | datacenter_tenants_dict["name"] = name |
| 5090 | else: |
| 5091 | datacenter_tenants_dict["name"] = datacenter_name |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5092 | if config: |
| 5093 | datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256) |
| 5094 | id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True) |
| 5095 | datacenter_tenants_dict["uuid"] = id_ |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5096 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5097 | #fill tenants_datacenters table |
| 5098 | datacenter_tenant_id = datacenter_tenants_dict["uuid"] |
| 5099 | tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id |
| 5100 | mydb.new_row('tenants_datacenters', tenants_datacenter_dict) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5101 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5102 | # create thread |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5103 | thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid']) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5104 | new_thread = vim_thread.vim_thread(task_lock, thread_name, datacenter_name, datacenter_tenant_id, |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5105 | db=db, db_lock=db_lock, ovim=ovim) |
| 5106 | new_thread.start() |
| 5107 | thread_id = datacenter_tenants_dict["uuid"] |
| 5108 | vim_threads["running"][thread_id] = new_thread |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5109 | return thread_id |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5110 | except vimconn.vimconnException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5111 | raise NfvoException(str(e), httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5112 | |
| tierno | 9931490 | 2017-04-26 13:23:09 +0200 | [diff] [blame] | 5113 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5114 | def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None, |
| 5115 | vim_tenant_name=None, vim_username=None, vim_password=None, config=None): |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5116 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5117 | # get vim_account; check is valid for this tenant |
| 5118 | from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id" |
| 5119 | where_ = {"td.nfvo_tenant_id": nfvo_tenant} |
| 5120 | if datacenter_tenant_id: |
| 5121 | where_["dt.uuid"] = datacenter_tenant_id |
| 5122 | if datacenter_id: |
| 5123 | where_["dt.datacenter_id"] = datacenter_id |
| 5124 | vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_) |
| 5125 | if not vim_accounts: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5126 | raise NfvoException("vim_account not found for this tenant", http_code=httperrors.Not_Found) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5127 | elif len(vim_accounts) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5128 | raise NfvoException("found more than one vim_account for this tenant", http_code=httperrors.Conflict) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5129 | datacenter_tenant_id = vim_accounts[0]["uuid"] |
| 5130 | original_config = vim_accounts[0]["config"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5131 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5132 | update_ = {} |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5133 | if config: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5134 | original_config_dict = yaml.load(original_config) |
| 5135 | original_config_dict.update(config) |
| 5136 | update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256) |
| 5137 | if name: |
| 5138 | update_['name'] = name |
| 5139 | if vim_tenant: |
| 5140 | update_['vim_tenant_id'] = vim_tenant |
| 5141 | if vim_tenant_name: |
| 5142 | update_['vim_tenant_name'] = vim_tenant_name |
| 5143 | if vim_username: |
| 5144 | update_['user'] = vim_username |
| 5145 | if vim_password: |
| 5146 | update_['passwd'] = vim_password |
| 5147 | if update_: |
| 5148 | mydb.update_rows("datacenter_tenants", UPDATE=update_, WHERE={"uuid": datacenter_tenant_id}) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5149 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5150 | vim_threads["running"][datacenter_tenant_id].insert_task("reload") |
| 5151 | return datacenter_tenant_id |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5152 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5153 | def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5154 | #get nfvo_tenant info |
| 5155 | if not tenant_id or tenant_id=="any": |
| 5156 | tenant_uuid = None |
| 5157 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5158 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5159 | tenant_uuid = tenant_dict['uuid'] |
| 5160 | |
| 5161 | #check that this association exist before |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5162 | tenants_datacenter_dict = {} |
| 5163 | if datacenter: |
| 5164 | datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter) |
| 5165 | tenants_datacenter_dict["datacenter_id"] = datacenter_id |
| 5166 | elif vim_account_id: |
| 5167 | tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5168 | if tenant_uuid: |
| 5169 | tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5170 | tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 5171 | if len(tenant_datacenter_list)==0 and tenant_uuid: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5172 | raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5173 | |
| 5174 | #delete this association |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5175 | mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5176 | |
| 5177 | #get vim_tenant info and deletes |
| 5178 | warning='' |
| 5179 | for tenant_datacenter_item in tenant_datacenter_list: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5180 | vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 5181 | #try to delete vim:tenant |
| 5182 | try: |
| 5183 | mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 5184 | if vim_tenant_dict['created']=='true': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5185 | #delete tenant at VIM if created by NFVO |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5186 | try: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5187 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5188 | myvim.delete_tenant(vim_tenant_dict['vim_tenant_id']) |
| 5189 | except vimconn.vimconnException as e: |
| 5190 | warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e)) |
| 5191 | logger.warn(warning) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5192 | except db_base_Exception as e: |
| 5193 | logger.error("Cannot delete datacenter_tenants " + str(e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5194 | pass # the error will be caused because dependencies, vim_tenant can not be deleted |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 5195 | thread_id = tenant_datacenter_item["datacenter_tenant_id"] |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 5196 | thread = vim_threads["running"].get(thread_id) |
| 5197 | if thread: |
| 5198 | thread.insert_task("exit") |
| 5199 | vim_threads["deleting"][thread_id] = thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5200 | return "datacenter {} detached. {}".format(datacenter_id, warning) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5201 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5202 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5203 | def datacenter_action(mydb, tenant_id, datacenter, action_dict): |
| 5204 | #DEPRECATED |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5205 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5206 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5207 | |
| tierno | 5509c2e | 2019-07-04 16:23:20 +0000 | [diff] [blame] | 5208 | if 'check-connectivity' in action_dict: |
| 5209 | try: |
| 5210 | myvim.check_vim_connectivity() |
| 5211 | except vimconn.vimconnException as e: |
| 5212 | #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e)) |
| 5213 | raise NfvoException(str(e), e.http_code) |
| 5214 | elif 'net-update' in action_dict: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5215 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5216 | nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'}) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5217 | #print content |
| 5218 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5219 | #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e)) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5220 | raise NfvoException(str(e), httperrors.Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5221 | #update nets Change from VIM format to NFVO format |
| 5222 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5223 | for net in nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5224 | net_nfvo={'datacenter_id': datacenter_id} |
| 5225 | net_nfvo['name'] = net['name'] |
| 5226 | #net_nfvo['description']= net['name'] |
| 5227 | net_nfvo['vim_net_id'] = net['id'] |
| 5228 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 5229 | net_nfvo['shared'] = net['shared'] |
| 5230 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| 5231 | net_list.append(net_nfvo) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5232 | inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list) |
| 5233 | logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted) |
| 5234 | return inserted |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5235 | elif 'net-edit' in action_dict: |
| 5236 | net = action_dict['net-edit'].pop('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5237 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5238 | result = mydb.update_rows('datacenter_nets', action_dict['net-edit'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5239 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5240 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5241 | elif 'net-delete' in action_dict: |
| 5242 | net = action_dict['net-deelte'].get('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5243 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5244 | result = mydb.delete_row(FROM='datacenter_nets', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5245 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5246 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5247 | |
| 5248 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5249 | raise NfvoException("Unknown action " + str(action_dict), httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5250 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5251 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5252 | def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): |
| 5253 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5254 | datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5255 | |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5256 | what = 'uuid' if utils.check_valid_uuid(netmap) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5257 | result = mydb.update_rows('datacenter_nets', action_dict['netmap'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5258 | WHERE={'datacenter_id':datacenter_id, what: netmap}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5259 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5260 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5261 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5262 | def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): |
| 5263 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5264 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5265 | filter_dict={} |
| 5266 | if action_dict: |
| 5267 | action_dict = action_dict["netmap"] |
| 5268 | if 'vim_id' in action_dict: |
| 5269 | filter_dict["id"] = action_dict['vim_id'] |
| 5270 | if 'vim_name' in action_dict: |
| 5271 | filter_dict["name"] = action_dict['vim_name'] |
| 5272 | else: |
| 5273 | filter_dict["shared"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5274 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5275 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5276 | vim_nets = myvim.get_network_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5277 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5278 | #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e)) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5279 | raise NfvoException(str(e), httperrors.Internal_Server_Error) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5280 | if len(vim_nets)>1 and action_dict: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5281 | raise NfvoException("more than two networks found, specify with vim_id", httperrors.Conflict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5282 | elif len(vim_nets)==0: # and action_dict: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5283 | raise NfvoException("Not found a network at VIM with " + str(filter_dict), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5284 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5285 | for net in vim_nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5286 | net_nfvo={'datacenter_id': datacenter_id} |
| 5287 | if action_dict and "name" in action_dict: |
| 5288 | net_nfvo['name'] = action_dict['name'] |
| 5289 | else: |
| 5290 | net_nfvo['name'] = net['name'] |
| 5291 | #net_nfvo['description']= net['name'] |
| 5292 | net_nfvo['vim_net_id'] = net['id'] |
| 5293 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 5294 | net_nfvo['shared'] = net['shared'] |
| 5295 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5296 | try: |
| 5297 | net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5298 | net_nfvo["status"] = "OK" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5299 | net_nfvo["uuid"] = net_id |
| 5300 | except db_base_Exception as e: |
| 5301 | if action_dict: |
| 5302 | raise |
| 5303 | else: |
| 5304 | net_nfvo["status"] = "FAIL: " + str(e) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5305 | net_list.append(net_nfvo) |
| 5306 | return net_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5307 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5308 | def get_sdn_net_id(mydb, tenant_id, datacenter, network_id): |
| 5309 | # obtain all network data |
| 5310 | try: |
| 5311 | if utils.check_valid_uuid(network_id): |
| 5312 | filter_dict = {"id": network_id} |
| 5313 | else: |
| 5314 | filter_dict = {"name": network_id} |
| 5315 | |
| 5316 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 5317 | network = myvim.get_network_list(filter_dict=filter_dict) |
| 5318 | except vimconn.vimconnException as e: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 5319 | raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e)), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5320 | |
| 5321 | # ensure the network is defined |
| 5322 | if len(network) == 0: |
| 5323 | raise NfvoException("Network {} is not present in the system".format(network_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5324 | httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5325 | |
| 5326 | # ensure there is only one network with the provided name |
| 5327 | if len(network) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5328 | raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5329 | |
| 5330 | # ensure it is a dataplane network |
| 5331 | if network[0]['type'] != 'data': |
| 5332 | return None |
| 5333 | |
| 5334 | # ensure we use the id |
| 5335 | network_id = network[0]['id'] |
| 5336 | |
| 5337 | # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id |
| 5338 | # and with instance_scenario_id==NULL |
| 5339 | #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None} |
| 5340 | search_dict = {'vim_net_id': network_id} |
| 5341 | |
| 5342 | try: |
| 5343 | #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id'] |
| 5344 | result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict) |
| 5345 | except db_base_Exception as e: |
| 5346 | raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5347 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5348 | |
| 5349 | sdn_net_counter = 0 |
| 5350 | for net in result: |
| 5351 | if net['sdn_net_id'] != None: |
| 5352 | sdn_net_counter+=1 |
| 5353 | sdn_net_id = net['sdn_net_id'] |
| 5354 | |
| 5355 | if sdn_net_counter == 0: |
| 5356 | return None |
| 5357 | elif sdn_net_counter == 1: |
| 5358 | return sdn_net_id |
| 5359 | else: |
| 5360 | raise NfvoException("More than one SDN network is associated to vim network {}".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5361 | network_id), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5362 | |
| 5363 | def get_sdn_controller_id(mydb, datacenter): |
| 5364 | # Obtain sdn controller id |
| 5365 | config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}') |
| 5366 | if not config: |
| 5367 | return None |
| 5368 | |
| 5369 | return yaml.load(config).get('sdn-controller') |
| 5370 | |
| 5371 | def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor): |
| 5372 | try: |
| 5373 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5374 | if not sdn_network_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5375 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5376 | |
| 5377 | #Obtain sdn controller id |
| 5378 | controller_id = get_sdn_controller_id(mydb, datacenter) |
| 5379 | if not controller_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5380 | raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5381 | |
| 5382 | #Obtain sdn controller info |
| 5383 | sdn_controller = ovim.show_of_controller(controller_id) |
| 5384 | |
| 5385 | port_data = { |
| 5386 | 'name': 'external_port', |
| 5387 | 'net_id': sdn_network_id, |
| 5388 | 'ofc_id': controller_id, |
| 5389 | 'switch_dpid': sdn_controller['dpid'], |
| 5390 | 'switch_port': descriptor['port'] |
| 5391 | } |
| 5392 | |
| 5393 | if 'vlan' in descriptor: |
| 5394 | port_data['vlan'] = descriptor['vlan'] |
| 5395 | if 'mac' in descriptor: |
| 5396 | port_data['mac'] = descriptor['mac'] |
| 5397 | |
| 5398 | result = ovim.new_port(port_data) |
| 5399 | except ovimException as e: |
| 5400 | raise NfvoException("ovimException attaching SDN network {} to vim network {}".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5401 | sdn_network_id, network_id) + str(e), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5402 | except db_base_Exception as e: |
| 5403 | raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5404 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5405 | |
| 5406 | return 'Port uuid: '+ result |
| 5407 | |
| 5408 | def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None): |
| 5409 | if port_id: |
| 5410 | filter = {'uuid': port_id} |
| 5411 | else: |
| 5412 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5413 | if not sdn_network_id: |
| 5414 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5415 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5416 | #in case no port_id is specified only ports marked as 'external_port' will be detached |
| 5417 | filter = {'name': 'external_port', 'net_id': sdn_network_id} |
| 5418 | |
| 5419 | try: |
| 5420 | port_list = ovim.get_ports(columns={'uuid'}, filter=filter) |
| 5421 | except ovimException as e: |
| 5422 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5423 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5424 | |
| 5425 | if len(port_list) == 0: |
| 5426 | raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5427 | httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5428 | |
| 5429 | port_uuid_list = [] |
| 5430 | for port in port_list: |
| 5431 | try: |
| 5432 | port_uuid_list.append(port['uuid']) |
| 5433 | ovim.delete_port(port['uuid']) |
| 5434 | except ovimException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5435 | raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5436 | |
| 5437 | return 'Detached ports uuid: {}'.format(','.join(port_uuid_list)) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5438 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5439 | def vim_action_get(mydb, tenant_id, datacenter, item, name): |
| 5440 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5441 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5442 | filter_dict={} |
| 5443 | if name: |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5444 | if utils.check_valid_uuid(name): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5445 | filter_dict["id"] = name |
| 5446 | else: |
| 5447 | filter_dict["name"] = name |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5448 | try: |
| 5449 | if item=="networks": |
| 5450 | #filter_dict['tenant_id'] = myvim['tenant_id'] |
| 5451 | content = myvim.get_network_list(filter_dict=filter_dict) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5452 | |
| 5453 | if len(content) == 0: |
| 5454 | raise NfvoException("Network {} is not present in the system. ".format(name), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5455 | httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5456 | |
| 5457 | #Update the networks with the attached ports |
| 5458 | for net in content: |
| 5459 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id']) |
| 5460 | if sdn_network_id != None: |
| 5461 | try: |
| 5462 | #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id}) |
| 5463 | port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id}) |
| 5464 | except ovimException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5465 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5466 | #Remove field name and if port name is external_port save it as 'type' |
| 5467 | for port in port_list: |
| 5468 | if port['name'] == 'external_port': |
| 5469 | port['type'] = "External" |
| 5470 | del port['name'] |
| 5471 | net['sdn_network_id'] = sdn_network_id |
| 5472 | net['sdn_attached_ports'] = port_list |
| 5473 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5474 | elif item=="tenants": |
| 5475 | content = myvim.get_tenant_list(filter_dict=filter_dict) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5476 | elif item == "images": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5477 | |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5478 | content = myvim.get_image_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5479 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5480 | raise NfvoException(item + "?", httperrors.Method_Not_Allowed) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 5481 | logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5482 | if name and len(content)==1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5483 | return {item[:-1]: content[0]} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5484 | elif name and len(content)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5485 | raise NfvoException("No {} found with ".format(item[:-1]) + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())), |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 5486 | datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5487 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5488 | return {item: content} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5489 | except vimconn.vimconnException as e: |
| 5490 | print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5491 | raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5492 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5493 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5494 | def vim_action_delete(mydb, tenant_id, datacenter, item, name): |
| 5495 | #get datacenter info |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5496 | if tenant_id == "any": |
| 5497 | tenant_id=None |
| 5498 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5499 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5500 | #get uuid name |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5501 | content = vim_action_get(mydb, tenant_id, datacenter, item, name) |
| 5502 | logger.debug("vim_action_delete vim response: " + str(content)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5503 | items = content.values()[0] |
| 5504 | if type(items)==list and len(items)==0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5505 | raise NfvoException("Not found " + item, httperrors.Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5506 | elif type(items)==list and len(items)>1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5507 | raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), httperrors.Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5508 | else: # it is a dict |
| 5509 | item_id = items["id"] |
| 5510 | item_name = str(items.get("name")) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5511 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5512 | try: |
| 5513 | if item=="networks": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5514 | # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it |
| 5515 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id) |
| 5516 | if sdn_network_id != None: |
| 5517 | #Delete any port attachment to this network |
| 5518 | try: |
| 5519 | port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id}) |
| 5520 | except ovimException as e: |
| 5521 | raise NfvoException( |
| 5522 | "ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5523 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5524 | |
| 5525 | # By calling one by one all ports to be detached we ensure that not only the external_ports get detached |
| 5526 | for port in port_list: |
| 5527 | vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid']) |
| 5528 | |
| 5529 | #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id |
| 5530 | try: |
| 5531 | mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id}) |
| 5532 | except db_base_Exception as e: |
| 5533 | raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) + |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5534 | str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5535 | |
| 5536 | #Delete the SDN network |
| 5537 | try: |
| 5538 | ovim.delete_network(sdn_network_id) |
| 5539 | except ovimException as e: |
| 5540 | logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True) |
| 5541 | raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5542 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5543 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5544 | content = myvim.delete_network(item_id) |
| 5545 | elif item=="tenants": |
| 5546 | content = myvim.delete_tenant(item_id) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5547 | elif item == "images": |
| 5548 | content = myvim.delete_image(item_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5549 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5550 | raise NfvoException(item + "?", httperrors.Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5551 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5552 | #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e))) |
| 5553 | raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5554 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5555 | return "{} {} {} deleted".format(item[:-1], item_id,item_name) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5556 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5557 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5558 | def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): |
| 5559 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5560 | logger.debug("vim_action_create descriptor %s", str(descriptor)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5561 | if tenant_id == "any": |
| 5562 | tenant_id=None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5563 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5564 | try: |
| 5565 | if item=="networks": |
| 5566 | net = descriptor["network"] |
| 5567 | net_name = net.pop("name") |
| 5568 | net_type = net.pop("type", "bridge") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 5569 | net_public = net.pop("shared", False) |
| 5570 | net_ipprofile = net.pop("ip_profile", None) |
| tierno | a7d34d0 | 2017-02-23 14:42:07 +0100 | [diff] [blame] | 5571 | net_vlan = net.pop("vlan", None) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 5572 | content, _ = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5573 | |
| 5574 | #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network |
| 5575 | if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'): |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5576 | #obtain datacenter_tenant_id |
| 5577 | datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',), |
| 5578 | FROM='datacenter_tenants', |
| 5579 | WHERE={'datacenter_id': datacenter})[0]['uuid'] |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5580 | try: |
| 5581 | sdn_network = {} |
| 5582 | sdn_network['vlan'] = net_vlan |
| 5583 | sdn_network['type'] = net_type |
| 5584 | sdn_network['name'] = net_name |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5585 | sdn_network['region'] = datacenter_tenant_id |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 5586 | ovim_content = ovim.new_network(sdn_network) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5587 | except ovimException as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5588 | logger.error("ovimException creating SDN network={} ".format( |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5589 | sdn_network) + str(e), exc_info=True) |
| 5590 | raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5591 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5592 | |
| 5593 | # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id |
| 5594 | # use instance_scenario_id=None to distinguish from real instaces of nets |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5595 | correspondence = {'instance_scenario_id': None, |
| 5596 | 'sdn_net_id': ovim_content, |
| 5597 | 'vim_net_id': content, |
| 5598 | 'datacenter_tenant_id': datacenter_tenant_id |
| 5599 | } |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5600 | try: |
| 5601 | mydb.new_row('instance_nets', correspondence, add_uuid=True) |
| 5602 | except db_base_Exception as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5603 | raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5604 | correspondence, e), e.http_code) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5605 | elif item=="tenants": |
| 5606 | tenant = descriptor["tenant"] |
| 5607 | content = myvim.new_tenant(tenant["name"], tenant.get("description")) |
| 5608 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5609 | raise NfvoException(item + "?", httperrors.Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5610 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5611 | raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5612 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5613 | return vim_action_get(mydb, tenant_id, datacenter, item, content) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5614 | |
| 5615 | def sdn_controller_create(mydb, tenant_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5616 | data = ovim.new_of_controller(sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5617 | logger.debug('New SDN controller created with uuid {}'.format(data)) |
| 5618 | return data |
| 5619 | |
| 5620 | def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5621 | data = ovim.edit_of_controller(controller_id, sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5622 | msg = 'SDN controller {} updated'.format(data) |
| 5623 | logger.debug(msg) |
| 5624 | return msg |
| 5625 | |
| 5626 | def sdn_controller_list(mydb, tenant_id, controller_id=None): |
| 5627 | if controller_id == None: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5628 | data = ovim.get_of_controllers() |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5629 | else: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5630 | data = ovim.show_of_controller(controller_id) |
| 5631 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5632 | msg = 'SDN controller list:\n {}'.format(data) |
| 5633 | logger.debug(msg) |
| 5634 | return data |
| 5635 | |
| 5636 | def sdn_controller_delete(mydb, tenant_id, controller_id): |
| 5637 | select_ = ('uuid', 'config') |
| 5638 | datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_) |
| 5639 | for datacenter in datacenters: |
| 5640 | if datacenter['config']: |
| 5641 | config = yaml.load(datacenter['config']) |
| 5642 | if 'sdn-controller' in config and config['sdn-controller'] == controller_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5643 | raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), httperrors.Conflict) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5644 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5645 | data = ovim.delete_of_controller(controller_id) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5646 | msg = 'SDN controller {} deleted'.format(data) |
| 5647 | logger.debug(msg) |
| 5648 | return msg |
| 5649 | |
| 5650 | def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping): |
| 5651 | controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id}) |
| 5652 | if len(controller) < 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5653 | raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), httperrors.Not_Found) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5654 | |
| 5655 | try: |
| 5656 | sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"] |
| 5657 | except: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5658 | raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), httperrors.Bad_Request) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5659 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5660 | sdn_controller = ovim.show_of_controller(sdn_controller_id) |
| 5661 | switch_dpid = sdn_controller["dpid"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5662 | |
| 5663 | maps = list() |
| 5664 | for compute_node in sdn_port_mapping: |
| 5665 | #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid} |
| 5666 | element = dict() |
| 5667 | element["compute_node"] = compute_node["compute_node"] |
| 5668 | for port in compute_node["ports"]: |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5669 | pci = port.get("pci") |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5670 | element["switch_port"] = port.get("switch_port") |
| 5671 | element["switch_mac"] = port.get("switch_mac") |
| tierno | 4070e44 | 2019-01-23 10:19:23 +0000 | [diff] [blame] | 5672 | if not element["switch_port"] and not element["switch_mac"]: |
| 5673 | raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors.Bad_Request) |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5674 | for pci_expanded in utils.expand_brackets(pci): |
| 5675 | element["pci"] = pci_expanded |
| 5676 | maps.append(dict(element)) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5677 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5678 | return ovim.set_of_port_mapping(maps, ofc_id=sdn_controller_id, switch_dpid=switch_dpid, region=datacenter_id) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5679 | |
| 5680 | def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5681 | maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id}) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5682 | |
| 5683 | result = { |
| 5684 | "sdn-controller": None, |
| 5685 | "datacenter-id": datacenter_id, |
| 5686 | "dpid": None, |
| 5687 | "ports_mapping": list() |
| 5688 | } |
| 5689 | |
| 5690 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id) |
| 5691 | if datacenter['config']: |
| 5692 | config = yaml.load(datacenter['config']) |
| 5693 | if 'sdn-controller' in config: |
| 5694 | controller_id = config['sdn-controller'] |
| 5695 | sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id) |
| 5696 | result["sdn-controller"] = controller_id |
| 5697 | result["dpid"] = sdn_controller["dpid"] |
| 5698 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5699 | if result["sdn-controller"] == None: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5700 | raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5701 | if result["dpid"] == None: |
| 5702 | raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5703 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5704 | |
| 5705 | if len(maps) == 0: |
| 5706 | return result |
| 5707 | |
| 5708 | ports_correspondence_dict = dict() |
| 5709 | for link in maps: |
| 5710 | if result["sdn-controller"] != link["ofc_id"]: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5711 | raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5712 | if result["dpid"] != link["switch_dpid"]: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5713 | raise NfvoException("The dpid specified for different port mappings differ", httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5714 | element = dict() |
| 5715 | element["pci"] = link["pci"] |
| 5716 | if link["switch_port"]: |
| 5717 | element["switch_port"] = link["switch_port"] |
| 5718 | if link["switch_mac"]: |
| 5719 | element["switch_mac"] = link["switch_mac"] |
| 5720 | |
| 5721 | if not link["compute_node"] in ports_correspondence_dict: |
| 5722 | content = dict() |
| 5723 | content["compute_node"] = link["compute_node"] |
| 5724 | content["ports"] = list() |
| 5725 | ports_correspondence_dict[link["compute_node"]] = content |
| 5726 | |
| 5727 | ports_correspondence_dict[link["compute_node"]]["ports"].append(element) |
| 5728 | |
| 5729 | for key in sorted(ports_correspondence_dict): |
| 5730 | result["ports_mapping"].append(ports_correspondence_dict[key]) |
| 5731 | |
| 5732 | return result |
| 5733 | |
| 5734 | def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id): |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 5735 | return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id}) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5736 | |
| 5737 | def create_RO_keypair(tenant_id): |
| 5738 | """ |
| 5739 | Creates a public / private keys for a RO tenant and returns their values |
| 5740 | Params: |
| 5741 | tenant_id: ID of the tenant |
| 5742 | Return: |
| 5743 | public_key: Public key for the RO tenant |
| 5744 | private_key: Encrypted private key for RO tenant |
| 5745 | """ |
| 5746 | |
| 5747 | bits = 2048 |
| 5748 | key = RSA.generate(bits) |
| 5749 | try: |
| 5750 | public_key = key.publickey().exportKey('OpenSSH') |
| 5751 | if isinstance(public_key, ValueError): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5752 | raise NfvoException("Unable to create public key: {}".format(public_key), httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5753 | private_key = key.exportKey(passphrase=tenant_id, pkcs=8) |
| 5754 | except (ValueError, NameError) as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5755 | raise NfvoException("Unable to create private key: {}".format(e), httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5756 | return public_key, private_key |
| 5757 | |
| 5758 | def decrypt_key (key, tenant_id): |
| 5759 | """ |
| 5760 | Decrypts an encrypted RSA key |
| 5761 | Params: |
| 5762 | key: Private key to be decrypted |
| 5763 | tenant_id: ID of the tenant |
| 5764 | Return: |
| 5765 | unencrypted_key: Unencrypted private key for RO tenant |
| 5766 | """ |
| 5767 | try: |
| 5768 | key = RSA.importKey(key,tenant_id) |
| 5769 | unencrypted_key = key.exportKey('PEM') |
| 5770 | if isinstance(unencrypted_key, ValueError): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5771 | raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5772 | except ValueError as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5773 | raise NfvoException("Unable to decrypt the private key: {}".format(e), httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5774 | return unencrypted_key |