| 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 | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 129 | name = datacenter_name[:16] + "." + tenant_name[:16] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 130 | if name not in vim_threads["names"]: |
| 131 | vim_threads["names"].append(name) |
| 132 | return name |
| 133 | name = datacenter_id + "-" + tenant_id |
| 134 | vim_threads["names"].append(name) |
| 135 | return name |
| 136 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 137 | # -- Move |
| 138 | def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id): |
| 139 | name = wim_name[:16] |
| 140 | if name not in wim_threads["names"]: |
| 141 | wim_threads["names"].append(name) |
| 142 | return name |
| 143 | name = wim_name[:16] + "." + tenant_name[:16] |
| 144 | if name not in wim_threads["names"]: |
| 145 | wim_threads["names"].append(name) |
| 146 | return name |
| 147 | name = wim_id + "-" + tenant_id |
| 148 | wim_threads["names"].append(name) |
| 149 | return name |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 150 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 151 | |
| 152 | def start_service(mydb, persistence=None, wim=None): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 153 | global db, global_config |
| Anderson Bravalheri | dfed511 | 2019-02-08 01:44:14 +0000 | [diff] [blame] | 154 | db = nfvo_db.nfvo_db(lock=db_lock) |
| 155 | mydb.lock = db_lock |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 156 | 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] | 157 | global ovim |
| 158 | |
| Anderson Bravalheri | dfed511 | 2019-02-08 01:44:14 +0000 | [diff] [blame] | 159 | persistence = persistence or WimPersistence(db) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 160 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 161 | # Initialize openvim for SDN control |
| 162 | # TODO: Avoid static configuration by adding new parameters to openmanod.cfg |
| 163 | # TODO: review ovim.py to delete not needed configuration |
| 164 | ovim_configuration = { |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 165 | 'logger_name': 'openmano.ovim', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 166 | 'network_vlan_range_start': 1000, |
| 167 | 'network_vlan_range_end': 4096, |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 168 | 'db_name': global_config["db_ovim_name"], |
| 169 | 'db_host': global_config["db_ovim_host"], |
| 170 | 'db_user': global_config["db_ovim_user"], |
| 171 | 'db_passwd': global_config["db_ovim_passwd"], |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 172 | 'bridge_ifaces': {}, |
| 173 | 'mode': 'normal', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 174 | 'network_type': 'bridge', |
| 175 | #TODO: log_level_of should not be needed. To be modified in ovim |
| 176 | 'log_level_of': 'DEBUG' |
| 177 | } |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 178 | try: |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 179 | # starts ovim library |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 180 | ovim = ovim_module.ovim(ovim_configuration) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 181 | |
| 182 | global wim_engine |
| 183 | wim_engine = wim or WimEngine(persistence) |
| 184 | wim_engine.ovim = ovim |
| 185 | |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 186 | ovim.start_service() |
| 187 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 188 | #delete old unneeded vim_wim_actions |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 189 | clean_db(mydb) |
| 190 | |
| 191 | # starts vim_threads |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 192 | from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\ |
| 193 | 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid' |
| 194 | select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin', |
| 195 | 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id', |
| 196 | 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id', |
| 197 | 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id') |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 198 | vims = mydb.get_rows(FROM=from_, SELECT=select_) |
| 199 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 200 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 201 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 202 | if vim["config"]: |
| 203 | extra.update(yaml.load(vim["config"])) |
| 204 | if vim.get('dt_config'): |
| 205 | extra.update(yaml.load(vim["dt_config"])) |
| 206 | if vim["type"] not in vimconn_imported: |
| 207 | module_info=None |
| 208 | try: |
| 209 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 210 | pkg = __import__("osm_ro." + module) |
| 211 | vim_conn = getattr(pkg, module) |
| 212 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 213 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 214 | vimconn_imported[vim["type"]] = vim_conn |
| 215 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 216 | # if module_info and module_info[0]: |
| 217 | # file.close(module_info[0]) |
| tierno | cdee8cc | 2017-04-25 13:42:06 +0200 | [diff] [blame] | 218 | raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 219 | vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 220 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 221 | thread_id = vim['datacenter_tenant_id'] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 222 | vim_persistent_info[thread_id] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 223 | try: |
| 224 | #if not tenant: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 225 | # 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] | 226 | myvim = vimconn_imported[ vim["type"] ].vimconnector( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 227 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| 228 | tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'], |
| 229 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| 230 | user=vim['user'], passwd=vim['passwd'], |
| 231 | config=extra, persistent_info=vim_persistent_info[thread_id] |
| 232 | ) |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 233 | except vimconn.vimconnException as e: |
| 234 | myvim = e |
| 235 | logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'], |
| 236 | vim['datacenter_id'], e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 237 | except Exception as e: |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 238 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 239 | httperrors.Internal_Server_Error) |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 240 | thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], |
| 241 | vim['vim_tenant_id']) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 242 | new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'], |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 243 | vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 244 | new_thread.start() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 245 | vim_threads["running"][thread_id] = new_thread |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 246 | |
| 247 | wim_engine.start_threads() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 248 | except db_base_Exception as e: |
| 249 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 250 | except ovim_module.ovimException as e: |
| 251 | message = str(e) |
| 252 | if message[:22] == "DATABASE wrong version": |
| 253 | message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\ |
| 254 | "at host {dbhost}".format( |
| 255 | msg=message[22:-3], dbname=global_config["db_ovim_name"], |
| 256 | dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"], |
| 257 | ver=message[-3:-1], dbhost=global_config["db_ovim_host"]) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 258 | raise NfvoException(message, httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 259 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 260 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 261 | def stop_service(): |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 262 | global ovim, global_config |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 263 | if ovim: |
| 264 | ovim.stop_service() |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 265 | for thread_id, thread in vim_threads["running"].items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 266 | thread.insert_task("exit") |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 267 | vim_threads["deleting"][thread_id] = thread |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 268 | vim_threads["running"] = {} |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 269 | |
| 270 | if wim_engine: |
| 271 | wim_engine.stop_threads() |
| 272 | |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 273 | if global_config and global_config.get("console_thread"): |
| 274 | for thread in global_config["console_thread"]: |
| 275 | thread.terminate = True |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 276 | |
| tierno | 6ddeded | 2017-05-16 15:40:26 +0200 | [diff] [blame] | 277 | def get_version(): |
| 278 | return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"], |
| 279 | global_config["version_date"] )) |
| 280 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 281 | def clean_db(mydb): |
| 282 | """ |
| 283 | Clean unused or old entries at database to avoid unlimited growing |
| 284 | :param mydb: database connector |
| 285 | :return: None |
| 286 | """ |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 287 | # 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] | 288 | now = t.time()-3600*24*7 |
| 289 | instance_action_id = None |
| 290 | nb_deleted = 0 |
| 291 | while True: |
| 292 | actions_to_delete = mydb.get_rows( |
| 293 | SELECT=("item", "item_id", "instance_action_id"), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 294 | 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] | 295 | "left join instance_scenarios as i on ia.instance_id=i.uuid", |
| 296 | WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None, |
| 297 | "va.status": ("DONE", "SUPERSEDED")}, |
| 298 | LIMIT=100 |
| 299 | ) |
| 300 | for to_delete in actions_to_delete: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 301 | mydb.delete_row(FROM="vim_wim_actions", WHERE=to_delete) |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 302 | if instance_action_id != to_delete["instance_action_id"]: |
| 303 | instance_action_id = to_delete["instance_action_id"] |
| 304 | mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id}) |
| 305 | nb_deleted += len(actions_to_delete) |
| 306 | if len(actions_to_delete) < 100: |
| 307 | break |
| 308 | if nb_deleted: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 309 | logger.debug("Removed {} unused vim_wim_actions".format(nb_deleted)) |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 310 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 311 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 312 | def get_flavorlist(mydb, vnf_id, nfvo_tenant=None): |
| 313 | '''Obtain flavorList |
| 314 | return result, content: |
| 315 | <0, error_text upon error |
| 316 | nb_records, flavor_list on success |
| 317 | ''' |
| 318 | WHERE_dict={} |
| 319 | WHERE_dict['vnf_id'] = vnf_id |
| 320 | if nfvo_tenant is not None: |
| 321 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 322 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 323 | #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict ) |
| 324 | #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] | 325 | flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict ) |
| 326 | #print "get_flavor_list result:", result |
| 327 | #print "get_flavor_list content:", content |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 328 | flavorList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 329 | for flavor in flavors: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 330 | flavorList.append(flavor['flavor_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 331 | return flavorList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 332 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 333 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 334 | def get_imagelist(mydb, vnf_id, nfvo_tenant=None): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 335 | """ |
| 336 | Get used images of all vms belonging to this VNFD |
| 337 | :param mydb: database conector |
| 338 | :param vnf_id: vnfd uuid |
| 339 | :param nfvo_tenant: tenant, not used |
| 340 | :return: The list of image uuid used |
| 341 | """ |
| 342 | image_list = [] |
| 343 | vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id}) |
| 344 | for vm in vms: |
| tierno | 89aada4 | 2018-12-19 16:00:25 +0000 | [diff] [blame] | 345 | if vm["image_id"] and vm["image_id"] not in image_list: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 346 | image_list.append(vm["image_id"]) |
| 347 | if vm["image_list"]: |
| 348 | vm_image_list = yaml.load(vm["image_list"]) |
| 349 | for image_dict in vm_image_list: |
| 350 | if image_dict["image_id"] not in image_list: |
| 351 | image_list.append(image_dict["image_id"]) |
| 352 | return image_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 353 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 354 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 355 | 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] | 356 | 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] | 357 | '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 358 | return dictionary with {datacenter_id: vim_class, ... }. vim_class contain: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 359 | '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] | 360 | raise exception upon error |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 361 | ''' |
| 362 | WHERE_dict={} |
| 363 | if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| 364 | if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 365 | 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] | 366 | if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name |
| 367 | 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] | 368 | if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name |
| 369 | 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] | 370 | 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] | 371 | 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] | 372 | '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] | 373 | 'user','passwd', 'dt.config as dt_config') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 374 | else: |
| 375 | from_ = 'datacenters as d' |
| 376 | 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] | 377 | try: |
| 378 | vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict ) |
| 379 | vim_dict={} |
| 380 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 381 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 382 | 'datacenter_id': vim.get('datacenter_id'), |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 383 | '_vim_type_internal': vim.get('type')} |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 384 | if vim["config"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 385 | extra.update(yaml.load(vim["config"])) |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 386 | if vim.get('dt_config'): |
| 387 | extra.update(yaml.load(vim["dt_config"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 388 | if vim["type"] not in vimconn_imported: |
| 389 | module_info=None |
| 390 | try: |
| 391 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 392 | pkg = __import__("osm_ro." + module) |
| 393 | vim_conn = getattr(pkg, module) |
| 394 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 395 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 396 | vimconn_imported[vim["type"]] = vim_conn |
| 397 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 398 | # if module_info and module_info[0]: |
| 399 | # file.close(module_info[0]) |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 400 | if ignore_errors: |
| 401 | logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 402 | vim["type"], module, type(e).__name__, str(e))) |
| 403 | continue |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 404 | raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 405 | vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 406 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 407 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 408 | if 'datacenter_tenant_id' in vim: |
| 409 | thread_id = vim["datacenter_tenant_id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 410 | if thread_id not in vim_persistent_info: |
| 411 | vim_persistent_info[thread_id] = {} |
| 412 | persistent_info = vim_persistent_info[thread_id] |
| 413 | else: |
| 414 | persistent_info = {} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 415 | #if not tenant: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 416 | # 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] | 417 | vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector( |
| 418 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 419 | tenant_id=vim.get('vim_tenant_id',vim_tenant), |
| 420 | tenant_name=vim.get('vim_tenant_name',vim_tenant_name), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 421 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 422 | user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd), |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 423 | config=extra, persistent_info=persistent_info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 424 | ) |
| 425 | except Exception as e: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 426 | if ignore_errors: |
| 427 | logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e))) |
| 428 | continue |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 429 | http_code = httperrors.Internal_Server_Error |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 430 | if isinstance(e, vimconn.vimconnException): |
| 431 | http_code = e.http_code |
| 432 | 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] | 433 | return vim_dict |
| 434 | except db_base_Exception as e: |
| 435 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 436 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 437 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 438 | def rollback(mydb, vims, rollback_list): |
| 439 | undeleted_items=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 440 | #delete things by reverse order |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 441 | for i in range(len(rollback_list)-1, -1, -1): |
| 442 | item = rollback_list[i] |
| 443 | if item["where"]=="vim": |
| 444 | if item["vim_id"] not in vims: |
| 445 | continue |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 446 | if is_task_id(item["uuid"]): |
| 447 | continue |
| 448 | vim = vims[item["vim_id"]] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 449 | try: |
| 450 | if item["what"]=="image": |
| 451 | vim.delete_image(item["uuid"]) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 452 | 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] | 453 | elif item["what"]=="flavor": |
| 454 | vim.delete_flavor(item["uuid"]) |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 455 | 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] | 456 | elif item["what"]=="network": |
| 457 | vim.delete_network(item["uuid"]) |
| 458 | elif item["what"]=="vm": |
| 459 | vim.delete_vminstance(item["uuid"]) |
| 460 | except vimconn.vimconnException as e: |
| 461 | logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e)) |
| 462 | undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 463 | except db_base_Exception as e: |
| 464 | 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] | 465 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 466 | else: # where==mano |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 467 | try: |
| 468 | if item["what"]=="image": |
| 469 | mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]}) |
| 470 | elif item["what"]=="flavor": |
| 471 | mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]}) |
| 472 | except db_base_Exception as e: |
| 473 | logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e)) |
| 474 | undeleted_items.append("{} '{}'".format(item['what'], item["uuid"])) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 475 | if len(undeleted_items)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 476 | return True," Rollback successful." |
| 477 | else: |
| 478 | return False," Rollback fails to delete: " + str(undeleted_items) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 479 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 480 | |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 481 | def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 482 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 483 | #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 484 | vnfc_interfaces={} |
| 485 | for vnfc in vnf_descriptor["vnf"]["VNFC"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 486 | name_dict = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 487 | #dataplane interfaces |
| 488 | for numa in vnfc.get("numas",() ): |
| 489 | for interface in numa.get("interfaces",()): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 490 | if interface["name"] in name_dict: |
| 491 | raise NfvoException( |
| 492 | "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format( |
| 493 | vnfc["name"], interface["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 494 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 495 | name_dict[ interface["name"] ] = "underlay" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 496 | #bridge interfaces |
| 497 | for interface in vnfc.get("bridge-ifaces",() ): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 498 | if interface["name"] in name_dict: |
| 499 | raise NfvoException( |
| 500 | "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format( |
| 501 | vnfc["name"], interface["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 502 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 503 | name_dict[ interface["name"] ] = "overlay" |
| 504 | vnfc_interfaces[ vnfc["name"] ] = name_dict |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 505 | # check bood-data info |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 506 | # if "boot-data" in vnfc: |
| 507 | # # check that user-data is incompatible with users and config-files |
| 508 | # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"): |
| 509 | # raise NfvoException( |
| 510 | # "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] | 511 | # httperrors.Bad_Request) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 512 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 513 | #check if the info in external_connections matches with the one in the vnfcs |
| 514 | name_list=[] |
| 515 | for external_connection in vnf_descriptor["vnf"].get("external-connections",() ): |
| 516 | if external_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 517 | raise NfvoException( |
| 518 | "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format( |
| 519 | external_connection["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 520 | httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 521 | name_list.append(external_connection["name"]) |
| 522 | if external_connection["VNFC"] not in vnfc_interfaces: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 523 | raise NfvoException( |
| 524 | "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format( |
| 525 | external_connection["name"], external_connection["VNFC"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 526 | httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 527 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 528 | if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 529 | raise NfvoException( |
| 530 | "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 531 | external_connection["name"], |
| 532 | external_connection["local_iface_name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 533 | httperrors.Bad_Request ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 534 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 535 | #check if the info in internal_connections matches with the one in the vnfcs |
| 536 | name_list=[] |
| 537 | for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ): |
| 538 | if internal_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 539 | raise NfvoException( |
| 540 | "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format( |
| 541 | internal_connection["name"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 542 | httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 543 | name_list.append(internal_connection["name"]) |
| 544 | #We should check that internal-connections of type "ptp" have only 2 elements |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 545 | |
| 546 | if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"): |
| 547 | raise NfvoException( |
| 548 | "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format( |
| 549 | internal_connection["name"], |
| 550 | 'ptp' if vnf_descriptor_version==1 else 'e-line', |
| 551 | 'data' if vnf_descriptor_version==1 else "e-lan"), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 552 | httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 553 | for port in internal_connection["elements"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 554 | vnf = port["VNFC"] |
| 555 | iface = port["local_iface_name"] |
| 556 | if vnf not in vnfc_interfaces: |
| 557 | raise NfvoException( |
| 558 | "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format( |
| 559 | internal_connection["name"], vnf), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 560 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 561 | if iface not in vnfc_interfaces[ vnf ]: |
| 562 | raise NfvoException( |
| 563 | "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 564 | internal_connection["name"], iface), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 565 | httperrors.Bad_Request) |
| 566 | return -httperrors.Bad_Request, |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 567 | if vnf_descriptor_version==1 and "type" not in internal_connection: |
| 568 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 569 | internal_connection["type"] = "bridge" |
| 570 | else: |
| 571 | internal_connection["type"] = "data" |
| 572 | if vnf_descriptor_version==2 and "implementation" not in internal_connection: |
| 573 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 574 | internal_connection["implementation"] = "overlay" |
| 575 | else: |
| 576 | internal_connection["implementation"] = "underlay" |
| 577 | if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \ |
| 578 | internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay": |
| 579 | raise NfvoException( |
| 580 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 581 | internal_connection["name"], |
| 582 | iface, 'bridge' if vnf_descriptor_version==1 else 'overlay', |
| 583 | 'data' if vnf_descriptor_version==1 else 'underlay'), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 584 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 585 | if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \ |
| 586 | vnfc_interfaces[vnf][iface] == "underlay": |
| 587 | raise NfvoException( |
| 588 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 589 | internal_connection["name"], iface, |
| 590 | 'data' if vnf_descriptor_version==1 else 'underlay', |
| 591 | 'bridge' if vnf_descriptor_version==1 else 'overlay'), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 592 | httperrors.Bad_Request) |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 593 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 594 | |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 595 | 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] | 596 | #look if image exist |
| 597 | if only_create_at_vim: |
| 598 | image_mano_id = image_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 599 | if return_on_error == None: |
| 600 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 601 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 602 | if image_dict['location']: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 603 | images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']}) |
| 604 | else: |
| 605 | 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] | 606 | if len(images)>=1: |
| 607 | image_mano_id = images[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 608 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 609 | #create image in MANO DB |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 610 | temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None), |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 611 | 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None), |
| 612 | 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 613 | } |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 614 | #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] | 615 | image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True) |
| 616 | rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 617 | #create image at every vim |
| 618 | for vim_id,vim in vims.iteritems(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 619 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 620 | image_created="false" |
| 621 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 622 | image_db = mydb.get_rows(FROM="datacenters_images", |
| 623 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 624 | #look at VIM if this image exist |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 625 | try: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 626 | if image_dict['location'] is not None: |
| 627 | image_vim_id = vim.get_image_id_from_path(image_dict['location']) |
| 628 | else: |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 629 | filter_dict = {} |
| 630 | filter_dict['name'] = image_dict['universal_name'] |
| 631 | if image_dict.get('checksum') != None: |
| 632 | filter_dict['checksum'] = image_dict['checksum'] |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 633 | #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 634 | vim_images = vim.get_image_list(filter_dict) |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 635 | #logger.debug('>>>>>>>> VIM images: %s', str(vim_images)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 636 | if len(vim_images) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 637 | 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] | 638 | elif len(vim_images) == 0: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 639 | 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] | 640 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 641 | #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0])) |
| 642 | image_vim_id = vim_images[0]['id'] |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 643 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 644 | except vimconn.vimconnNotFoundException as e: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 645 | #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] | 646 | try: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 647 | #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None |
| 648 | if image_dict['location']: |
| 649 | image_vim_id = vim.new_image(image_dict) |
| 650 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id}) |
| 651 | image_created="true" |
| 652 | else: |
| garciadeblas | b6153a2 | 2017-02-06 15:38:33 +0100 | [diff] [blame] | 653 | #If we reach this point, then the image has image name, and optionally checksum, and could not be found |
| 654 | raise vimconn.vimconnException(str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 655 | except vimconn.vimconnException as e: |
| 656 | if return_on_error: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 657 | logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 658 | raise |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 659 | image_vim_id = None |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 660 | logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 661 | continue |
| 662 | except vimconn.vimconnException as e: |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 663 | if return_on_error: |
| 664 | logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e)) |
| 665 | raise |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 666 | 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] | 667 | image_vim_id = None |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 668 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 669 | #if we reach here, the image has been created or existed |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 670 | if len(image_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 671 | #add new vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 672 | mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id, |
| 673 | 'image_id':image_mano_id, |
| 674 | 'vim_id': image_vim_id, |
| 675 | 'created':image_created}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 676 | elif image_db[0]["vim_id"]!=image_vim_id: |
| 677 | #modify existing vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 678 | 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] | 679 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 680 | return image_vim_id if only_create_at_vim else image_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 681 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 682 | |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 683 | 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] | 684 | temp_flavor_dict= {'disk':flavor_dict.get('disk',0), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 685 | 'ram':flavor_dict.get('ram'), |
| 686 | 'vcpus':flavor_dict.get('vcpus'), |
| 687 | } |
| 688 | if 'extended' in flavor_dict and flavor_dict['extended']==None: |
| 689 | del flavor_dict['extended'] |
| 690 | if 'extended' in flavor_dict: |
| 691 | temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256) |
| 692 | |
| 693 | #look if flavor exist |
| 694 | if only_create_at_vim: |
| 695 | flavor_mano_id = flavor_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 696 | if return_on_error == None: |
| 697 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 698 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 699 | flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 700 | if len(flavors)>=1: |
| 701 | flavor_mano_id = flavors[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 702 | else: |
| 703 | #create flavor |
| 704 | #create one by one the images of aditional disks |
| 705 | dev_image_list=[] #list of images |
| 706 | if 'extended' in flavor_dict and flavor_dict['extended']!=None: |
| 707 | dev_nb=0 |
| 708 | for device in flavor_dict['extended'].get('devices',[]): |
| garciadeblas | 41f18be | 2016-10-04 09:09:58 +0200 | [diff] [blame] | 709 | if "image" not in device and "image name" not in device: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 710 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 711 | image_dict={} |
| 712 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 713 | image_dict['universal_name']=device.get('image name') |
| 714 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 715 | image_dict['location']=device.get('image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 716 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 717 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 718 | image_metadata_dict = device.get('image metadata', None) |
| 719 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 720 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 721 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 722 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 723 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 724 | #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] | 725 | dev_image_list.append(image_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 726 | dev_nb += 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 727 | temp_flavor_dict['name'] = flavor_dict['name'] |
| 728 | temp_flavor_dict['description'] = flavor_dict.get('description',None) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 729 | content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True) |
| 730 | flavor_mano_id= content |
| 731 | rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 732 | #create flavor at every vim |
| 733 | if 'uuid' in flavor_dict: |
| 734 | del flavor_dict['uuid'] |
| 735 | flavor_vim_id=None |
| 736 | for vim_id,vim in vims.items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 737 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 738 | flavor_created="false" |
| 739 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 740 | flavor_db = mydb.get_rows(FROM="datacenters_flavors", |
| 741 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 742 | #look at VIM if this flavor exist SKIPPED |
| 743 | #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location']) |
| 744 | #if res_vim < 0: |
| 745 | # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id |
| 746 | # continue |
| 747 | #elif res_vim==0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 748 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 749 | # Create the flavor in VIM |
| 750 | # Translate images at devices from MANO id to VIM id |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 751 | disk_list = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 752 | 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] | 753 | # make a copy of original devices |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 754 | devices_original=[] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 755 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 756 | for device in flavor_dict["extended"].get("devices",[]): |
| 757 | dev={} |
| 758 | dev.update(device) |
| 759 | devices_original.append(dev) |
| 760 | if 'image' in device: |
| 761 | del device['image'] |
| 762 | if 'image metadata' in device: |
| 763 | del device['image metadata'] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 764 | if 'image checksum' in device: |
| 765 | del device['image checksum'] |
| 766 | dev_nb = 0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 767 | for index in range(0,len(devices_original)) : |
| 768 | device=devices_original[index] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 769 | if "image" not in device and "image name" not in device: |
| tierno | ecc6839 | 2018-09-06 13:47:11 +0200 | [diff] [blame] | 770 | # if 'size' in device: |
| 771 | 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] | 772 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 773 | image_dict={} |
| 774 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 775 | image_dict['universal_name']=device.get('image name') |
| 776 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 777 | image_dict['location']=device.get('image') |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 778 | # image_dict['new_location']=device.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 779 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 780 | image_metadata_dict = device.get('image metadata', None) |
| 781 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 782 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 783 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 784 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 785 | 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] | 786 | image_dict["uuid"]=image_mano_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 787 | 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] | 788 | |
| 789 | #save disk information (image must be based on and size |
| 790 | disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)}) |
| 791 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 792 | flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id |
| 793 | dev_nb += 1 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 794 | if len(flavor_db)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 795 | #check that this vim_id exist in VIM, if not create |
| 796 | flavor_vim_id=flavor_db[0]["vim_id"] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 797 | try: |
| 798 | vim.get_flavor(flavor_vim_id) |
| 799 | continue #flavor exist |
| 800 | except vimconn.vimconnException: |
| 801 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 802 | #create flavor at vim |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 803 | logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"]) |
| 804 | try: |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 805 | flavor_vim_id = None |
| 806 | flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict) |
| 807 | flavor_create="false" |
| 808 | except vimconn.vimconnException as e: |
| 809 | pass |
| 810 | try: |
| 811 | if not flavor_vim_id: |
| 812 | flavor_vim_id = vim.new_flavor(flavor_dict) |
| 813 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id}) |
| 814 | flavor_created="true" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 815 | except vimconn.vimconnException as e: |
| 816 | if return_on_error: |
| 817 | logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 818 | raise |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 819 | logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 820 | flavor_vim_id = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 821 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 822 | #if reach here the flavor has been create or exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 823 | if len(flavor_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 824 | #add new vim_id at datacenters_flavors |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 825 | extended_devices_yaml = None |
| 826 | if len(disk_list) > 0: |
| 827 | extended_devices = dict() |
| 828 | extended_devices['disks'] = disk_list |
| 829 | extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256) |
| 830 | mydb.new_row('datacenters_flavors', |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 831 | {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id, |
| 832 | 'created': flavor_created, 'extended': extended_devices_yaml}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 833 | elif flavor_db[0]["vim_id"]!=flavor_vim_id: |
| 834 | #modify existing vim_id at datacenters_flavors |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 835 | mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, |
| 836 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 837 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 838 | return flavor_vim_id if only_create_at_vim else flavor_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 839 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 840 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 841 | def get_str(obj, field, length): |
| 842 | """ |
| 843 | Obtain the str value, |
| 844 | :param obj: |
| 845 | :param length: |
| 846 | :return: |
| 847 | """ |
| 848 | value = obj.get(field) |
| 849 | if value is not None: |
| 850 | value = str(value)[:length] |
| 851 | return value |
| 852 | |
| 853 | def _lookfor_or_create_image(db_image, mydb, descriptor): |
| 854 | """ |
| 855 | fill image content at db_image dictionary. Check if the image with this image and checksum exist |
| 856 | :param db_image: dictionary to insert data |
| 857 | :param mydb: database connector |
| 858 | :param descriptor: yang descriptor |
| 859 | :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image |
| 860 | """ |
| 861 | |
| 862 | db_image["name"] = get_str(descriptor, "image", 255) |
| 863 | db_image["checksum"] = get_str(descriptor, "image-checksum", 32) |
| 864 | if not db_image["checksum"]: # Ensure that if empty string, None is stored |
| 865 | db_image["checksum"] = None |
| 866 | if db_image["name"].startswith("/"): |
| 867 | db_image["location"] = db_image["name"] |
| 868 | existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]}) |
| 869 | else: |
| 870 | db_image["universal_name"] = db_image["name"] |
| 871 | existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'], |
| 872 | 'checksum': db_image['checksum']}) |
| 873 | if existing_images: |
| 874 | return existing_images[0]["uuid"] |
| 875 | else: |
| 876 | image_uuid = str(uuid4()) |
| 877 | db_image["uuid"] = image_uuid |
| 878 | return None |
| 879 | |
| 880 | def new_vnfd_v3(mydb, tenant_id, vnf_descriptor): |
| 881 | """ |
| 882 | Parses an OSM IM vnfd_catalog and insert at DB |
| 883 | :param mydb: |
| 884 | :param tenant_id: |
| 885 | :param vnf_descriptor: |
| 886 | :return: The list of cretated vnf ids |
| 887 | """ |
| 888 | try: |
| 889 | myvnfd = vnfd_catalog.vnfd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 890 | try: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 891 | pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True) |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 892 | except Exception as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 893 | raise NfvoException("Error. Invalid VNF descriptor format " + str(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 894 | db_vnfs = [] |
| 895 | db_nets = [] |
| 896 | db_vms = [] |
| 897 | db_vms_index = 0 |
| 898 | db_interfaces = [] |
| 899 | db_images = [] |
| 900 | db_flavors = [] |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 901 | db_ip_profiles_index = 0 |
| 902 | db_ip_profiles = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 903 | uuid_list = [] |
| 904 | vnfd_uuid_list = [] |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 905 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog") |
| 906 | if not vnfd_catalog_descriptor: |
| 907 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog") |
| 908 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd") |
| 909 | if not vnfd_descriptor_list: |
| 910 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd") |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 911 | for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues(): |
| 912 | vnfd = vnfd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 913 | |
| 914 | # table vnf |
| 915 | vnf_uuid = str(uuid4()) |
| 916 | uuid_list.append(vnf_uuid) |
| 917 | vnfd_uuid_list.append(vnf_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 918 | vnfd_id = get_str(vnfd, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 919 | db_vnf = { |
| 920 | "uuid": vnf_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 921 | "osm_id": vnfd_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 922 | "name": get_str(vnfd, "name", 255), |
| 923 | "description": get_str(vnfd, "description", 255), |
| 924 | "tenant_id": tenant_id, |
| 925 | "vendor": get_str(vnfd, "vendor", 255), |
| 926 | "short_name": get_str(vnfd, "short-name", 255), |
| 927 | "descriptor": str(vnf_descriptor)[:60000] |
| 928 | } |
| 929 | |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 930 | for vnfd_descriptor in vnfd_descriptor_list: |
| 931 | if vnfd_descriptor["id"] == str(vnfd["id"]): |
| 932 | break |
| 933 | |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 934 | # table ip_profiles (ip-profiles) |
| 935 | ip_profile_name2db_table_index = {} |
| 936 | for ip_profile in vnfd.get("ip-profiles").itervalues(): |
| 937 | db_ip_profile = { |
| 938 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 939 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 940 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 941 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 942 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 943 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 944 | } |
| 945 | dns_list = [] |
| 946 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 947 | dns_list.append(str(dns.get("address"))) |
| 948 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 949 | if ip_profile["ip-profile-params"].get('security-group'): |
| 950 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 951 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 952 | db_ip_profiles_index += 1 |
| 953 | db_ip_profiles.append(db_ip_profile) |
| 954 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 955 | # table nets (internal-vld) |
| 956 | net_id2uuid = {} # for mapping interface with network |
| 957 | for vld in vnfd.get("internal-vld").itervalues(): |
| 958 | net_uuid = str(uuid4()) |
| 959 | uuid_list.append(net_uuid) |
| 960 | db_net = { |
| 961 | "name": get_str(vld, "name", 255), |
| 962 | "vnf_id": vnf_uuid, |
| 963 | "uuid": net_uuid, |
| 964 | "description": get_str(vld, "description", 255), |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 965 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 966 | "type": "bridge", # TODO adjust depending on connection point type |
| 967 | } |
| 968 | net_id2uuid[vld.get("id")] = net_uuid |
| 969 | db_nets.append(db_net) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 970 | # ip-profile, link db_ip_profile with db_sce_net |
| 971 | if vld.get("ip-profile-ref"): |
| 972 | ip_profile_name = vld.get("ip-profile-ref") |
| 973 | if ip_profile_name not in ip_profile_name2db_table_index: |
| 974 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':" |
| 975 | "'{}'. Reference to a non-existing 'ip_profiles'".format( |
| 976 | str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 977 | httperrors.Bad_Request) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 978 | db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid |
| 979 | else: #check no ip-address has been defined |
| tierno | 45140f5 | 2018-03-26 12:11:46 +0200 | [diff] [blame] | 980 | for icp in vld.get("internal-connection-point").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 981 | if icp.get("ip-address"): |
| 982 | raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' " |
| 983 | "contains an ip-address but no ip-profile has been defined at VLD".format( |
| 984 | str(vnfd["id"]), str(vld["id"]), str(icp["id"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 985 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 986 | |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 987 | # connection points vaiable declaration |
| 988 | cp_name2iface_uuid = {} |
| 989 | cp_name2vm_uuid = {} |
| 990 | cp_name2db_interface = {} |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 991 | 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] | 992 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 993 | # table vms (vdus) |
| 994 | vdu_id2uuid = {} |
| 995 | vdu_id2db_table_index = {} |
| 996 | for vdu in vnfd.get("vdu").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 997 | |
| 998 | for vdu_descriptor in vnfd_descriptor["vdu"]: |
| 999 | if vdu_descriptor["id"] == str(vdu["id"]): |
| 1000 | break |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1001 | vm_uuid = str(uuid4()) |
| 1002 | uuid_list.append(vm_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1003 | vdu_id = get_str(vdu, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1004 | db_vm = { |
| 1005 | "uuid": vm_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1006 | "osm_id": vdu_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1007 | "name": get_str(vdu, "name", 255), |
| 1008 | "description": get_str(vdu, "description", 255), |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1009 | "pdu_type": get_str(vdu, "pdu-type", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1010 | "vnf_id": vnf_uuid, |
| 1011 | } |
| 1012 | vdu_id2uuid[db_vm["osm_id"]] = vm_uuid |
| 1013 | vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index |
| 1014 | if vdu.get("count"): |
| 1015 | db_vm["count"] = int(vdu["count"]) |
| 1016 | |
| 1017 | # table image |
| 1018 | image_present = False |
| 1019 | if vdu.get("image"): |
| 1020 | image_present = True |
| 1021 | db_image = {} |
| 1022 | image_uuid = _lookfor_or_create_image(db_image, mydb, vdu) |
| 1023 | if not image_uuid: |
| 1024 | image_uuid = db_image["uuid"] |
| 1025 | db_images.append(db_image) |
| 1026 | db_vm["image_id"] = image_uuid |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 1027 | if vdu.get("alternative-images"): |
| 1028 | vm_alternative_images = [] |
| 1029 | for alt_image in vdu.get("alternative-images").itervalues(): |
| 1030 | db_image = {} |
| 1031 | image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image) |
| 1032 | if not image_uuid: |
| 1033 | image_uuid = db_image["uuid"] |
| 1034 | db_images.append(db_image) |
| 1035 | vm_alternative_images.append({ |
| 1036 | "image_id": image_uuid, |
| 1037 | "vim_type": str(alt_image["vim-type"]), |
| 1038 | # "universal_name": str(alt_image["image"]), |
| 1039 | # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None |
| 1040 | }) |
| 1041 | |
| 1042 | 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] | 1043 | |
| 1044 | # volumes |
| 1045 | devices = [] |
| 1046 | if vdu.get("volumes"): |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1047 | for volume_key in vdu["volumes"]: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1048 | volume = vdu["volumes"][volume_key] |
| 1049 | if not image_present: |
| 1050 | # Convert the first volume to vnfc.image |
| 1051 | image_present = True |
| 1052 | db_image = {} |
| 1053 | image_uuid = _lookfor_or_create_image(db_image, mydb, volume) |
| 1054 | if not image_uuid: |
| 1055 | image_uuid = db_image["uuid"] |
| 1056 | db_images.append(db_image) |
| 1057 | db_vm["image_id"] = image_uuid |
| 1058 | else: |
| 1059 | # Add Openmano devices |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1060 | device = {"name": str(volume.get("name"))} |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1061 | device["type"] = str(volume.get("device-type")) |
| 1062 | if volume.get("size"): |
| 1063 | device["size"] = int(volume["size"]) |
| 1064 | if volume.get("image"): |
| 1065 | device["image name"] = str(volume["image"]) |
| 1066 | if volume.get("image-checksum"): |
| 1067 | device["image checksum"] = str(volume["image-checksum"]) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1068 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1069 | devices.append(device) |
| 1070 | |
| tierno | 89aada4 | 2018-12-19 16:00:25 +0000 | [diff] [blame] | 1071 | if not db_vm.get("image_id"): |
| 1072 | if not db_vm["pdu_type"]: |
| 1073 | raise NfvoException("Not defined image for VDU") |
| 1074 | # create a fake image |
| 1075 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1076 | # cloud-init |
| 1077 | boot_data = {} |
| 1078 | if vdu.get("cloud-init"): |
| 1079 | boot_data["user-data"] = str(vdu["cloud-init"]) |
| 1080 | elif vdu.get("cloud-init-file"): |
| 1081 | # TODO Where this file content is present??? |
| 1082 | # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]] |
| 1083 | boot_data["user-data"] = str(vdu["cloud-init-file"]) |
| 1084 | |
| 1085 | if vdu.get("supplemental-boot-data"): |
| 1086 | if vdu["supplemental-boot-data"].get('boot-data-drive'): |
| 1087 | boot_data['boot-data-drive'] = True |
| 1088 | if vdu["supplemental-boot-data"].get('config-file'): |
| 1089 | om_cfgfile_list = list() |
| 1090 | for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues(): |
| 1091 | # TODO Where this file content is present??? |
| 1092 | cfg_source = str(custom_config_file["source"]) |
| 1093 | om_cfgfile_list.append({"dest": custom_config_file["dest"], |
| 1094 | "content": cfg_source}) |
| 1095 | boot_data['config-files'] = om_cfgfile_list |
| 1096 | if boot_data: |
| 1097 | db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256) |
| 1098 | |
| 1099 | db_vms.append(db_vm) |
| 1100 | db_vms_index += 1 |
| 1101 | |
| 1102 | # table interfaces (internal/external interfaces) |
| 1103 | flavor_epa_interfaces = [] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1104 | # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()): |
| 1105 | for iface in vdu.get("interface").itervalues(): |
| 1106 | flavor_epa_interface = {} |
| 1107 | iface_uuid = str(uuid4()) |
| 1108 | uuid_list.append(iface_uuid) |
| 1109 | db_interface = { |
| 1110 | "uuid": iface_uuid, |
| 1111 | "internal_name": get_str(iface, "name", 255), |
| 1112 | "vm_id": vm_uuid, |
| 1113 | } |
| 1114 | flavor_epa_interface["name"] = db_interface["internal_name"] |
| 1115 | if iface.get("virtual-interface").get("vpci"): |
| 1116 | db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12) |
| 1117 | flavor_epa_interface["vpci"] = db_interface["vpci"] |
| 1118 | |
| 1119 | if iface.get("virtual-interface").get("bandwidth"): |
| 1120 | bps = int(iface.get("virtual-interface").get("bandwidth")) |
| 1121 | db_interface["bw"] = int(math.ceil(bps/1000000.0)) |
| 1122 | flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"]) |
| 1123 | |
| 1124 | if iface.get("virtual-interface").get("type") == "OM-MGMT": |
| 1125 | db_interface["type"] = "mgmt" |
| garciadeblas | 31e141b | 2018-10-25 18:33:19 +0200 | [diff] [blame] | 1126 | elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"): |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1127 | db_interface["type"] = "bridge" |
| 1128 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1129 | elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 1130 | db_interface["type"] = "data" |
| 1131 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1132 | flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \ |
| 1133 | else "yes" |
| 1134 | flavor_epa_interfaces.append(flavor_epa_interface) |
| 1135 | else: |
| 1136 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual" |
| 1137 | "-interface':'type':'{}'. Interface type is not supported".format( |
| 1138 | vnfd_id, vdu_id, iface.get("virtual-interface").get("type")), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1139 | httperrors.Bad_Request) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1140 | |
| tierno | e72710b | 2018-07-23 16:16:00 +0200 | [diff] [blame] | 1141 | if iface.get("mgmt-interface"): |
| 1142 | db_interface["type"] = "mgmt" |
| 1143 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1144 | if iface.get("external-connection-point-ref"): |
| 1145 | try: |
| 1146 | cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")] |
| 1147 | db_interface["external_name"] = get_str(cp, "name", 255) |
| 1148 | cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid |
| 1149 | cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid |
| 1150 | cp_name2db_interface[db_interface["external_name"]] = db_interface |
| 1151 | for cp_descriptor in vnfd_descriptor["connection-point"]: |
| 1152 | if cp_descriptor["name"] == db_interface["external_name"]: |
| 1153 | break |
| 1154 | else: |
| 1155 | raise KeyError() |
| 1156 | |
| 1157 | if vdu_id in vdu_id2cp_name: |
| 1158 | vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU |
| 1159 | else: |
| 1160 | vdu_id2cp_name[vdu_id] = db_interface["external_name"] |
| 1161 | |
| 1162 | # port security |
| 1163 | if str(cp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1164 | db_interface["port_security"] = 0 |
| 1165 | elif str(cp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1166 | db_interface["port_security"] = 1 |
| 1167 | except KeyError: |
| 1168 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| 1169 | "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present" |
| 1170 | " at connection-point".format( |
| 1171 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| 1172 | cp=iface.get("vnfd-connection-point-ref")), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1173 | httperrors.Bad_Request) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1174 | elif iface.get("internal-connection-point-ref"): |
| 1175 | try: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1176 | for icp_descriptor in vdu_descriptor["internal-connection-point"]: |
| 1177 | if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")): |
| 1178 | break |
| 1179 | else: |
| 1180 | raise KeyError("does not exist at vdu:internal-connection-point") |
| 1181 | icp = None |
| 1182 | icp_vld = None |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1183 | for vld in vnfd.get("internal-vld").itervalues(): |
| 1184 | for cp in vld.get("internal-connection-point").itervalues(): |
| 1185 | if cp.get("id-ref") == iface.get("internal-connection-point-ref"): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1186 | if icp: |
| 1187 | raise KeyError("is referenced by more than one 'internal-vld'") |
| 1188 | icp = cp |
| 1189 | icp_vld = vld |
| 1190 | if not icp: |
| 1191 | raise KeyError("is not referenced by any 'internal-vld'") |
| 1192 | |
| 1193 | db_interface["net_id"] = net_id2uuid[icp_vld.get("id")] |
| 1194 | if str(icp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1195 | db_interface["port_security"] = 0 |
| 1196 | elif str(icp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1197 | db_interface["port_security"] = 1 |
| 1198 | if icp.get("ip-address"): |
| 1199 | if not icp_vld.get("ip-profile-ref"): |
| 1200 | raise NfvoException |
| 1201 | db_interface["ip_address"] = str(icp.get("ip-address")) |
| 1202 | except KeyError as e: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1203 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1204 | "'interface[{iface}]':'internal-connection-point-ref':'{cp}'" |
| 1205 | " {msg}".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1206 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1207 | cp=iface.get("internal-connection-point-ref"), msg=str(e)), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1208 | httperrors.Bad_Request) |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 1209 | if iface.get("position"): |
| 1210 | db_interface["created_at"] = int(iface.get("position")) * 50 |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1211 | if iface.get("mac-address"): |
| 1212 | db_interface["mac"] = str(iface.get("mac-address")) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1213 | db_interfaces.append(db_interface) |
| 1214 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1215 | # table flavors |
| 1216 | db_flavor = { |
| 1217 | "name": get_str(vdu, "name", 250) + "-flv", |
| 1218 | "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)), |
| 1219 | "ram": int(vdu["vm-flavor"].get("memory-mb", 1)), |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1220 | "disk": int(vdu["vm-flavor"].get("storage-gb", 0)), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1221 | } |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1222 | # TODO revise the case of several numa-node-policy node |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1223 | extended = {} |
| 1224 | numa = {} |
| 1225 | if devices: |
| 1226 | extended["devices"] = devices |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1227 | if flavor_epa_interfaces: |
| 1228 | numa["interfaces"] = flavor_epa_interfaces |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1229 | if vdu.get("guest-epa"): # TODO or dedicated_int: |
| 1230 | epa_vcpu_set = False |
| 1231 | if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int: |
| 1232 | numa_node_policy = vdu["guest-epa"].get("numa-node-policy") |
| 1233 | if numa_node_policy.get("node"): |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1234 | numa_node = numa_node_policy["node"].values()[0] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1235 | if numa_node.get("num-cores"): |
| 1236 | numa["cores"] = numa_node["num-cores"] |
| 1237 | epa_vcpu_set = True |
| 1238 | if numa_node.get("paired-threads"): |
| 1239 | if numa_node["paired-threads"].get("num-paired-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1240 | numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1241 | epa_vcpu_set = True |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1242 | if len(numa_node["paired-threads"].get("paired-thread-ids")): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1243 | numa["paired-threads-id"] = [] |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1244 | for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues(): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1245 | numa["paired-threads-id"].append( |
| 1246 | (str(pair["thread-a"]), str(pair["thread-b"])) |
| 1247 | ) |
| 1248 | if numa_node.get("num-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1249 | numa["threads"] = int(numa_node["num-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1250 | epa_vcpu_set = True |
| 1251 | if numa_node.get("memory-mb"): |
| 1252 | numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1) |
| 1253 | if vdu["guest-epa"].get("mempage-size"): |
| 1254 | if vdu["guest-epa"]["mempage-size"] != "SMALL": |
| 1255 | numa["memory"] = max(int(db_flavor["ram"] / 1024), 1) |
| 1256 | if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set: |
| 1257 | if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED": |
| 1258 | if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \ |
| 1259 | vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER": |
| 1260 | numa["cores"] = max(db_flavor["vcpus"], 1) |
| 1261 | else: |
| 1262 | numa["threads"] = max(db_flavor["vcpus"], 1) |
| 1263 | if numa: |
| 1264 | extended["numas"] = [numa] |
| 1265 | if extended: |
| 1266 | extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256) |
| 1267 | db_flavor["extended"] = extended_text |
| 1268 | # look if flavor exist |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1269 | temp_flavor_dict = {'disk': db_flavor.get('disk', 0), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1270 | 'ram': db_flavor.get('ram'), |
| 1271 | 'vcpus': db_flavor.get('vcpus'), |
| 1272 | 'extended': db_flavor.get('extended') |
| 1273 | } |
| 1274 | existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 1275 | if existing_flavors: |
| 1276 | flavor_uuid = existing_flavors[0]["uuid"] |
| 1277 | else: |
| 1278 | flavor_uuid = str(uuid4()) |
| 1279 | uuid_list.append(flavor_uuid) |
| 1280 | db_flavor["uuid"] = flavor_uuid |
| 1281 | db_flavors.append(db_flavor) |
| 1282 | db_vm["flavor_id"] = flavor_uuid |
| 1283 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1284 | # VNF affinity and antiaffinity |
| 1285 | for pg in vnfd.get("placement-groups").itervalues(): |
| 1286 | pg_name = get_str(pg, "name", 255) |
| 1287 | for vdu in pg.get("member-vdus").itervalues(): |
| 1288 | vdu_id = get_str(vdu, "member-vdu-ref", 255) |
| 1289 | if vdu_id not in vdu_id2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1290 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':" |
| 1291 | "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1292 | vnf=vnfd_id, pg=pg_name, vdu=vdu_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1293 | httperrors.Bad_Request) |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1294 | if vdu_id2db_table_index[vdu_id]: |
| 1295 | db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1296 | # TODO consider the case of isolation and not colocation |
| 1297 | # if pg.get("strategy") == "ISOLATION": |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1298 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1299 | # VNF mgmt configuration |
| 1300 | mgmt_access = {} |
| 1301 | if vnfd["mgmt-interface"].get("vdu-id"): |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1302 | mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255) |
| 1303 | if mgmt_vdu_id not in vdu_id2uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1304 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':" |
| 1305 | "'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1306 | vnf=vnfd_id, vdu=mgmt_vdu_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1307 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1308 | mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1309 | # if only one cp is defined by this VDU, mark this interface as of type "mgmt" |
| 1310 | if vdu_id2cp_name.get(mgmt_vdu_id): |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1311 | if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]: |
| 1312 | cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt" |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1313 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1314 | if vnfd["mgmt-interface"].get("ip-address"): |
| 1315 | mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address")) |
| 1316 | if vnfd["mgmt-interface"].get("cp"): |
| 1317 | if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid: |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1318 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. " |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1319 | "Reference to a non-existing connection-point".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1320 | vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1321 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1322 | mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]] |
| 1323 | mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]] |
| tierno | e2ff1ce | 2017-11-02 17:01:10 +0100 | [diff] [blame] | 1324 | # mark this interface as of type mgmt |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 1325 | if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]: |
| 1326 | cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt" |
| tierno | e2ff1ce | 2017-11-02 17:01:10 +0100 | [diff] [blame] | 1327 | |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 1328 | 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] | 1329 | "default-user", 64) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1330 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1331 | if default_user: |
| 1332 | mgmt_access["default_user"] = default_user |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1333 | required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}), |
| 1334 | "required", 6) |
| 1335 | if required: |
| 1336 | mgmt_access["required"] = required |
| 1337 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1338 | if mgmt_access: |
| 1339 | db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256) |
| 1340 | |
| 1341 | db_vnfs.append(db_vnf) |
| 1342 | db_tables=[ |
| 1343 | {"vnfs": db_vnfs}, |
| 1344 | {"nets": db_nets}, |
| 1345 | {"images": db_images}, |
| 1346 | {"flavors": db_flavors}, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1347 | {"ip_profiles": db_ip_profiles}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1348 | {"vms": db_vms}, |
| 1349 | {"interfaces": db_interfaces}, |
| 1350 | ] |
| 1351 | |
| 1352 | logger.debug("create_vnf Deployment done vnfDict: %s", |
| 1353 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 1354 | mydb.new_rows(db_tables, uuid_list) |
| 1355 | return vnfd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1356 | except NfvoException: |
| 1357 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1358 | except Exception as e: |
| 1359 | logger.error("Exception {}".format(e)) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1360 | raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1361 | |
| 1362 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1363 | @deprecated("Use new_vnfd_v3") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1364 | def new_vnf(mydb, tenant_id, vnf_descriptor): |
| 1365 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1366 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1367 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1368 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1369 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1370 | vims = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1371 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1372 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1373 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1374 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1375 | 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] | 1376 | httperrors.Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1377 | else: |
| 1378 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1379 | # 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] | 1380 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1381 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1382 | |
| 1383 | # Step 4. Review the descriptor and add missing fields |
| 1384 | #print vnf_descriptor |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1385 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1386 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1387 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1388 | if "physical" in vnf_descriptor['vnf']: |
| 1389 | del vnf_descriptor['vnf']['physical'] |
| 1390 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1391 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1392 | # 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] | 1393 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1394 | 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] | 1395 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1396 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1397 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1398 | 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] | 1399 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1400 | 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] | 1401 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1402 | VNFCitem={} |
| 1403 | VNFCitem["name"] = vnfc['name'] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1404 | VNFCitem["availability_zone"] = vnfc.get('availability_zone') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1405 | 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] | 1406 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1407 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1408 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1409 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1410 | 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] | 1411 | myflavorDict["description"] = VNFCitem["description"] |
| 1412 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1413 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1414 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1415 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1416 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1417 | devices = vnfc.get("devices") |
| 1418 | if devices != None: |
| 1419 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1420 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1421 | # TODO: |
| 1422 | # 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] | 1423 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1424 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1425 | # Previous code has been commented |
| 1426 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1427 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1428 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1429 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1430 | #else: |
| 1431 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1432 | # if result2: |
| 1433 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1434 | # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1435 | # else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1436 | # 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] | 1437 | 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] | 1438 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1439 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1440 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1441 | |
| 1442 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1443 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1444 | # Step 6.2 New flavors are created in the VIM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1445 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1446 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1447 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1448 | VNFCitem["flavor_id"] = flavor_id |
| 1449 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1450 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1451 | logger.debug("Creating new images in the VIM for each VNFC") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1452 | # Step 6.3 New images are created in the VIM |
| 1453 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1454 | #This "for" loop might be integrated with the previous one |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1455 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1456 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1457 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1458 | image_dict={} |
| 1459 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1460 | image_dict['universal_name']=vnfc.get('image name') |
| 1461 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1462 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1463 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1464 | image_dict['checksum']=vnfc.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1465 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1466 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1467 | if image_metadata_dict is not None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1468 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1469 | image_dict['metadata']=image_metadata_str |
| 1470 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1471 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1472 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1473 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1474 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1475 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1476 | if vnfc.get("boot-data"): |
| 1477 | 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] | 1478 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1479 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1480 | # Step 7. Storing the VNF descriptor in the repository |
| 1481 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1482 | 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] | 1483 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1484 | # Step 8. Adding the VNF to the NFVO DB |
| 1485 | vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1486 | return vnf_id |
| 1487 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1488 | _, message = rollback(mydb, vims, rollback_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1489 | if isinstance(e, db_base_Exception): |
| 1490 | error_text = "Exception at database" |
| 1491 | elif isinstance(e, KeyError): |
| 1492 | error_text = "KeyError exception " |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1493 | e.http_code = httperrors.Internal_Server_Error |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1494 | else: |
| 1495 | error_text = "Exception at VIM" |
| 1496 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1497 | #logger.error("start_scenario %s", error_text) |
| 1498 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1499 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1500 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1501 | @deprecated("Use new_vnfd_v3") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1502 | def new_vnf_v02(mydb, tenant_id, vnf_descriptor): |
| 1503 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1504 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1505 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1506 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1507 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1508 | vims = {} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1509 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1510 | check_tenant(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1511 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1512 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| 1513 | 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] | 1514 | httperrors.Unauthorized) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1515 | else: |
| 1516 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1517 | # 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] | 1518 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1519 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1520 | |
| 1521 | # Step 4. Review the descriptor and add missing fields |
| 1522 | #print vnf_descriptor |
| 1523 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| 1524 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1525 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1526 | if "physical" in vnf_descriptor['vnf']: |
| 1527 | del vnf_descriptor['vnf']['physical'] |
| 1528 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1529 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1530 | # 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] | 1531 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1532 | 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] | 1533 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1534 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1535 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1536 | rollback_list = [] # It will contain the new images created in mano. It is used for rollback |
| 1537 | try: |
| 1538 | logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC") |
| 1539 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1540 | VNFCitem={} |
| 1541 | VNFCitem["name"] = vnfc['name'] |
| 1542 | 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] | 1543 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1544 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1545 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1546 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1547 | 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] | 1548 | myflavorDict["description"] = VNFCitem["description"] |
| 1549 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1550 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1551 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1552 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1553 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1554 | devices = vnfc.get("devices") |
| 1555 | if devices != None: |
| 1556 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1557 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1558 | # TODO: |
| 1559 | # 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] | 1560 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1561 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1562 | # Previous code has been commented |
| 1563 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1564 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1565 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1566 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1567 | #else: |
| 1568 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1569 | # if result2: |
| 1570 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1571 | # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1572 | # else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1573 | # 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] | 1574 | 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] | 1575 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1576 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1577 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1578 | |
| 1579 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1580 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1581 | # Step 6.2 New flavors are created in the VIM |
| 1582 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| 1583 | |
| 1584 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| 1585 | VNFCitem["flavor_id"] = flavor_id |
| 1586 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1587 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1588 | logger.debug("Creating new images in the VIM for each VNFC") |
| 1589 | # Step 6.3 New images are created in the VIM |
| 1590 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1591 | #This "for" loop might be integrated with the previous one |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1592 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1593 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1594 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1595 | image_dict={} |
| 1596 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1597 | image_dict['universal_name']=vnfc.get('image name') |
| 1598 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1599 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1600 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1601 | image_dict['checksum']=vnfc.get('image checksum') |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1602 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1603 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1604 | if image_metadata_dict is not None: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1605 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1606 | image_dict['metadata']=image_metadata_str |
| 1607 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| 1608 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1609 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| 1610 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1611 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1612 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1613 | if vnfc.get("boot-data"): |
| 1614 | 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] | 1615 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1616 | # Step 7. Storing the VNF descriptor in the repository |
| 1617 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1618 | 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] | 1619 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1620 | # Step 8. Adding the VNF to the NFVO DB |
| 1621 | vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1622 | return vnf_id |
| 1623 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| 1624 | _, message = rollback(mydb, vims, rollback_list) |
| 1625 | if isinstance(e, db_base_Exception): |
| 1626 | error_text = "Exception at database" |
| 1627 | elif isinstance(e, KeyError): |
| 1628 | error_text = "KeyError exception " |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1629 | e.http_code = httperrors.Internal_Server_Error |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1630 | else: |
| 1631 | error_text = "Exception at VIM" |
| 1632 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1633 | #logger.error("start_scenario %s", error_text) |
| 1634 | raise NfvoException(error_text, e.http_code) |
| 1635 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1636 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1637 | def get_vnf_id(mydb, tenant_id, vnf_id): |
| 1638 | #check valid tenant_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1639 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1640 | #obtain data |
| 1641 | where_or = {} |
| 1642 | if tenant_id != "any": |
| 1643 | where_or["tenant_id"] = tenant_id |
| 1644 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1645 | vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") |
| 1646 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1647 | vnf_id = vnf["uuid"] |
| 1648 | filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1649 | 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] | 1650 | #change_keys_http2db(filtered_content, http2db_vnf, reverse=True) |
| 1651 | data={'vnf' : filtered_content} |
| 1652 | #GET VM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1653 | 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] | 1654 | SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description', |
| 1655 | 'boot_data'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1656 | WHERE={'vnfs.uuid': vnf_id} ) |
| gcalvino | bfa2fd9 | 2018-11-13 18:47:28 +0100 | [diff] [blame] | 1657 | if len(content) != 0: |
| Anderson Bravalheri | c5293de | 2018-11-28 17:21:26 +0000 | [diff] [blame] | 1658 | #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1659 | # change boot_data into boot-data |
| gcalvino | 319b8a5 | 2018-11-05 15:33:23 +0100 | [diff] [blame] | 1660 | for vm in content: |
| 1661 | if vm.get("boot_data"): |
| 1662 | vm["boot-data"] = yaml.safe_load(vm["boot_data"]) |
| 1663 | del vm["boot_data"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1664 | |
| gcalvino | bfa2fd9 | 2018-11-13 18:47:28 +0100 | [diff] [blame] | 1665 | data['vnf']['VNFC'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1666 | #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] | 1667 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1668 | #GET NET |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1669 | 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] | 1670 | SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'), |
| 1671 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1672 | data['vnf']['nets'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1673 | |
| 1674 | #GET ip-profile for each net |
| 1675 | for net in data['vnf']['nets']: |
| 1676 | ipprofiles = mydb.get_rows(FROM='ip_profiles', |
| 1677 | SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'), |
| 1678 | WHERE={'net_id': net["uuid"]} ) |
| 1679 | if len(ipprofiles)==1: |
| 1680 | net["ip_profile"] = ipprofiles[0] |
| 1681 | elif len(ipprofiles)>1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1682 | 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] | 1683 | |
| 1684 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1685 | #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] | 1686 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1687 | #GET External Interfaces |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1688 | 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] | 1689 | SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \ |
| 1690 | '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] | 1691 | WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1692 | #print content |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1693 | data['vnf']['external-connections'] = content |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1694 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1695 | return data |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1696 | |
| 1697 | |
| 1698 | def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None): |
| 1699 | # Check tenant exist |
| 1700 | if tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1701 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1702 | # Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1703 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1704 | else: |
| 1705 | vims={} |
| 1706 | |
| 1707 | # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided" |
| 1708 | where_or = {} |
| 1709 | if tenant_id != "any": |
| 1710 | where_or["tenant_id"] = tenant_id |
| 1711 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1712 | 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] | 1713 | vnf_id = vnf["uuid"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1714 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1715 | # "Getting the list of flavors and tenants of the VNF" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1716 | flavorList = get_flavorlist(mydb, vnf_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1717 | if len(flavorList)==0: |
| 1718 | 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] | 1719 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1720 | imageList = get_imagelist(mydb, vnf_id) |
| 1721 | if len(imageList)==0: |
| 1722 | 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] | 1723 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1724 | deleted = mydb.delete_row_by_id('vnfs', vnf_id) |
| 1725 | if deleted == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1726 | raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1727 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1728 | undeletedItems = [] |
| 1729 | for flavor in flavorList: |
| 1730 | #check if flavor is used by other vnf |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1731 | try: |
| 1732 | c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} ) |
| 1733 | if len(c) > 0: |
| 1734 | logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor) |
| 1735 | continue |
| 1736 | #flavor not used, must be deleted |
| 1737 | #delelte at VIM |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1738 | c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1739 | for flavor_vim in c: |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1740 | if not flavor_vim['created']: # skip this flavor because not created by openmano |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1741 | continue |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1742 | # look for vim |
| 1743 | myvim = None |
| 1744 | for vim in vims.values(): |
| 1745 | if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]: |
| 1746 | myvim = vim |
| 1747 | break |
| 1748 | if not myvim: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1749 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1750 | try: |
| 1751 | myvim.delete_flavor(flavor_vim["vim_id"]) |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1752 | except vimconn.vimconnNotFoundException: |
| 1753 | logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], |
| 1754 | flavor_vim["datacenter_vim_id"] ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1755 | except vimconn.vimconnException as e: |
| 1756 | 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] | 1757 | flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e)) |
| 1758 | undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], |
| 1759 | flavor_vim["datacenter_vim_id"])) |
| 1760 | # 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] | 1761 | mydb.delete_row_by_id('flavors', flavor) |
| 1762 | except db_base_Exception as e: |
| 1763 | 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] | 1764 | undeletedItems.append("flavor {}".format(flavor)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1765 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1766 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1767 | for image in imageList: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1768 | try: |
| 1769 | #check if image is used by other vnf |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 1770 | 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] | 1771 | if len(c) > 0: |
| 1772 | logger.debug("Image '%s' not deleted because it is being used by another VNF", image) |
| 1773 | continue |
| 1774 | #image not used, must be deleted |
| 1775 | #delelte at VIM |
| 1776 | c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1777 | for image_vim in c: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 1778 | 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] | 1779 | continue |
| 1780 | if image_vim['created']=='false': #skip this image because not created by openmano |
| 1781 | continue |
| 1782 | myvim=vims[ image_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1783 | try: |
| 1784 | myvim.delete_image(image_vim["vim_id"]) |
| 1785 | except vimconn.vimconnNotFoundException as e: |
| 1786 | logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] ) |
| 1787 | except vimconn.vimconnException as e: |
| 1788 | logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s", |
| 1789 | image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1790 | 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] | 1791 | #delete image from Database, using table images and with cascade foreign key also at datacenters_images |
| 1792 | mydb.delete_row_by_id('images', image) |
| 1793 | except db_base_Exception as e: |
| 1794 | 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] | 1795 | undeletedItems.append("image %s" % image) |
| 1796 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1797 | return vnf_id + " " + vnf["name"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1798 | #if undeletedItems: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1799 | # return "delete_vnf. Undeleted: %s" %(undeletedItems) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1800 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1801 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1802 | @deprecated("Not used") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1803 | def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None): |
| 1804 | result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name) |
| 1805 | if result < 0: |
| 1806 | return result, vims |
| 1807 | elif result == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1808 | return -httperrors.Not_Found, "datacenter '%s' not found" % datacenter_name |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1809 | myvim = vims.values()[0] |
| 1810 | result,servers = myvim.get_hosts_info() |
| 1811 | if result < 0: |
| 1812 | return result, servers |
| 1813 | topology = {'name':myvim['name'] , 'servers': servers} |
| 1814 | return result, topology |
| 1815 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1816 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1817 | def get_hosts(mydb, nfvo_tenant_id): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1818 | vims = get_vim(mydb, nfvo_tenant_id) |
| 1819 | if len(vims) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1820 | 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] | 1821 | elif len(vims)>1: |
| 1822 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1823 | 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] | 1824 | myvim = vims.values()[0] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1825 | try: |
| 1826 | hosts = myvim.get_hosts() |
| 1827 | 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] | 1828 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1829 | datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] } |
| 1830 | for host in hosts: |
| 1831 | server={'name':host['name'], 'vms':[]} |
| 1832 | for vm in host['instances']: |
| 1833 | #get internal name and model |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1834 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1835 | c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\ |
| 1836 | WHERE={'vim_vm_id':vm['id']} ) |
| 1837 | if len(c) == 0: |
| 1838 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id'])) |
| 1839 | continue |
| 1840 | server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1841 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1842 | except db_base_Exception as e: |
| 1843 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e))) |
| 1844 | datacenter['Datacenters'][0]['servers'].append(server) |
| 1845 | #return -400, "en construccion" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1846 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1847 | #print 'datacenters '+ json.dumps(datacenter, indent=4) |
| 1848 | return datacenter |
| 1849 | except vimconn.vimconnException as e: |
| 1850 | 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] | 1851 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1852 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1853 | @deprecated("Use new_nsd_v3") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1854 | def new_scenario(mydb, tenant_id, topo): |
| 1855 | |
| 1856 | # result, vims = get_vim(mydb, tenant_id) |
| 1857 | # if result < 0: |
| 1858 | # return result, vims |
| 1859 | #1: parse input |
| 1860 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1861 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1862 | if "tenant_id" in topo: |
| 1863 | if topo["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1864 | 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] | 1865 | httperrors.Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1866 | else: |
| 1867 | tenant_id=None |
| 1868 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1869 | #1.1: get VNFs and external_networks (other_nets). |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1870 | vnfs={} |
| 1871 | other_nets={} #external_networks, bridge_networks and data_networkds |
| 1872 | nodes = topo['topology']['nodes'] |
| 1873 | for k in nodes.keys(): |
| 1874 | if nodes[k]['type'] == 'VNF': |
| 1875 | vnfs[k] = nodes[k] |
| 1876 | vnfs[k]['ifaces'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1877 | elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1878 | other_nets[k] = nodes[k] |
| 1879 | other_nets[k]['external']=True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1880 | elif nodes[k]['type'] == 'network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1881 | other_nets[k] = nodes[k] |
| 1882 | other_nets[k]['external']=False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1883 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1884 | |
| 1885 | #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces |
| 1886 | for name,vnf in vnfs.items(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1887 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1888 | error_text = "" |
| 1889 | error_pos = "'topology':'nodes':'" + name + "'" |
| 1890 | if 'vnf_id' in vnf: |
| 1891 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1892 | where['uuid'] = vnf['vnf_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1893 | if 'VNF model' in vnf: |
| 1894 | error_text += " 'VNF model' " + vnf['VNF model'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1895 | where['name'] = vnf['VNF model'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1896 | if len(where) == 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1897 | 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] | 1898 | |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1899 | vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), |
| 1900 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1901 | WHERE=where) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1902 | if len(vnf_db)==0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1903 | raise NfvoException("unknown" + error_text + " at " + error_pos, httperrors.Not_Found) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1904 | elif len(vnf_db)>1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1905 | 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] | 1906 | vnf['uuid']=vnf_db[0]['uuid'] |
| 1907 | vnf['description']=vnf_db[0]['description'] |
| 1908 | #get external interfaces |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1909 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), |
| 1910 | 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] | 1911 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1912 | for ext_iface in ext_ifaces: |
| 1913 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']} |
| 1914 | |
| 1915 | #1.4 get list of connections |
| 1916 | conections = topo['topology']['connections'] |
| 1917 | conections_list = [] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1918 | conections_list_name = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1919 | for k in conections.keys(): |
| 1920 | if type(conections[k]['nodes'])==dict: #dict with node:iface pairs |
| 1921 | ifaces_list = conections[k]['nodes'].items() |
| 1922 | elif type(conections[k]['nodes'])==list: #list with dictionary |
| 1923 | ifaces_list=[] |
| 1924 | conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] ) |
| 1925 | for k2 in conection_pair_list: |
| 1926 | ifaces_list += k2 |
| 1927 | |
| 1928 | con_type = conections[k].get("type", "link") |
| 1929 | if con_type != "link": |
| 1930 | if k in other_nets: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1931 | 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] | 1932 | other_nets[k] = {'external': False} |
| 1933 | if conections[k].get("graph"): |
| 1934 | other_nets[k]["graph"] = conections[k]["graph"] |
| 1935 | ifaces_list.append( (k, None) ) |
| 1936 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1937 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1938 | if con_type == "external_network": |
| 1939 | other_nets[k]['external'] = True |
| 1940 | if conections[k].get("model"): |
| 1941 | other_nets[k]["model"] = conections[k]["model"] |
| 1942 | else: |
| 1943 | other_nets[k]["model"] = k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1944 | if con_type == "dataplane_net" or con_type == "bridge_net": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1945 | other_nets[k]["model"] = con_type |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1946 | |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1947 | conections_list_name.append(k) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1948 | 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) |
| 1949 | #print set(ifaces_list) |
| 1950 | #check valid VNF and iface names |
| 1951 | for iface in ifaces_list: |
| 1952 | if iface[0] not in vnfs and iface[0] not in other_nets : |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1953 | raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1954 | str(k), iface[0]), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1955 | 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] | 1956 | raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1957 | str(k), iface[0], iface[1]), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1958 | |
| 1959 | #1.5 unify connections from the pair list to a consolidated list |
| 1960 | index=0 |
| 1961 | while index < len(conections_list): |
| 1962 | index2 = index+1 |
| 1963 | while index2 < len(conections_list): |
| 1964 | if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets |
| 1965 | conections_list[index] |= conections_list[index2] |
| 1966 | del conections_list[index2] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1967 | del conections_list_name[index2] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1968 | else: |
| 1969 | index2 += 1 |
| 1970 | conections_list[index] = list(conections_list[index]) # from set to list again |
| 1971 | index += 1 |
| 1972 | #for k in conections_list: |
| 1973 | # print k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1974 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1975 | |
| 1976 | |
| 1977 | #1.6 Delete non external nets |
| 1978 | # for k in other_nets.keys(): |
| 1979 | # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net': |
| 1980 | # for con in conections_list: |
| 1981 | # delete_indexes=[] |
| 1982 | # for index in range(0,len(con)): |
| 1983 | # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower |
| 1984 | # for index in delete_indexes: |
| 1985 | # del con[index] |
| 1986 | # del other_nets[k] |
| 1987 | #1.7: Check external_ports are present at database table datacenter_nets |
| 1988 | for k,net in other_nets.items(): |
| 1989 | error_pos = "'topology':'nodes':'" + k + "'" |
| 1990 | if net['external']==False: |
| 1991 | if 'name' not in net: |
| 1992 | net['name']=k |
| 1993 | if 'model' not in net: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 1994 | raise NfvoException("needed a 'model' at " + error_pos, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1995 | if net['model']=='bridge_net': |
| 1996 | net['type']='bridge'; |
| 1997 | elif net['model']=='dataplane_net': |
| 1998 | net['type']='data'; |
| 1999 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2000 | raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2001 | else: #external |
| 2002 | #IF we do not want to check that external network exist at datacenter |
| 2003 | pass |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2004 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2005 | # error_text = "" |
| 2006 | # WHERE_={} |
| 2007 | # if 'net_id' in net: |
| 2008 | # error_text += " 'net_id' " + net['net_id'] |
| 2009 | # WHERE_['uuid'] = net['net_id'] |
| 2010 | # if 'model' in net: |
| 2011 | # error_text += " 'model' " + net['model'] |
| 2012 | # WHERE_['name'] = net['model'] |
| 2013 | # if len(WHERE_) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2014 | # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2015 | # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'), |
| 2016 | # FROM='datacenter_nets', WHERE=WHERE_ ) |
| 2017 | # if r<0: |
| 2018 | # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db |
| 2019 | # elif r==0: |
| 2020 | # print "nfvo.new_scenario Error" +error_text+ " is not present at database" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2021 | # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2022 | # elif r>1: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2023 | # 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] | 2024 | # 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] | 2025 | # other_nets[k].update(net_db[0]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2026 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2027 | net_list={} |
| 2028 | net_nb=0 #Number of nets |
| 2029 | for con in conections_list: |
| 2030 | #check if this is connected to a external net |
| 2031 | other_net_index=-1 |
| 2032 | #print |
| 2033 | #print "con", con |
| 2034 | for index in range(0,len(con)): |
| 2035 | #check if this is connected to a external net |
| 2036 | for net_key in other_nets.keys(): |
| 2037 | if con[index][0]==net_key: |
| 2038 | if other_net_index>=0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2039 | 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] | 2040 | #print "nfvo.new_scenario " + error_text |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2041 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2042 | else: |
| 2043 | other_net_index = index |
| 2044 | net_target = net_key |
| 2045 | break |
| 2046 | #print "other_net_index", other_net_index |
| 2047 | try: |
| 2048 | if other_net_index>=0: |
| 2049 | del con[other_net_index] |
| 2050 | #IF we do not want to check that external network exist at datacenter |
| 2051 | if other_nets[net_target]['external'] : |
| 2052 | if "name" not in other_nets[net_target]: |
| 2053 | other_nets[net_target]['name'] = other_nets[net_target]['model'] |
| 2054 | if other_nets[net_target]["type"] == "external_network": |
| 2055 | if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data": |
| 2056 | other_nets[net_target]["type"] = "data" |
| 2057 | else: |
| 2058 | other_nets[net_target]["type"] = "bridge" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2059 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2060 | # if other_nets[net_target]['external'] : |
| 2061 | # 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 |
| 2062 | # if type_=='data' and other_nets[net_target]['type']=="ptp": |
| 2063 | # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target) |
| 2064 | # print "nfvo.new_scenario " + error_text |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2065 | # return -httperrors.Bad_Request, error_text |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2066 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2067 | for iface in con: |
| 2068 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2069 | else: |
| 2070 | #create a net |
| 2071 | net_type_bridge=False |
| 2072 | net_type_data=False |
| 2073 | net_target = "__-__net"+str(net_nb) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2074 | 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] | 2075 | 'description':"net-%s in scenario %s" %(net_nb,topo['name']), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2076 | 'external':False} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2077 | for iface in con: |
| 2078 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2079 | iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type'] |
| 2080 | if iface_type=='mgmt' or iface_type=='bridge': |
| 2081 | net_type_bridge = True |
| 2082 | else: |
| 2083 | net_type_data = True |
| 2084 | if net_type_bridge and net_type_data: |
| 2085 | 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] | 2086 | #print "nfvo.new_scenario " + error_text |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2087 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2088 | elif net_type_bridge: |
| 2089 | type_='bridge' |
| 2090 | else: |
| 2091 | type_='data' if len(con)>2 else 'ptp' |
| 2092 | net_list[net_target]['type'] = type_ |
| 2093 | net_nb+=1 |
| 2094 | except Exception: |
| 2095 | 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] | 2096 | #print "nfvo.new_scenario " + error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2097 | #raise e |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2098 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2099 | |
| 2100 | #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] | 2101 | #1.8.1 obtain management net |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2102 | mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2103 | FROM='datacenter_nets', WHERE={'name':'mgmt'} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2104 | #1.8.2 check all interfaces from all vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2105 | if len(mgmt_net)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2106 | add_mgmt_net = False |
| 2107 | for vnf in vnfs.values(): |
| 2108 | for iface in vnf['ifaces'].values(): |
| 2109 | if iface['type']=='mgmt' and 'net_key' not in iface: |
| 2110 | #iface not connected |
| 2111 | iface['net_key'] = 'mgmt' |
| 2112 | add_mgmt_net = True |
| 2113 | if add_mgmt_net and 'mgmt' not in net_list: |
| 2114 | net_list['mgmt']=mgmt_net[0] |
| 2115 | net_list['mgmt']['external']=True |
| 2116 | net_list['mgmt']['graph']={'visible':False} |
| 2117 | |
| 2118 | net_list.update(other_nets) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2119 | #print |
| 2120 | #print 'net_list', net_list |
| 2121 | #print |
| 2122 | #print 'vnfs', vnfs |
| 2123 | #print |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2124 | |
| 2125 | #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2126 | c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list, |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2127 | 'tenant_id':tenant_id, 'name':topo['name'], |
| 2128 | 'description':topo.get('description',topo['name']), |
| 2129 | 'public': topo.get('public', False) |
| 2130 | }) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2131 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2132 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2133 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2134 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 2135 | @deprecated("Use new_nsd_v3") |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2136 | def new_scenario_v02(mydb, tenant_id, scenario_dict, version): |
| 2137 | """ This creates a new scenario for version 0.2 and 0.3""" |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2138 | scenario = scenario_dict["scenario"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2139 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2140 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2141 | if "tenant_id" in scenario: |
| 2142 | if scenario["tenant_id"] != tenant_id: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2143 | # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2144 | 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] | 2145 | scenario["tenant_id"], tenant_id), httperrors.Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2146 | else: |
| 2147 | tenant_id=None |
| 2148 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2149 | # 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] | 2150 | for name,vnf in scenario["vnfs"].iteritems(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2151 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2152 | error_text = "" |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 2153 | error_pos = "'scenario':'vnfs':'" + name + "'" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2154 | if 'vnf_id' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2155 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2156 | where['uuid'] = vnf['vnf_id'] |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2157 | if 'vnf_name' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2158 | error_text += " 'vnf_name' " + vnf['vnf_name'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2159 | where['name'] = vnf['vnf_name'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2160 | if len(where) == 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2161 | 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] | 2162 | vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'), |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2163 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2164 | WHERE=where) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2165 | if len(vnf_db) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2166 | raise NfvoException("Unknown" + error_text + " at " + error_pos, httperrors.Not_Found) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2167 | elif len(vnf_db) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2168 | 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] | 2169 | vnf['uuid'] = vnf_db[0]['uuid'] |
| 2170 | vnf['description'] = vnf_db[0]['description'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2171 | vnf['ifaces'] = {} |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2172 | # get external interfaces |
| 2173 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'), |
| 2174 | 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] | 2175 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2176 | for ext_iface in ext_ifaces: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2177 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']} |
| 2178 | # 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] | 2179 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2180 | # 2: Insert net_key and ip_address at every vnf interface |
| 2181 | for net_name, net in scenario["networks"].items(): |
| 2182 | net_type_bridge = False |
| 2183 | net_type_data = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2184 | for iface_dict in net["interfaces"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2185 | if version == "0.2": |
| 2186 | temp_dict = iface_dict |
| 2187 | ip_address = None |
| 2188 | elif version == "0.3": |
| 2189 | temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]} |
| 2190 | ip_address = iface_dict.get('ip_address', None) |
| 2191 | for vnf, iface in temp_dict.items(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2192 | if vnf not in scenario["vnfs"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2193 | error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format( |
| 2194 | net_name, vnf) |
| 2195 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2196 | raise NfvoException(error_text, httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2197 | if iface not in scenario["vnfs"][vnf]['ifaces']: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2198 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\ |
| 2199 | .format(net_name, iface) |
| 2200 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2201 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2202 | if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2203 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\ |
| 2204 | "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key']) |
| 2205 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2206 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2207 | scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2208 | scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2209 | iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type'] |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2210 | if iface_type == 'mgmt' or iface_type == 'bridge': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2211 | net_type_bridge = True |
| 2212 | else: |
| 2213 | net_type_data = True |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2214 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2215 | if net_type_bridge and net_type_data: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2216 | error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\ |
| 2217 | .format(net_name) |
| 2218 | # logger.debug("nfvo.new_scenario " + error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2219 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2220 | elif net_type_bridge: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2221 | type_ = 'bridge' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2222 | else: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2223 | type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp' |
| 2224 | |
| 2225 | if net.get("implementation"): # for v0.3 |
| 2226 | if type_ == "bridge" and net["implementation"] == "underlay": |
| 2227 | error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\ |
| 2228 | "'network':'{}'".format(net_name) |
| 2229 | # logger.debug(error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2230 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2231 | elif type_ != "bridge" and net["implementation"] == "overlay": |
| 2232 | error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\ |
| 2233 | "'network':'{}'".format(net_name) |
| 2234 | # logger.debug(error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2235 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2236 | net.pop("implementation") |
| 2237 | if "type" in net and version == "0.3": # for v0.3 |
| 2238 | if type_ == "data" and net["type"] == "e-line": |
| 2239 | error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\ |
| 2240 | "'e-line' at 'network':'{}'".format(net_name) |
| 2241 | # logger.debug(error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2242 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2243 | elif type_ == "ptp" and net["type"] == "e-lan": |
| 2244 | type_ = "data" |
| 2245 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2246 | net['type'] = type_ |
| 2247 | net['name'] = net_name |
| 2248 | net['external'] = net.get('external', False) |
| 2249 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2250 | # 3: insert at database |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2251 | scenario["nets"] = scenario["networks"] |
| 2252 | scenario['tenant_id'] = tenant_id |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2253 | scenario_id = mydb.new_scenario(scenario) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2254 | return scenario_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2255 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2256 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2257 | def new_nsd_v3(mydb, tenant_id, nsd_descriptor): |
| 2258 | """ |
| 2259 | Parses an OSM IM nsd_catalog and insert at DB |
| 2260 | :param mydb: |
| 2261 | :param tenant_id: |
| 2262 | :param nsd_descriptor: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2263 | :return: The list of created NSD ids |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2264 | """ |
| 2265 | try: |
| 2266 | mynsd = nsd_catalog.nsd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 2267 | try: |
| 2268 | pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd) |
| 2269 | except Exception as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2270 | raise NfvoException("Error. Invalid NS descriptor format: " + str(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2271 | db_scenarios = [] |
| 2272 | db_sce_nets = [] |
| 2273 | db_sce_vnfs = [] |
| 2274 | db_sce_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2275 | db_sce_vnffgs = [] |
| 2276 | db_sce_rsps = [] |
| 2277 | db_sce_rsp_hops = [] |
| 2278 | db_sce_classifiers = [] |
| 2279 | db_sce_classifier_matches = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2280 | db_ip_profiles = [] |
| 2281 | db_ip_profiles_index = 0 |
| 2282 | uuid_list = [] |
| 2283 | nsd_uuid_list = [] |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2284 | for nsd_yang in mynsd.nsd_catalog.nsd.itervalues(): |
| 2285 | nsd = nsd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2286 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2287 | # table scenarios |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2288 | scenario_uuid = str(uuid4()) |
| 2289 | uuid_list.append(scenario_uuid) |
| 2290 | nsd_uuid_list.append(scenario_uuid) |
| 2291 | db_scenario = { |
| 2292 | "uuid": scenario_uuid, |
| 2293 | "osm_id": get_str(nsd, "id", 255), |
| 2294 | "name": get_str(nsd, "name", 255), |
| 2295 | "description": get_str(nsd, "description", 255), |
| 2296 | "tenant_id": tenant_id, |
| 2297 | "vendor": get_str(nsd, "vendor", 255), |
| 2298 | "short_name": get_str(nsd, "short-name", 255), |
| 2299 | "descriptor": str(nsd_descriptor)[:60000], |
| 2300 | } |
| 2301 | db_scenarios.append(db_scenario) |
| 2302 | |
| 2303 | # table sce_vnfs (constituent-vnfd) |
| 2304 | vnf_index2scevnf_uuid = {} |
| 2305 | vnf_index2vnf_uuid = {} |
| 2306 | for vnf in nsd.get("constituent-vnfd").itervalues(): |
| 2307 | existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255], |
| 2308 | 'tenant_id': tenant_id}) |
| 2309 | if not existing_vnf: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2310 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':" |
| 2311 | "'{}'. Reference to a non-existing VNFD in the catalog".format( |
| 2312 | str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2313 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2314 | sce_vnf_uuid = str(uuid4()) |
| 2315 | uuid_list.append(sce_vnf_uuid) |
| 2316 | db_sce_vnf = { |
| 2317 | "uuid": sce_vnf_uuid, |
| 2318 | "scenario_id": scenario_uuid, |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 2319 | # "name": get_str(vnf, "member-vnf-index", 255), |
| 2320 | "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2321 | "vnf_id": existing_vnf[0]["uuid"], |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2322 | "member_vnf_index": str(vnf["member-vnf-index"]), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2323 | # TODO 'start-by-default': True |
| 2324 | } |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2325 | vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid |
| 2326 | vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2327 | db_sce_vnfs.append(db_sce_vnf) |
| 2328 | |
| 2329 | # table ip_profiles (ip-profiles) |
| 2330 | ip_profile_name2db_table_index = {} |
| 2331 | for ip_profile in nsd.get("ip-profiles").itervalues(): |
| 2332 | db_ip_profile = { |
| 2333 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 2334 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 2335 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 2336 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 2337 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 2338 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 2339 | } |
| 2340 | dns_list = [] |
| 2341 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 2342 | dns_list.append(str(dns.get("address"))) |
| 2343 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 2344 | if ip_profile["ip-profile-params"].get('security-group'): |
| 2345 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 2346 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 2347 | db_ip_profiles_index += 1 |
| 2348 | db_ip_profiles.append(db_ip_profile) |
| 2349 | |
| 2350 | # table sce_nets (internal-vld) |
| 2351 | for vld in nsd.get("vld").itervalues(): |
| 2352 | sce_net_uuid = str(uuid4()) |
| 2353 | uuid_list.append(sce_net_uuid) |
| 2354 | db_sce_net = { |
| 2355 | "uuid": sce_net_uuid, |
| 2356 | "name": get_str(vld, "name", 255), |
| 2357 | "scenario_id": scenario_uuid, |
| 2358 | # "type": #TODO |
| 2359 | "multipoint": not vld.get("type") == "ELINE", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 2360 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2361 | # "external": #TODO |
| 2362 | "description": get_str(vld, "description", 255), |
| 2363 | } |
| 2364 | # guess type of network |
| 2365 | if vld.get("mgmt-network"): |
| 2366 | db_sce_net["type"] = "bridge" |
| 2367 | db_sce_net["external"] = True |
| 2368 | elif vld.get("provider-network").get("overlay-type") == "VLAN": |
| 2369 | db_sce_net["type"] = "data" |
| 2370 | else: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2371 | # later on it will be fixed to bridge or data depending on the type of interfaces attached to it |
| 2372 | db_sce_net["type"] = None |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2373 | db_sce_nets.append(db_sce_net) |
| 2374 | |
| 2375 | # ip-profile, link db_ip_profile with db_sce_net |
| 2376 | if vld.get("ip-profile-ref"): |
| 2377 | ip_profile_name = vld.get("ip-profile-ref") |
| 2378 | if ip_profile_name not in ip_profile_name2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2379 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'." |
| 2380 | " Reference to a non-existing 'ip_profiles'".format( |
| 2381 | str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2382 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2383 | 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] | 2384 | elif vld.get("vim-network-name"): |
| 2385 | db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2386 | |
| 2387 | # table sce_interfaces (vld:vnfd-connection-point-ref) |
| 2388 | for iface in vld.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2389 | vnf_index = str(iface['member-vnf-index-ref']) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2390 | # check correct parameters |
| 2391 | if vnf_index not in vnf_index2vnf_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2392 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2393 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2394 | "'nsd':'constituent-vnfd'".format( |
| 2395 | str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2396 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2397 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2398 | 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] | 2399 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2400 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2401 | 'external_name': get_str(iface, "vnfd-connection-point-ref", |
| 2402 | 255)}) |
| 2403 | if not existing_ifaces: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2404 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2405 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2406 | "connection-point name at VNFD '{}'".format( |
| 2407 | str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2408 | str(iface.get("vnfd-id-ref"))[:255]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2409 | httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2410 | interface_uuid = existing_ifaces[0]["uuid"] |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2411 | if existing_ifaces[0]["iface_type"] == "data": |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2412 | db_sce_net["type"] = "data" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2413 | sce_interface_uuid = str(uuid4()) |
| 2414 | uuid_list.append(sce_net_uuid) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2415 | iface_ip_address = None |
| 2416 | if iface.get("ip-address"): |
| 2417 | iface_ip_address = str(iface.get("ip-address")) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2418 | db_sce_interface = { |
| 2419 | "uuid": sce_interface_uuid, |
| 2420 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2421 | "sce_net_id": sce_net_uuid, |
| 2422 | "interface_id": interface_uuid, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2423 | "ip_address": iface_ip_address, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2424 | } |
| 2425 | db_sce_interfaces.append(db_sce_interface) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2426 | if not db_sce_net["type"]: |
| 2427 | db_sce_net["type"] = "bridge" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2428 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2429 | # table sce_vnffgs (vnffgd) |
| 2430 | for vnffg in nsd.get("vnffgd").itervalues(): |
| 2431 | sce_vnffg_uuid = str(uuid4()) |
| 2432 | uuid_list.append(sce_vnffg_uuid) |
| 2433 | db_sce_vnffg = { |
| 2434 | "uuid": sce_vnffg_uuid, |
| 2435 | "name": get_str(vnffg, "name", 255), |
| 2436 | "scenario_id": scenario_uuid, |
| 2437 | "vendor": get_str(vnffg, "vendor", 255), |
| 2438 | "description": get_str(vld, "description", 255), |
| 2439 | } |
| 2440 | db_sce_vnffgs.append(db_sce_vnffg) |
| 2441 | |
| 2442 | # deal with rsps |
| 2443 | db_sce_rsps = [] |
| 2444 | for rsp in vnffg.get("rsp").itervalues(): |
| 2445 | sce_rsp_uuid = str(uuid4()) |
| 2446 | uuid_list.append(sce_rsp_uuid) |
| 2447 | db_sce_rsp = { |
| 2448 | "uuid": sce_rsp_uuid, |
| 2449 | "name": get_str(rsp, "name", 255), |
| 2450 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2451 | "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code |
| 2452 | } |
| 2453 | db_sce_rsps.append(db_sce_rsp) |
| 2454 | db_sce_rsp_hops = [] |
| 2455 | for iface in rsp.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2456 | vnf_index = str(iface['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2457 | if_order = int(iface['order']) |
| 2458 | # check correct parameters |
| 2459 | if vnf_index not in vnf_index2vnf_uuid: |
| 2460 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2461 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2462 | "'nsd':'constituent-vnfd'".format( |
| 2463 | str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2464 | httperrors.Bad_Request) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2465 | |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2466 | ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2467 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2468 | WHERE={ |
| 2469 | 'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2470 | 'external_name': get_str(iface, "vnfd-ingress-connection-point-ref", |
| 2471 | 255)}) |
| 2472 | if not ingress_existing_ifaces: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2473 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2474 | "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing " |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2475 | "connection-point name at VNFD '{}'".format( |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2476 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]), |
| 2477 | str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request) |
| 2478 | |
| 2479 | egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2480 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2481 | WHERE={ |
| 2482 | 'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2483 | 'external_name': get_str(iface, "vnfd-egress-connection-point-ref", |
| 2484 | 255)}) |
| 2485 | if not egress_existing_ifaces: |
| 2486 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2487 | "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing " |
| 2488 | "connection-point name at VNFD '{}'".format( |
| 2489 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]), |
| 2490 | str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request) |
| 2491 | |
| 2492 | ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"] |
| 2493 | egress_interface_uuid = egress_existing_ifaces[0]["uuid"] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2494 | sce_rsp_hop_uuid = str(uuid4()) |
| 2495 | uuid_list.append(sce_rsp_hop_uuid) |
| 2496 | db_sce_rsp_hop = { |
| 2497 | "uuid": sce_rsp_hop_uuid, |
| 2498 | "if_order": if_order, |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2499 | "ingress_interface_id": ingress_interface_uuid, |
| 2500 | "egress_interface_id": egress_interface_uuid, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2501 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2502 | "sce_rsp_id": sce_rsp_uuid, |
| 2503 | } |
| 2504 | db_sce_rsp_hops.append(db_sce_rsp_hop) |
| 2505 | |
| 2506 | # deal with classifiers |
| 2507 | db_sce_classifiers = [] |
| 2508 | for classifier in vnffg.get("classifier").itervalues(): |
| 2509 | sce_classifier_uuid = str(uuid4()) |
| 2510 | uuid_list.append(sce_classifier_uuid) |
| 2511 | |
| 2512 | # source VNF |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2513 | vnf_index = str(classifier['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2514 | if vnf_index not in vnf_index2vnf_uuid: |
| 2515 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point" |
| 2516 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2517 | "'nsd':'constituent-vnfd'".format( |
| 2518 | str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2519 | httperrors.Bad_Request) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2520 | existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2521 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2522 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2523 | 'external_name': get_str(classifier, "vnfd-connection-point-ref", |
| 2524 | 255)}) |
| 2525 | if not existing_ifaces: |
| 2526 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2527 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2528 | "connection-point name at VNFD '{}'".format( |
| 2529 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2530 | str(iface.get("vnfd-id-ref"))[:255]), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2531 | httperrors.Bad_Request) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2532 | interface_uuid = existing_ifaces[0]["uuid"] |
| 2533 | |
| 2534 | db_sce_classifier = { |
| 2535 | "uuid": sce_classifier_uuid, |
| 2536 | "name": get_str(classifier, "name", 255), |
| 2537 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2538 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2539 | "interface_id": interface_uuid, |
| 2540 | } |
| 2541 | rsp_id = get_str(classifier, "rsp-id-ref", 255) |
| 2542 | rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None) |
| 2543 | db_sce_classifier["sce_rsp_id"] = rsp["uuid"] |
| 2544 | db_sce_classifiers.append(db_sce_classifier) |
| 2545 | |
| 2546 | db_sce_classifier_matches = [] |
| 2547 | for match in classifier.get("match-attributes").itervalues(): |
| 2548 | sce_classifier_match_uuid = str(uuid4()) |
| 2549 | uuid_list.append(sce_classifier_match_uuid) |
| 2550 | db_sce_classifier_match = { |
| 2551 | "uuid": sce_classifier_match_uuid, |
| 2552 | "ip_proto": get_str(match, "ip-proto", 2), |
| 2553 | "source_ip": get_str(match, "source-ip-address", 16), |
| 2554 | "destination_ip": get_str(match, "destination-ip-address", 16), |
| 2555 | "source_port": get_str(match, "source-port", 5), |
| 2556 | "destination_port": get_str(match, "destination-port", 5), |
| 2557 | "sce_classifier_id": sce_classifier_uuid, |
| 2558 | } |
| 2559 | db_sce_classifier_matches.append(db_sce_classifier_match) |
| 2560 | # TODO: vnf/cp keys |
| 2561 | |
| 2562 | # remove unneeded id's in sce_rsps |
| 2563 | for rsp in db_sce_rsps: |
| 2564 | rsp.pop('id') |
| 2565 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2566 | db_tables = [ |
| 2567 | {"scenarios": db_scenarios}, |
| 2568 | {"sce_nets": db_sce_nets}, |
| 2569 | {"ip_profiles": db_ip_profiles}, |
| 2570 | {"sce_vnfs": db_sce_vnfs}, |
| 2571 | {"sce_interfaces": db_sce_interfaces}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2572 | {"sce_vnffgs": db_sce_vnffgs}, |
| 2573 | {"sce_rsps": db_sce_rsps}, |
| 2574 | {"sce_rsp_hops": db_sce_rsp_hops}, |
| 2575 | {"sce_classifiers": db_sce_classifiers}, |
| 2576 | {"sce_classifier_matches": db_sce_classifier_matches}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2577 | ] |
| 2578 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2579 | logger.debug("new_nsd_v3 done: %s", |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2580 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 2581 | mydb.new_rows(db_tables, uuid_list) |
| 2582 | return nsd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2583 | except NfvoException: |
| 2584 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2585 | except Exception as e: |
| 2586 | logger.error("Exception {}".format(e)) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2587 | raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2588 | |
| 2589 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2590 | def edit_scenario(mydb, tenant_id, scenario_id, data): |
| 2591 | data["uuid"] = scenario_id |
| 2592 | data["tenant_id"] = tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2593 | c = mydb.edit_scenario( data ) |
| 2594 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2595 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2596 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 2597 | @deprecated("Use create_instance") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2598 | 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] | 2599 | #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] | 2600 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant) |
| 2601 | vims = {datacenter_id: myvim} |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2602 | myvim_tenant = myvim['tenant_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2603 | datacenter_name = myvim['name'] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2604 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2605 | rollbackList=[] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2606 | try: |
| 2607 | #print "Checking that the scenario_id exists and getting the scenario dictionary" |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2608 | scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2609 | scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] } |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2610 | scenarioDict['datacenter_id'] = datacenter_id |
| 2611 | #print '================scenarioDict=======================' |
| 2612 | #print json.dumps(scenarioDict, indent=4) |
| 2613 | #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2614 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2615 | logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs'])) |
| 2616 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2617 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2618 | auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id |
| 2619 | auxNetDict['scenario'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2620 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2621 | logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM") |
| 2622 | for sce_net in scenarioDict['nets']: |
| 2623 | #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2624 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2625 | myNetName = "%s.%s" % (instance_scenario_name, sce_net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2626 | myNetName = myNetName[0:255] #limit length |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2627 | myNetType = sce_net['type'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2628 | myNetDict = {} |
| 2629 | myNetDict["name"] = myNetName |
| 2630 | myNetDict["type"] = myNetType |
| 2631 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2632 | myNetIPProfile = sce_net.get('ip_profile', None) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2633 | #TODO: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2634 | #We should use the dictionary as input parameter for new_network |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2635 | #print myNetDict |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2636 | if not sce_net["external"]: |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2637 | network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2638 | #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id) |
| 2639 | sce_net['vim_id'] = network_id |
| 2640 | auxNetDict['scenario'][sce_net['uuid']] = network_id |
| 2641 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2642 | sce_net["created"] = True |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2643 | else: |
| 2644 | if sce_net['vim_id'] == None: |
| 2645 | error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name']) |
| 2646 | _, message = rollback(mydb, vims, rollbackList) |
| 2647 | logger.error("nfvo.start_scenario: %s", error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2648 | raise NfvoException(error_text, httperrors.Bad_Request) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2649 | logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id']) |
| 2650 | auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2651 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2652 | logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM") |
| 2653 | #For each vnf net, we create it and we add it to instanceNetlist. |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2654 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2655 | for sce_vnf in scenarioDict['vnfs']: |
| 2656 | for net in sce_vnf['nets']: |
| 2657 | #print "Net name: %s. Description: %s" % (net["name"], net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2658 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2659 | myNetName = "%s.%s" % (instance_scenario_name,net['name']) |
| 2660 | myNetName = myNetName[0:255] #limit length |
| 2661 | myNetType = net['type'] |
| 2662 | myNetDict = {} |
| 2663 | myNetDict["name"] = myNetName |
| 2664 | myNetDict["type"] = myNetType |
| 2665 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2666 | myNetIPProfile = net.get('ip_profile', None) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2667 | #print myNetDict |
| 2668 | #TODO: |
| 2669 | #We should use the dictionary as input parameter for new_network |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 2670 | network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2671 | #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id) |
| 2672 | net['vim_id'] = network_id |
| 2673 | if sce_vnf['uuid'] not in auxNetDict: |
| 2674 | auxNetDict[sce_vnf['uuid']] = {} |
| 2675 | auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id |
| 2676 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2677 | net["created"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2678 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2679 | #print "auxNetDict:" |
| 2680 | #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2681 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2682 | logger.debug("start_scenario 3. Creating new vm instances in the VIM") |
| 2683 | #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 2684 | i = 0 |
| 2685 | for sce_vnf in scenarioDict['vnfs']: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2686 | vnf_availability_zones = [] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2687 | for vm in sce_vnf['vms']: |
| 2688 | vm_av = vm.get('availability_zone') |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2689 | if vm_av and vm_av not in vnf_availability_zones: |
| 2690 | vnf_availability_zones.append(vm_av) |
| 2691 | |
| 2692 | # check if there is enough availability zones available at vim level. |
| 2693 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 2694 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2695 | 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] | 2696 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2697 | for vm in sce_vnf['vms']: |
| 2698 | i += 1 |
| 2699 | myVMDict = {} |
| 2700 | #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name']) |
| tierno | ae65a48 | 2016-11-24 16:20:05 +0100 | [diff] [blame] | 2701 | myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2702 | #myVMDict['description'] = vm['description'] |
| 2703 | myVMDict['description'] = myVMDict['name'][0:99] |
| 2704 | if not startvms: |
| 2705 | myVMDict['start'] = "no" |
| 2706 | myVMDict['name'] = myVMDict['name'][0:255] #limit name length |
| 2707 | #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2708 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2709 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2710 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2711 | image_id = create_or_use_image(mydb, vims, image_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2712 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2713 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2714 | #create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2715 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2716 | if flavor_dict['extended']!=None: |
| 2717 | flavor_dict['extended']= yaml.load(flavor_dict['extended']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2718 | flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2719 | vm['vim_flavor_id'] = flavor_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2720 | |
| 2721 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2722 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 2723 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 2724 | myVMDict['networks'] = [] |
| 2725 | for iface in vm['interfaces']: |
| 2726 | netDict = {} |
| 2727 | if iface['type']=="data": |
| 2728 | netDict['type'] = iface['model'] |
| 2729 | elif "model" in iface and iface["model"]!=None: |
| 2730 | netDict['model']=iface['model'] |
| 2731 | #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model |
| 2732 | #discover type of interface looking at flavor |
| 2733 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 2734 | for flavor_iface in numa.get('interfaces',[]): |
| 2735 | if flavor_iface.get('name') == iface['internal_name']: |
| 2736 | if flavor_iface['dedicated'] == 'yes': |
| 2737 | netDict['type']="PF" #passthrough |
| 2738 | elif flavor_iface['dedicated'] == 'no': |
| 2739 | netDict['type']="VF" #siov |
| 2740 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 2741 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 2742 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 2743 | break; |
| 2744 | netDict["use"]=iface['type'] |
| 2745 | if netDict["use"]=="data" and not netDict.get("type"): |
| 2746 | #print "netDict", netDict |
| 2747 | #print "iface", iface |
| 2748 | e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name']) |
| 2749 | if flavor_dict.get('extended')==None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2750 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2751 | Try to delete and create the scenarios and VNFs again", httperrors.Conflict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2752 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2753 | raise NfvoException(e_text, httperrors.Internal_Server_Error) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2754 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 2755 | netDict["type"]="virtual" |
| 2756 | if "vpci" in iface and iface["vpci"] is not None: |
| 2757 | netDict['vpci'] = iface['vpci'] |
| 2758 | if "mac" in iface and iface["mac"] is not None: |
| 2759 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 2760 | if "port-security" in iface and iface["port-security"] is not None: |
| 2761 | netDict['port_security'] = iface['port-security'] |
| 2762 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 2763 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2764 | netDict['name'] = iface['internal_name'] |
| 2765 | if iface['net_id'] is None: |
| 2766 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2767 | #print iface |
| 2768 | #print vnf_iface |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2769 | if vnf_iface['interface_id']==iface['uuid']: |
| 2770 | netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ] |
| 2771 | break |
| 2772 | else: |
| 2773 | netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| 2774 | #skip bridge ifaces not connected to any net |
| 2775 | #if 'net_id' not in netDict or netDict['net_id']==None: |
| 2776 | # continue |
| 2777 | myVMDict['networks'].append(netDict) |
| 2778 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 2779 | #print myVMDict['name'] |
| 2780 | #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 2781 | #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 2782 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2783 | |
| 2784 | if 'availability_zone' in myVMDict: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2785 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2786 | else: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2787 | av_index = None |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2788 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2789 | vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None), |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2790 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2791 | availability_zone_index=av_index, |
| 2792 | availability_zone_list=vnf_availability_zones) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2793 | #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id) |
| 2794 | vm['vim_id'] = vm_id |
| 2795 | rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id}) |
| 2796 | #put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 2797 | for net in myVMDict['networks']: |
| 2798 | if "vim_id" in net: |
| 2799 | for iface in vm['interfaces']: |
| 2800 | if net["name"]==iface["internal_name"]: |
| 2801 | iface["vim_id"]=net["vim_id"] |
| 2802 | break |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2803 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2804 | logger.debug("start scenario Deployment done") |
| 2805 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| 2806 | #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2807 | instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict) |
| 2808 | return mydb.get_instance_scenario(instance_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2809 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2810 | except (db_base_Exception, vimconn.vimconnException) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2811 | _, message = rollback(mydb, vims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2812 | if isinstance(e, db_base_Exception): |
| 2813 | error_text = "Exception at database" |
| 2814 | else: |
| 2815 | error_text = "Exception at VIM" |
| 2816 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 2817 | #logger.error("start_scenario %s", error_text) |
| 2818 | raise NfvoException(error_text, e.http_code) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2819 | |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2820 | def unify_cloud_config(cloud_config_preserve, cloud_config): |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2821 | """ join the cloud config information into cloud_config_preserve. |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2822 | In case of conflict cloud_config_preserve preserves |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2823 | None is allowed |
| 2824 | """ |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2825 | if not cloud_config_preserve and not cloud_config: |
| 2826 | return None |
| 2827 | |
| 2828 | new_cloud_config = {"key-pairs":[], "users":[]} |
| 2829 | # key-pairs |
| 2830 | if cloud_config_preserve: |
| 2831 | for key in cloud_config_preserve.get("key-pairs", () ): |
| 2832 | if key not in new_cloud_config["key-pairs"]: |
| 2833 | new_cloud_config["key-pairs"].append(key) |
| 2834 | if cloud_config: |
| 2835 | for key in cloud_config.get("key-pairs", () ): |
| 2836 | if key not in new_cloud_config["key-pairs"]: |
| 2837 | new_cloud_config["key-pairs"].append(key) |
| 2838 | if not new_cloud_config["key-pairs"]: |
| 2839 | del new_cloud_config["key-pairs"] |
| 2840 | |
| 2841 | # users |
| 2842 | if cloud_config: |
| 2843 | new_cloud_config["users"] += cloud_config.get("users", () ) |
| 2844 | if cloud_config_preserve: |
| 2845 | new_cloud_config["users"] += cloud_config_preserve.get("users", () ) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2846 | index_to_delete = [] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2847 | users = new_cloud_config.get("users", []) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2848 | for index0 in range(0,len(users)): |
| 2849 | if index0 in index_to_delete: |
| 2850 | continue |
| 2851 | for index1 in range(index0+1,len(users)): |
| 2852 | if index1 in index_to_delete: |
| 2853 | continue |
| 2854 | if users[index0]["name"] == users[index1]["name"]: |
| 2855 | index_to_delete.append(index1) |
| 2856 | for key in users[index1].get("key-pairs",()): |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2857 | if "key-pairs" not in users[index0]: |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2858 | users[index0]["key-pairs"] = [key] |
| 2859 | elif key not in users[index0]["key-pairs"]: |
| 2860 | users[index0]["key-pairs"].append(key) |
| 2861 | index_to_delete.sort(reverse=True) |
| 2862 | for index in index_to_delete: |
| 2863 | del users[index] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2864 | if not new_cloud_config["users"]: |
| 2865 | del new_cloud_config["users"] |
| 2866 | |
| 2867 | #boot-data-drive |
| 2868 | if cloud_config and cloud_config.get("boot-data-drive") != None: |
| 2869 | new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"] |
| 2870 | if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None: |
| 2871 | new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"] |
| 2872 | |
| 2873 | # user-data |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2874 | new_cloud_config["user-data"] = [] |
| 2875 | if cloud_config and cloud_config.get("user-data"): |
| 2876 | if isinstance(cloud_config["user-data"], list): |
| 2877 | new_cloud_config["user-data"] += cloud_config["user-data"] |
| 2878 | else: |
| 2879 | new_cloud_config["user-data"].append(cloud_config["user-data"]) |
| 2880 | if cloud_config_preserve and cloud_config_preserve.get("user-data"): |
| 2881 | if isinstance(cloud_config_preserve["user-data"], list): |
| 2882 | new_cloud_config["user-data"] += cloud_config_preserve["user-data"] |
| 2883 | else: |
| 2884 | new_cloud_config["user-data"].append(cloud_config_preserve["user-data"]) |
| 2885 | if not new_cloud_config["user-data"]: |
| 2886 | del new_cloud_config["user-data"] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2887 | |
| 2888 | # config files |
| 2889 | new_cloud_config["config-files"] = [] |
| 2890 | if cloud_config and cloud_config.get("config-files") != None: |
| 2891 | new_cloud_config["config-files"] += cloud_config["config-files"] |
| 2892 | if cloud_config_preserve: |
| 2893 | for file in cloud_config_preserve.get("config-files", ()): |
| 2894 | for index in range(0, len(new_cloud_config["config-files"])): |
| 2895 | if new_cloud_config["config-files"][index]["dest"] == file["dest"]: |
| 2896 | new_cloud_config["config-files"][index] = file |
| 2897 | break |
| 2898 | else: |
| 2899 | new_cloud_config["config-files"].append(file) |
| 2900 | if not new_cloud_config["config-files"]: |
| 2901 | del new_cloud_config["config-files"] |
| 2902 | return new_cloud_config |
| 2903 | |
| 2904 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2905 | 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] | 2906 | datacenter_id = None |
| 2907 | datacenter_name = None |
| 2908 | thread = None |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2909 | try: |
| 2910 | if datacenter_tenant_id: |
| 2911 | thread_id = datacenter_tenant_id |
| 2912 | thread = vim_threads["running"].get(datacenter_tenant_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2913 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2914 | where_={"td.nfvo_tenant_id": tenant_id} |
| 2915 | if datacenter_id_name: |
| 2916 | if utils.check_valid_uuid(datacenter_id_name): |
| 2917 | datacenter_id = datacenter_id_name |
| 2918 | where_["dt.datacenter_id"] = datacenter_id |
| 2919 | else: |
| 2920 | datacenter_name = datacenter_id_name |
| 2921 | where_["d.name"] = datacenter_name |
| 2922 | if datacenter_tenant_id: |
| 2923 | where_["dt.uuid"] = datacenter_tenant_id |
| 2924 | datacenters = mydb.get_rows( |
| 2925 | SELECT=("dt.uuid as datacenter_tenant_id",), |
| 2926 | FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id " |
| 2927 | "join datacenters as d on d.uuid=dt.datacenter_id", |
| 2928 | WHERE=where_) |
| 2929 | if len(datacenters) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2930 | 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] | 2931 | elif datacenters: |
| 2932 | thread_id = datacenters[0]["datacenter_tenant_id"] |
| 2933 | thread = vim_threads["running"].get(thread_id) |
| 2934 | if not thread: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2935 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2936 | return thread_id, thread |
| 2937 | except db_base_Exception as e: |
| 2938 | raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2939 | |
| tierno | f575596 | 2017-07-13 15:44:34 +0200 | [diff] [blame] | 2940 | |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2941 | def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name): |
| 2942 | WHERE_dict={} |
| 2943 | if utils.check_valid_uuid(datacenter_id_name): |
| 2944 | WHERE_dict['d.uuid'] = datacenter_id_name |
| 2945 | else: |
| 2946 | WHERE_dict['d.name'] = datacenter_id_name |
| 2947 | |
| 2948 | if tenant_id: |
| 2949 | WHERE_dict['nfvo_tenant_id'] = tenant_id |
| 2950 | from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \ |
| 2951 | " dt on td.datacenter_tenant_id=dt.uuid" |
| 2952 | else: |
| 2953 | from_ = 'datacenters as d' |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 2954 | 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] | 2955 | if len(vimaccounts) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2956 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found) |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2957 | elif len(vimaccounts)>1: |
| 2958 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2959 | 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] | 2960 | return vimaccounts[0]["uuid"], vimaccounts[0]["name"] |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2961 | |
| 2962 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2963 | 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] | 2964 | datacenter_id = None |
| 2965 | datacenter_name = None |
| 2966 | if datacenter_id_name: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2967 | if utils.check_valid_uuid(datacenter_id_name): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2968 | datacenter_id = datacenter_id_name |
| 2969 | else: |
| 2970 | datacenter_name = datacenter_id_name |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2971 | vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2972 | if len(vims) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2973 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2974 | elif len(vims)>1: |
| 2975 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 2976 | 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] | 2977 | return vims.keys()[0], vims.values()[0] |
| 2978 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2979 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2980 | def update(d, u): |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 2981 | """Takes dict d and updates it with the values in dict u. |
| 2982 | It merges all depth levels""" |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2983 | for k, v in u.iteritems(): |
| 2984 | if isinstance(v, collections.Mapping): |
| 2985 | r = update(d.get(k, {}), v) |
| 2986 | d[k] = r |
| 2987 | else: |
| 2988 | d[k] = u[k] |
| 2989 | return d |
| 2990 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2991 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2992 | def create_instance(mydb, tenant_id, instance_dict): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2993 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| 2994 | # logger.debug("Creating instance...") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2995 | scenario = instance_dict["scenario"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2996 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2997 | # find main datacenter |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2998 | myvims = {} |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2999 | myvim_threads_id = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3000 | datacenter = instance_dict.get("datacenter") |
| tierno | fc7cfbf | 2019-03-20 17:23:45 +0000 | [diff] [blame^] | 3001 | default_wim_account = instance_dict.get("wim_account") |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3002 | default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 3003 | myvims[default_datacenter_id] = vim |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3004 | 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] | 3005 | tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3006 | # myvim_tenant = myvim['tenant_id'] |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3007 | rollbackList = [] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3008 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3009 | # print "Checking that the scenario exists and getting the scenario dictionary" |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3010 | if isinstance(scenario, str): |
| 3011 | scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id], |
| 3012 | datacenter_id=default_datacenter_id) |
| 3013 | else: |
| 3014 | scenarioDict = scenario |
| 3015 | scenarioDict["uuid"] = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3016 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3017 | # logger.debug(">>>>>> Dictionaries before merging") |
| 3018 | # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256))) |
| 3019 | # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256))) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3020 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3021 | db_instance_vnfs = [] |
| 3022 | db_instance_vms = [] |
| 3023 | db_instance_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3024 | db_instance_sfis = [] |
| 3025 | db_instance_sfs = [] |
| 3026 | db_instance_classifications = [] |
| 3027 | db_instance_sfps = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3028 | db_ip_profiles = [] |
| 3029 | db_vim_actions = [] |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3030 | uuid_list = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3031 | task_index = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3032 | instance_name = instance_dict["name"] |
| 3033 | instance_uuid = str(uuid4()) |
| 3034 | uuid_list.append(instance_uuid) |
| 3035 | db_instance_scenario = { |
| 3036 | "uuid": instance_uuid, |
| 3037 | "name": instance_name, |
| 3038 | "tenant_id": tenant_id, |
| 3039 | "scenario_id": scenarioDict['uuid'], |
| 3040 | "datacenter_id": default_datacenter_id, |
| 3041 | # filled bellow 'datacenter_tenant_id' |
| 3042 | "description": instance_dict.get("description"), |
| 3043 | } |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3044 | if scenarioDict.get("cloud-config"): |
| 3045 | db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], |
| 3046 | default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3047 | instance_action_id = get_task_id() |
| 3048 | db_instance_action = { |
| 3049 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 3050 | "tenant_id": tenant_id, |
| 3051 | "instance_id": instance_uuid, |
| 3052 | "description": "CREATE", |
| 3053 | } |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3054 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3055 | # Auxiliary dictionaries from x to y |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3056 | sce_net2instance = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3057 | net2task_id = {'scenario': {}} |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3058 | # Mapping between local networks and WIMs |
| 3059 | wim_usage = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3060 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3061 | def ip_profile_IM2RO(ip_profile_im): |
| 3062 | # translate from input format to database format |
| 3063 | ip_profile_ro = {} |
| 3064 | if 'subnet-address' in ip_profile_im: |
| 3065 | ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address'] |
| 3066 | if 'ip-version' in ip_profile_im: |
| 3067 | ip_profile_ro['ip_version'] = ip_profile_im['ip-version'] |
| 3068 | if 'gateway-address' in ip_profile_im: |
| 3069 | ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address'] |
| 3070 | if 'dns-address' in ip_profile_im: |
| 3071 | ip_profile_ro['dns_address'] = ip_profile_im['dns-address'] |
| 3072 | if isinstance(ip_profile_ro['dns_address'], (list, tuple)): |
| 3073 | ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address']) |
| 3074 | if 'dhcp' in ip_profile_im: |
| 3075 | ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address') |
| 3076 | ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True) |
| 3077 | ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count') |
| 3078 | return ip_profile_ro |
| 3079 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3080 | # logger.debug("Creating instance from scenario-dict:\n%s", |
| 3081 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3082 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3083 | # 0 check correct parameters |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3084 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3085 | for scenario_net in scenarioDict['nets']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3086 | 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] | 3087 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3088 | else: |
| 3089 | 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] | 3090 | httperrors.Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3091 | if "sites" not in net_instance_desc: |
| 3092 | net_instance_desc["sites"] = [ {} ] |
| 3093 | site_without_datacenter_field = False |
| 3094 | for site in net_instance_desc["sites"]: |
| 3095 | if site.get("datacenter"): |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3096 | site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"]) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3097 | if site["datacenter"] not in myvims: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3098 | # Add this datacenter to myvims |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3099 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"]) |
| 3100 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3101 | myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"]) |
| 3102 | site["datacenter"] = d # change name to id |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3103 | else: |
| 3104 | if site_without_datacenter_field: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3105 | raise NfvoException("Found more than one entries without datacenter field at " |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3106 | "instance:networks:{}:sites".format(net_name), httperrors.Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3107 | site_without_datacenter_field = True |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3108 | site["datacenter"] = default_datacenter_id # change name to id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3109 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3110 | for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3111 | for scenario_vnf in scenarioDict['vnfs']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3112 | 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] | 3113 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3114 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3115 | 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] | 3116 | if "datacenter" in vnf_instance_desc: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3117 | # Add this datacenter to myvims |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3118 | 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] | 3119 | if vnf_instance_desc["datacenter"] not in myvims: |
| 3120 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| 3121 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3122 | 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] | 3123 | scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"] |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 3124 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3125 | for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems(): |
| 3126 | for scenario_net in scenario_vnf['nets']: |
| 3127 | if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]: |
| 3128 | break |
| 3129 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3130 | 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] | 3131 | if net_instance_desc.get("vim-network-name"): |
| 3132 | scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"] |
| gcalvino | 0a48054 | 2018-12-17 16:19:33 +0100 | [diff] [blame] | 3133 | if net_instance_desc.get("vim-network-id"): |
| 3134 | scenario_net["vim-network-id"] = net_instance_desc["vim-network-id"] |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3135 | if net_instance_desc.get("name"): |
| 3136 | scenario_net["name"] = net_instance_desc["name"] |
| 3137 | if 'ip-profile' in net_instance_desc: |
| 3138 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| 3139 | if 'ip_profile' not in scenario_net: |
| 3140 | scenario_net['ip_profile'] = ipprofile_db |
| 3141 | else: |
| 3142 | update(scenario_net['ip_profile'], ipprofile_db) |
| 3143 | |
| 3144 | for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems(): |
| 3145 | for scenario_vm in scenario_vnf['vms']: |
| 3146 | if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]: |
| 3147 | break |
| 3148 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3149 | 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] | 3150 | scenario_vm["instance_parameters"] = vdu_instance_desc |
| 3151 | for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems(): |
| 3152 | for scenario_interface in scenario_vm['interfaces']: |
| 3153 | if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]: |
| 3154 | scenario_interface.update(iface_instance_desc) |
| 3155 | break |
| 3156 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3157 | 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] | 3158 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3159 | # 0.1 parse cloud-config parameters |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 3160 | 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] | 3161 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3162 | # 0.2 merge instance information into scenario |
| 3163 | # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict) |
| 3164 | # However, this is not possible yet. |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3165 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3166 | for scenario_net in scenarioDict['nets']: |
| tierno | fc7cfbf | 2019-03-20 17:23:45 +0000 | [diff] [blame^] | 3167 | if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"): |
| 3168 | if "wim_account" in net_instance_desc and net_instance_desc["wim_account"] is not None: |
| 3169 | scenario_net["wim_account"] = net_instance_desc["wim_account"] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3170 | if 'ip-profile' in net_instance_desc: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3171 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3172 | if 'ip_profile' not in scenario_net: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3173 | scenario_net['ip_profile'] = ipprofile_db |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3174 | else: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3175 | update(scenario_net['ip_profile'], ipprofile_db) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3176 | for interface in net_instance_desc.get('interfaces', ()): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3177 | if 'ip_address' in interface: |
| 3178 | for vnf in scenarioDict['vnfs']: |
| 3179 | if interface['vnf'] == vnf['name']: |
| 3180 | for vnf_interface in vnf['interfaces']: |
| 3181 | if interface['vnf_interface'] == vnf_interface['external_name']: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3182 | vnf_interface['ip_address'] = interface['ip_address'] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3183 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3184 | # logger.debug(">>>>>>>> Merged dictionary") |
| 3185 | # logger.debug("Creating instance scenario-dict MERGED:\n%s", |
| 3186 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3187 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3188 | # 1. Creating new nets (sce_nets) in the VIM" |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3189 | number_mgmt_networks = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3190 | db_instance_nets = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3191 | for sce_net in scenarioDict['nets']: |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3192 | sce_net_uuid = sce_net.get('uuid', sce_net["name"]) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3193 | # get involved datacenters where this network need to be created |
| 3194 | involved_datacenters = [] |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3195 | for sce_vnf in scenarioDict.get("vnfs", ()): |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3196 | vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id) |
| 3197 | if vnf_datacenter in involved_datacenters: |
| 3198 | continue |
| 3199 | if sce_vnf.get("interfaces"): |
| 3200 | for sce_vnf_ifaces in sce_vnf["interfaces"]: |
| 3201 | if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]: |
| 3202 | involved_datacenters.append(vnf_datacenter) |
| 3203 | break |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 3204 | if not involved_datacenters: |
| 3205 | involved_datacenters.append(default_datacenter_id) |
| tierno | fc7cfbf | 2019-03-20 17:23:45 +0000 | [diff] [blame^] | 3206 | target_wim_account = scenario_net.get("wim_account", default_wim_account) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3207 | |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3208 | # --> WIM |
| 3209 | # TODO: use this information during network creation |
| tierno | 4070e44 | 2019-01-23 10:19:23 +0000 | [diff] [blame] | 3210 | wim_account_id = wim_account_name = None |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3211 | if len(involved_datacenters) > 1 and 'uuid' in sce_net: |
| tierno | fc7cfbf | 2019-03-20 17:23:45 +0000 | [diff] [blame^] | 3212 | if target_wim_account is None or target_wim_account is True: # automatic selection of WIM |
| 3213 | # OBS: sce_net without uuid are used internally to VNFs |
| 3214 | # and the assumption is that VNFs will not be split among |
| 3215 | # different datacenters |
| 3216 | wim_account = wim_engine.find_suitable_wim_account( |
| 3217 | involved_datacenters, tenant_id) |
| 3218 | wim_account_id = wim_account['uuid'] |
| 3219 | wim_account_name = wim_account['name'] |
| 3220 | wim_usage[sce_net['uuid']] = wim_account_id |
| 3221 | elif isinstance(target_wim_account, str): # manual selection of WIM |
| 3222 | wim_account.persist.get_wim_account_by(target_wim_account, tenant_id) |
| 3223 | wim_account_id = wim_account['uuid'] |
| 3224 | wim_account_name = wim_account['name'] |
| 3225 | wim_usage[sce_net['uuid']] = wim_account_id |
| 3226 | else: # not WIM usage |
| 3227 | wim_usage[sce_net['uuid']] = False |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3228 | # <-- WIM |
| 3229 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3230 | descriptor_net = {} |
| 3231 | if instance_dict.get("networks") and instance_dict["networks"].get(sce_net["name"]): |
| 3232 | descriptor_net = instance_dict["networks"][sce_net["name"]] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3233 | net_name = descriptor_net.get("vim-network-name") |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3234 | # add datacenters from instantiation parameters |
| 3235 | if descriptor_net.get("sites"): |
| 3236 | for site in descriptor_net["sites"]: |
| 3237 | if site.get("datacenter") and site["datacenter"] not in involved_datacenters: |
| 3238 | involved_datacenters.append(site["datacenter"]) |
| 3239 | sce_net2instance[sce_net_uuid] = {} |
| 3240 | net2task_id['scenario'][sce_net_uuid] = {} |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3241 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3242 | if sce_net["external"]: |
| 3243 | number_mgmt_networks += 1 |
| 3244 | |
| 3245 | for datacenter_id in involved_datacenters: |
| 3246 | netmap_use = None |
| 3247 | netmap_create = None |
| 3248 | if descriptor_net.get("sites"): |
| 3249 | for site in descriptor_net["sites"]: |
| 3250 | if site.get("datacenter") == datacenter_id: |
| 3251 | netmap_use = site.get("netmap-use") |
| 3252 | netmap_create = site.get("netmap-create") |
| 3253 | break |
| 3254 | |
| 3255 | vim = myvims[datacenter_id] |
| 3256 | myvim_thread_id = myvim_threads_id[datacenter_id] |
| 3257 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3258 | net_type = sce_net['type'] |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3259 | net_vim_name = None |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3260 | lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3261 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3262 | if not net_name: |
| 3263 | if sce_net["external"]: |
| 3264 | net_name = sce_net["name"] |
| 3265 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3266 | net_name = "{}-{}".format(instance_name, sce_net["name"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3267 | net_name = net_name[:255] # limit length |
| 3268 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3269 | if netmap_use or netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3270 | create_network = False |
| 3271 | lookfor_network = False |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3272 | if netmap_use: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3273 | lookfor_network = True |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3274 | if utils.check_valid_uuid(netmap_use): |
| 3275 | lookfor_filter["id"] = netmap_use |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3276 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3277 | lookfor_filter["name"] = netmap_use |
| 3278 | if netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3279 | create_network = True |
| 3280 | net_vim_name = net_name |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3281 | if isinstance(netmap_create, str): |
| 3282 | net_vim_name = netmap_create |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3283 | elif sce_net.get("vim_network_name"): |
| 3284 | create_network = False |
| 3285 | lookfor_network = True |
| 3286 | lookfor_filter["name"] = sce_net.get("vim_network_name") |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3287 | elif sce_net["external"]: |
| tierno | d108c41 | 2018-12-18 15:19:27 +0000 | [diff] [blame] | 3288 | if sce_net.get('vim_id'): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3289 | # there is a netmap at datacenter_nets database # TODO REVISE!!!! |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3290 | create_network = False |
| 3291 | lookfor_network = True |
| 3292 | lookfor_filter["id"] = sce_net['vim_id'] |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3293 | elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"): |
| 3294 | if number_mgmt_networks > 1: |
| 3295 | raise NfvoException("Found several VLD of type mgmt. " |
| 3296 | "You must concrete what vim-network must be use for each one", |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3297 | httperrors.Bad_Request) |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3298 | create_network = False |
| 3299 | lookfor_network = True |
| 3300 | if vim["config"].get("management_network_id"): |
| 3301 | lookfor_filter["id"] = vim["config"]["management_network_id"] |
| 3302 | else: |
| 3303 | lookfor_filter["name"] = vim["config"]["management_network_name"] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3304 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3305 | # 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] | 3306 | create_network = True |
| 3307 | lookfor_network = True |
| 3308 | lookfor_filter["name"] = sce_net["name"] |
| 3309 | net_vim_name = sce_net["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3310 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3311 | net_vim_name = net_name |
| 3312 | create_network = True |
| 3313 | lookfor_network = False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3314 | |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3315 | task_extra = {} |
| 3316 | if create_network: |
| 3317 | task_action = "CREATE" |
| tierno | 4070e44 | 2019-01-23 10:19:23 +0000 | [diff] [blame] | 3318 | 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] | 3319 | if lookfor_network: |
| 3320 | task_extra["find"] = (lookfor_filter,) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3321 | elif lookfor_network: |
| 3322 | task_action = "FIND" |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3323 | task_extra["params"] = (lookfor_filter,) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3324 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3325 | # fill database content |
| 3326 | net_uuid = str(uuid4()) |
| 3327 | uuid_list.append(net_uuid) |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3328 | sce_net2instance[sce_net_uuid][datacenter_id] = net_uuid |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3329 | db_net = { |
| 3330 | "uuid": net_uuid, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3331 | 'vim_net_id': None, |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3332 | "vim_name": net_vim_name, |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3333 | "instance_scenario_id": instance_uuid, |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3334 | "sce_net_id": sce_net.get("uuid"), |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3335 | "created": create_network, |
| 3336 | 'datacenter_id': datacenter_id, |
| 3337 | 'datacenter_tenant_id': myvim_thread_id, |
| tierno | d2836fc | 2018-05-30 15:03:27 +0200 | [diff] [blame] | 3338 | 'status': 'BUILD' # if create_network else "ACTIVE" |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3339 | } |
| 3340 | db_instance_nets.append(db_net) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3341 | db_vim_action = { |
| 3342 | "instance_action_id": instance_action_id, |
| 3343 | "status": "SCHEDULED", |
| 3344 | "task_index": task_index, |
| 3345 | "datacenter_vim_id": myvim_thread_id, |
| 3346 | "action": task_action, |
| 3347 | "item": "instance_nets", |
| 3348 | "item_id": net_uuid, |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3349 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3350 | } |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3351 | net2task_id['scenario'][sce_net_uuid][datacenter_id] = task_index |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3352 | task_index += 1 |
| 3353 | db_vim_actions.append(db_vim_action) |
| 3354 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3355 | if 'ip_profile' in sce_net: |
| 3356 | db_ip_profile={ |
| 3357 | 'instance_net_id': net_uuid, |
| 3358 | 'ip_version': sce_net['ip_profile']['ip_version'], |
| 3359 | 'subnet_address': sce_net['ip_profile']['subnet_address'], |
| 3360 | 'gateway_address': sce_net['ip_profile']['gateway_address'], |
| 3361 | 'dns_address': sce_net['ip_profile']['dns_address'], |
| 3362 | 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'], |
| 3363 | 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'], |
| 3364 | 'dhcp_count': sce_net['ip_profile']['dhcp_count'], |
| 3365 | } |
| 3366 | db_ip_profiles.append(db_ip_profile) |
| 3367 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3368 | # Create VNFs |
| 3369 | vnf_params = { |
| 3370 | "default_datacenter_id": default_datacenter_id, |
| 3371 | "myvim_threads_id": myvim_threads_id, |
| 3372 | "instance_uuid": instance_uuid, |
| 3373 | "instance_name": instance_name, |
| 3374 | "instance_action_id": instance_action_id, |
| 3375 | "myvims": myvims, |
| 3376 | "cloud_config": cloud_config, |
| 3377 | "RO_pub_key": tenant[0].get('RO_pub_key'), |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3378 | "instance_parameters": instance_dict, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3379 | } |
| 3380 | vnf_params_out = { |
| 3381 | "task_index": task_index, |
| 3382 | "uuid_list": uuid_list, |
| 3383 | "db_instance_nets": db_instance_nets, |
| 3384 | "db_vim_actions": db_vim_actions, |
| 3385 | "db_ip_profiles": db_ip_profiles, |
| 3386 | "db_instance_vnfs": db_instance_vnfs, |
| 3387 | "db_instance_vms": db_instance_vms, |
| 3388 | "db_instance_interfaces": db_instance_interfaces, |
| 3389 | "net2task_id": net2task_id, |
| 3390 | "sce_net2instance": sce_net2instance, |
| 3391 | } |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 3392 | # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3393 | for sce_vnf in scenarioDict.get('vnfs', ()): # sce_vnf_list: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3394 | instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList) |
| 3395 | task_index = vnf_params_out["task_index"] |
| 3396 | uuid_list = vnf_params_out["uuid_list"] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3397 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3398 | # Create VNFFGs |
| 3399 | # task_depends_on = [] |
| tierno | 7fe8264 | 2018-11-26 14:14:51 +0000 | [diff] [blame] | 3400 | for vnffg in scenarioDict.get('vnffgs', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3401 | for rsp in vnffg['rsps']: |
| 3402 | sfs_created = [] |
| 3403 | for cp in rsp['connection_points']: |
| 3404 | count = mydb.get_rows( |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 3405 | SELECT='vms.count', |
| 3406 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h " |
| 3407 | "on interfaces.uuid=h.ingress_interface_id", |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3408 | WHERE={'h.uuid': cp['uuid']})[0]['count'] |
| 3409 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None) |
| 3410 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3411 | dependencies = [] |
| 3412 | for instance_vm in instance_vms: |
| 3413 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3414 | if action: |
| 3415 | dependencies.append(action['task_index']) |
| 3416 | # TODO: throw exception if count != len(instance_vms) |
| 3417 | # TODO: and action shouldn't ever be None |
| 3418 | sfis_created = [] |
| 3419 | for i in range(count): |
| 3420 | # create sfis |
| 3421 | sfi_uuid = str(uuid4()) |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 3422 | extra_params = { |
| 3423 | "ingress_interface_id": cp["ingress_interface_id"], |
| 3424 | "egress_interface_id": cp["egress_interface_id"] |
| 3425 | } |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3426 | uuid_list.append(sfi_uuid) |
| 3427 | db_sfi = { |
| 3428 | "uuid": sfi_uuid, |
| 3429 | "instance_scenario_id": instance_uuid, |
| 3430 | 'sce_rsp_hop_id': cp['uuid'], |
| 3431 | 'datacenter_id': datacenter_id, |
| 3432 | 'datacenter_tenant_id': myvim_thread_id, |
| 3433 | "vim_sfi_id": None, # vim thread will populate |
| 3434 | } |
| 3435 | db_instance_sfis.append(db_sfi) |
| 3436 | db_vim_action = { |
| 3437 | "instance_action_id": instance_action_id, |
| 3438 | "task_index": task_index, |
| 3439 | "datacenter_vim_id": myvim_thread_id, |
| 3440 | "action": "CREATE", |
| 3441 | "status": "SCHEDULED", |
| 3442 | "item": "instance_sfis", |
| 3443 | "item_id": sfi_uuid, |
| Eduardo Sousa | 16cfd56 | 2018-11-30 15:33:35 +0000 | [diff] [blame] | 3444 | "extra": yaml.safe_dump({"params": extra_params, "depends_on": [dependencies[i]]}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3445 | default_flow_style=True, width=256) |
| 3446 | } |
| 3447 | sfis_created.append(task_index) |
| 3448 | task_index += 1 |
| 3449 | db_vim_actions.append(db_vim_action) |
| 3450 | # create sfs |
| 3451 | sf_uuid = str(uuid4()) |
| 3452 | uuid_list.append(sf_uuid) |
| 3453 | db_sf = { |
| 3454 | "uuid": sf_uuid, |
| 3455 | "instance_scenario_id": instance_uuid, |
| 3456 | 'sce_rsp_hop_id': cp['uuid'], |
| 3457 | 'datacenter_id': datacenter_id, |
| 3458 | 'datacenter_tenant_id': myvim_thread_id, |
| 3459 | "vim_sf_id": None, # vim thread will populate |
| 3460 | } |
| 3461 | db_instance_sfs.append(db_sf) |
| 3462 | db_vim_action = { |
| 3463 | "instance_action_id": instance_action_id, |
| 3464 | "task_index": task_index, |
| 3465 | "datacenter_vim_id": myvim_thread_id, |
| 3466 | "action": "CREATE", |
| 3467 | "status": "SCHEDULED", |
| 3468 | "item": "instance_sfs", |
| 3469 | "item_id": sf_uuid, |
| 3470 | "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created}, |
| 3471 | default_flow_style=True, width=256) |
| 3472 | } |
| 3473 | sfs_created.append(task_index) |
| 3474 | task_index += 1 |
| 3475 | db_vim_actions.append(db_vim_action) |
| 3476 | classifier = rsp['classifier'] |
| 3477 | |
| 3478 | # TODO the following ~13 lines can be reused for the sfi case |
| 3479 | count = mydb.get_rows( |
| 3480 | SELECT=('vms.count'), |
| 3481 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id", |
| 3482 | WHERE={'c.uuid': classifier['uuid']})[0]['count'] |
| 3483 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None) |
| 3484 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3485 | dependencies = [] |
| 3486 | for instance_vm in instance_vms: |
| 3487 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3488 | if action: |
| 3489 | dependencies.append(action['task_index']) |
| 3490 | # TODO: throw exception if count != len(instance_vms) |
| 3491 | # TODO: and action shouldn't ever be None |
| 3492 | classifications_created = [] |
| 3493 | for i in range(count): |
| 3494 | for match in classifier['matches']: |
| 3495 | # create classifications |
| 3496 | classification_uuid = str(uuid4()) |
| 3497 | uuid_list.append(classification_uuid) |
| 3498 | db_classification = { |
| 3499 | "uuid": classification_uuid, |
| 3500 | "instance_scenario_id": instance_uuid, |
| 3501 | 'sce_classifier_match_id': match['uuid'], |
| 3502 | 'datacenter_id': datacenter_id, |
| 3503 | 'datacenter_tenant_id': myvim_thread_id, |
| 3504 | "vim_classification_id": None, # vim thread will populate |
| 3505 | } |
| 3506 | db_instance_classifications.append(db_classification) |
| 3507 | classification_params = { |
| 3508 | "ip_proto": match["ip_proto"], |
| 3509 | "source_ip": match["source_ip"], |
| 3510 | "destination_ip": match["destination_ip"], |
| 3511 | "source_port": match["source_port"], |
| 3512 | "destination_port": match["destination_port"] |
| 3513 | } |
| 3514 | db_vim_action = { |
| 3515 | "instance_action_id": instance_action_id, |
| 3516 | "task_index": task_index, |
| 3517 | "datacenter_vim_id": myvim_thread_id, |
| 3518 | "action": "CREATE", |
| 3519 | "status": "SCHEDULED", |
| 3520 | "item": "instance_classifications", |
| 3521 | "item_id": classification_uuid, |
| 3522 | "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]}, |
| 3523 | default_flow_style=True, width=256) |
| 3524 | } |
| 3525 | classifications_created.append(task_index) |
| 3526 | task_index += 1 |
| 3527 | db_vim_actions.append(db_vim_action) |
| 3528 | |
| 3529 | # create sfps |
| 3530 | sfp_uuid = str(uuid4()) |
| 3531 | uuid_list.append(sfp_uuid) |
| 3532 | db_sfp = { |
| 3533 | "uuid": sfp_uuid, |
| 3534 | "instance_scenario_id": instance_uuid, |
| 3535 | 'sce_rsp_id': rsp['uuid'], |
| 3536 | 'datacenter_id': datacenter_id, |
| 3537 | 'datacenter_tenant_id': myvim_thread_id, |
| 3538 | "vim_sfp_id": None, # vim thread will populate |
| 3539 | } |
| 3540 | db_instance_sfps.append(db_sfp) |
| 3541 | db_vim_action = { |
| 3542 | "instance_action_id": instance_action_id, |
| 3543 | "task_index": task_index, |
| 3544 | "datacenter_vim_id": myvim_thread_id, |
| 3545 | "action": "CREATE", |
| 3546 | "status": "SCHEDULED", |
| 3547 | "item": "instance_sfps", |
| 3548 | "item_id": sfp_uuid, |
| 3549 | "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created}, |
| 3550 | default_flow_style=True, width=256) |
| 3551 | } |
| 3552 | task_index += 1 |
| 3553 | db_vim_actions.append(db_vim_action) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3554 | db_instance_action["number_tasks"] = task_index |
| 3555 | |
| 3556 | # --> WIM |
| Anderson Bravalheri | e2c09f3 | 2018-11-30 09:55:29 +0000 | [diff] [blame] | 3557 | logger.debug('wim_usage:\n%s\n\n', pformat(wim_usage)) |
| 3558 | 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] | 3559 | wim_actions = wim_engine.create_actions(wan_links) |
| 3560 | wim_actions, db_instance_action = ( |
| 3561 | wim_engine.incorporate_actions(wim_actions, db_instance_action)) |
| 3562 | # <-- WIM |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3563 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3564 | scenarioDict["datacenter2tenant"] = myvim_threads_id |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3565 | |
| 3566 | db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id] |
| 3567 | db_instance_scenario['datacenter_id'] = default_datacenter_id |
| 3568 | db_tables=[ |
| 3569 | {"instance_scenarios": db_instance_scenario}, |
| 3570 | {"instance_vnfs": db_instance_vnfs}, |
| 3571 | {"instance_nets": db_instance_nets}, |
| 3572 | {"ip_profiles": db_ip_profiles}, |
| 3573 | {"instance_vms": db_instance_vms}, |
| 3574 | {"instance_interfaces": db_instance_interfaces}, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3575 | {"instance_actions": db_instance_action}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3576 | {"instance_sfis": db_instance_sfis}, |
| 3577 | {"instance_sfs": db_instance_sfs}, |
| 3578 | {"instance_classifications": db_instance_classifications}, |
| 3579 | {"instance_sfps": db_instance_sfps}, |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3580 | {"instance_wim_nets": wan_links}, |
| 3581 | {"vim_wim_actions": db_vim_actions + wim_actions} |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3582 | ] |
| 3583 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3584 | logger.debug("create_instance done DB tables: %s", |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3585 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 3586 | mydb.new_rows(db_tables, uuid_list) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3587 | for myvim_thread_id in myvim_threads_id.values(): |
| 3588 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3589 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3590 | wim_engine.dispatch(wim_actions) |
| 3591 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3592 | returned_instance = mydb.get_instance_scenario(instance_uuid) |
| 3593 | returned_instance["action_id"] = instance_action_id |
| 3594 | return returned_instance |
| 3595 | except (NfvoException, vimconn.vimconnException, db_base_Exception) as e: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3596 | message = rollback(mydb, myvims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3597 | if isinstance(e, db_base_Exception): |
| 3598 | error_text = "database Exception" |
| 3599 | elif isinstance(e, vimconn.vimconnException): |
| 3600 | error_text = "VIM Exception" |
| 3601 | else: |
| 3602 | error_text = "Exception" |
| 3603 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3604 | # logger.error("create_instance: %s", error_text) |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3605 | logger.exception(e) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3606 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3607 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3608 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3609 | def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): |
| 3610 | default_datacenter_id = params["default_datacenter_id"] |
| 3611 | myvim_threads_id = params["myvim_threads_id"] |
| 3612 | instance_uuid = params["instance_uuid"] |
| 3613 | instance_name = params["instance_name"] |
| 3614 | instance_action_id = params["instance_action_id"] |
| 3615 | myvims = params["myvims"] |
| 3616 | cloud_config = params["cloud_config"] |
| 3617 | RO_pub_key = params["RO_pub_key"] |
| 3618 | |
| 3619 | task_index = params_out["task_index"] |
| 3620 | uuid_list = params_out["uuid_list"] |
| 3621 | db_instance_nets = params_out["db_instance_nets"] |
| 3622 | db_vim_actions = params_out["db_vim_actions"] |
| 3623 | db_ip_profiles = params_out["db_ip_profiles"] |
| 3624 | db_instance_vnfs = params_out["db_instance_vnfs"] |
| 3625 | db_instance_vms = params_out["db_instance_vms"] |
| 3626 | db_instance_interfaces = params_out["db_instance_interfaces"] |
| 3627 | net2task_id = params_out["net2task_id"] |
| 3628 | sce_net2instance = params_out["sce_net2instance"] |
| 3629 | |
| 3630 | vnf_net2instance = {} |
| 3631 | |
| 3632 | # 2. Creating new nets (vnf internal nets) in the VIM" |
| 3633 | # For each vnf net, we create it and we add it to instanceNetlist. |
| 3634 | if sce_vnf.get("datacenter"): |
| 3635 | datacenter_id = sce_vnf["datacenter"] |
| 3636 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3637 | else: |
| 3638 | datacenter_id = default_datacenter_id |
| 3639 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3640 | for net in sce_vnf['nets']: |
| 3641 | # TODO revis |
| 3642 | # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {}) |
| 3643 | # net_name = descriptor_net.get("name") |
| 3644 | net_name = None |
| 3645 | if not net_name: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3646 | net_name = "{}-{}".format(instance_name, net["name"]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3647 | net_name = net_name[:255] # limit length |
| 3648 | net_type = net['type'] |
| 3649 | |
| 3650 | if sce_vnf['uuid'] not in vnf_net2instance: |
| 3651 | vnf_net2instance[sce_vnf['uuid']] = {} |
| 3652 | if sce_vnf['uuid'] not in net2task_id: |
| 3653 | net2task_id[sce_vnf['uuid']] = {} |
| 3654 | net2task_id[sce_vnf['uuid']][net['uuid']] = task_index |
| 3655 | |
| 3656 | # fill database content |
| 3657 | net_uuid = str(uuid4()) |
| 3658 | uuid_list.append(net_uuid) |
| 3659 | vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid |
| 3660 | db_net = { |
| 3661 | "uuid": net_uuid, |
| 3662 | 'vim_net_id': None, |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3663 | "vim_name": net_name, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3664 | "instance_scenario_id": instance_uuid, |
| 3665 | "net_id": net["uuid"], |
| 3666 | "created": True, |
| 3667 | 'datacenter_id': datacenter_id, |
| 3668 | 'datacenter_tenant_id': myvim_thread_id, |
| 3669 | } |
| 3670 | db_instance_nets.append(db_net) |
| 3671 | |
| gcalvino | 0a48054 | 2018-12-17 16:19:33 +0100 | [diff] [blame] | 3672 | lookfor_filter = {} |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3673 | if net.get("vim-network-name"): |
| gcalvino | 0a48054 | 2018-12-17 16:19:33 +0100 | [diff] [blame] | 3674 | lookfor_filter["name"] = net["vim-network-name"] |
| 3675 | if net.get("vim-network-id"): |
| 3676 | lookfor_filter["id"] = net["vim-network-id"] |
| 3677 | if lookfor_filter: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3678 | task_action = "FIND" |
| 3679 | task_extra = {"params": (lookfor_filter,)} |
| 3680 | else: |
| 3681 | task_action = "CREATE" |
| 3682 | task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))} |
| 3683 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3684 | db_vim_action = { |
| 3685 | "instance_action_id": instance_action_id, |
| 3686 | "task_index": task_index, |
| 3687 | "datacenter_vim_id": myvim_thread_id, |
| 3688 | "status": "SCHEDULED", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3689 | "action": task_action, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3690 | "item": "instance_nets", |
| 3691 | "item_id": net_uuid, |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3692 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3693 | } |
| 3694 | task_index += 1 |
| 3695 | db_vim_actions.append(db_vim_action) |
| 3696 | |
| 3697 | if 'ip_profile' in net: |
| 3698 | db_ip_profile = { |
| 3699 | 'instance_net_id': net_uuid, |
| 3700 | 'ip_version': net['ip_profile']['ip_version'], |
| 3701 | 'subnet_address': net['ip_profile']['subnet_address'], |
| 3702 | 'gateway_address': net['ip_profile']['gateway_address'], |
| 3703 | 'dns_address': net['ip_profile']['dns_address'], |
| 3704 | 'dhcp_enabled': net['ip_profile']['dhcp_enabled'], |
| 3705 | 'dhcp_start_address': net['ip_profile']['dhcp_start_address'], |
| 3706 | 'dhcp_count': net['ip_profile']['dhcp_count'], |
| 3707 | } |
| 3708 | db_ip_profiles.append(db_ip_profile) |
| 3709 | |
| 3710 | # print "vnf_net2instance:" |
| 3711 | # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False) |
| 3712 | |
| 3713 | # 3. Creating new vm instances in the VIM |
| 3714 | # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 3715 | ssh_access = None |
| 3716 | if sce_vnf.get('mgmt_access'): |
| 3717 | ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access') |
| 3718 | vnf_availability_zones = [] |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 3719 | for vm in sce_vnf.get('vms'): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3720 | vm_av = vm.get('availability_zone') |
| 3721 | if vm_av and vm_av not in vnf_availability_zones: |
| 3722 | vnf_availability_zones.append(vm_av) |
| 3723 | |
| 3724 | # check if there is enough availability zones available at vim level. |
| 3725 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 3726 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3727 | 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] | 3728 | |
| 3729 | if sce_vnf.get("datacenter"): |
| 3730 | vim = myvims[sce_vnf["datacenter"]] |
| 3731 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3732 | datacenter_id = sce_vnf["datacenter"] |
| 3733 | else: |
| 3734 | vim = myvims[default_datacenter_id] |
| 3735 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3736 | datacenter_id = default_datacenter_id |
| 3737 | sce_vnf["datacenter_id"] = datacenter_id |
| 3738 | i = 0 |
| 3739 | |
| 3740 | vnf_uuid = str(uuid4()) |
| 3741 | uuid_list.append(vnf_uuid) |
| 3742 | db_instance_vnf = { |
| 3743 | 'uuid': vnf_uuid, |
| 3744 | 'instance_scenario_id': instance_uuid, |
| 3745 | 'vnf_id': sce_vnf['vnf_id'], |
| 3746 | 'sce_vnf_id': sce_vnf['uuid'], |
| 3747 | 'datacenter_id': datacenter_id, |
| 3748 | 'datacenter_tenant_id': myvim_thread_id, |
| 3749 | } |
| 3750 | db_instance_vnfs.append(db_instance_vnf) |
| 3751 | |
| 3752 | for vm in sce_vnf['vms']: |
| tierno | b699079 | 2018-11-13 10:37:42 +0100 | [diff] [blame] | 3753 | # skip PDUs |
| 3754 | if vm.get("pdu_type"): |
| 3755 | continue |
| 3756 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3757 | myVMDict = {} |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 3758 | sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name'] |
| 3759 | myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3760 | myVMDict['description'] = myVMDict['name'][0:99] |
| 3761 | # if not startvms: |
| 3762 | # myVMDict['start'] = "no" |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3763 | if vm.get("instance_parameters") and vm["instance_parameters"].get("name"): |
| 3764 | myVMDict['name'] = vm["instance_parameters"].get("name") |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3765 | myVMDict['name'] = myVMDict['name'][0:255] # limit name length |
| 3766 | # create image at vim in case it not exist |
| 3767 | image_uuid = vm['image_id'] |
| 3768 | if vm.get("image_list"): |
| 3769 | for alternative_image in vm["image_list"]: |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 3770 | if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3771 | image_uuid = alternative_image['image_id'] |
| 3772 | break |
| 3773 | image_dict = mydb.get_table_by_uuid_name("images", image_uuid) |
| 3774 | image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True) |
| 3775 | vm['vim_image_id'] = image_id |
| 3776 | |
| 3777 | # create flavor at vim in case it not exist |
| 3778 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| 3779 | if flavor_dict['extended'] != None: |
| 3780 | flavor_dict['extended'] = yaml.load(flavor_dict['extended']) |
| 3781 | flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True) |
| 3782 | |
| 3783 | # Obtain information for additional disks |
| 3784 | extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), |
| 3785 | WHERE={'vim_id': flavor_id}) |
| 3786 | if not extended_flavor_dict: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3787 | raise NfvoException("flavor '{}' not found".format(flavor_id), httperrors.Not_Found) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3788 | |
| 3789 | # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0]) |
| 3790 | myVMDict['disks'] = None |
| 3791 | extended_info = extended_flavor_dict[0]['extended'] |
| 3792 | if extended_info != None: |
| 3793 | extended_flavor_dict_yaml = yaml.load(extended_info) |
| 3794 | if 'disks' in extended_flavor_dict_yaml: |
| 3795 | myVMDict['disks'] = extended_flavor_dict_yaml['disks'] |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3796 | if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"): |
| 3797 | for disk in myVMDict['disks']: |
| 3798 | if disk.get("name") in vm["instance_parameters"]["devices"]: |
| 3799 | disk.update(vm["instance_parameters"]["devices"][disk.get("name")]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3800 | |
| 3801 | vm['vim_flavor_id'] = flavor_id |
| 3802 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 3803 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 3804 | myVMDict['availability_zone'] = vm.get('availability_zone') |
| 3805 | myVMDict['networks'] = [] |
| 3806 | task_depends_on = [] |
| 3807 | # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3808 | is_management_vm = False |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3809 | db_vm_ifaces = [] |
| 3810 | for iface in vm['interfaces']: |
| 3811 | netDict = {} |
| 3812 | if iface['type'] == "data": |
| 3813 | netDict['type'] = iface['model'] |
| 3814 | elif "model" in iface and iface["model"] != None: |
| 3815 | netDict['model'] = iface['model'] |
| 3816 | # TODO in future, remove this because mac_address will not be set, and the type of PV,VF |
| 3817 | # is obtained from iterface table model |
| 3818 | # discover type of interface looking at flavor |
| 3819 | for numa in flavor_dict.get('extended', {}).get('numas', []): |
| 3820 | for flavor_iface in numa.get('interfaces', []): |
| 3821 | if flavor_iface.get('name') == iface['internal_name']: |
| 3822 | if flavor_iface['dedicated'] == 'yes': |
| 3823 | netDict['type'] = "PF" # passthrough |
| 3824 | elif flavor_iface['dedicated'] == 'no': |
| 3825 | netDict['type'] = "VF" # siov |
| 3826 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 3827 | netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF |
| 3828 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 3829 | break |
| 3830 | netDict["use"] = iface['type'] |
| 3831 | if netDict["use"] == "data" and not netDict.get("type"): |
| 3832 | # print "netDict", netDict |
| 3833 | # print "iface", iface |
| 3834 | e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat( |
| 3835 | sce_vnf['name'], vm['name'], iface['internal_name']) |
| 3836 | if flavor_dict.get('extended') == None: |
| 3837 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3838 | Try to delete and create the scenarios and VNFs again", httperrors.Conflict) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3839 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3840 | raise NfvoException(e_text, httperrors.Internal_Server_Error) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3841 | if netDict["use"] == "mgmt": |
| 3842 | is_management_vm = True |
| 3843 | netDict["type"] = "virtual" |
| 3844 | if netDict["use"] == "bridge": |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3845 | netDict["type"] = "virtual" |
| 3846 | if iface.get("vpci"): |
| 3847 | netDict['vpci'] = iface['vpci'] |
| 3848 | if iface.get("mac"): |
| 3849 | netDict['mac_address'] = iface['mac'] |
| tierno | 6082b7d | 2018-08-31 11:24:08 +0000 | [diff] [blame] | 3850 | if iface.get("mac_address"): |
| 3851 | netDict['mac_address'] = iface['mac_address'] |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3852 | if iface.get("ip_address"): |
| 3853 | netDict['ip_address'] = iface['ip_address'] |
| 3854 | if iface.get("port-security") is not None: |
| 3855 | netDict['port_security'] = iface['port-security'] |
| 3856 | if iface.get("floating-ip") is not None: |
| 3857 | netDict['floating_ip'] = iface['floating-ip'] |
| 3858 | netDict['name'] = iface['internal_name'] |
| 3859 | if iface['net_id'] is None: |
| 3860 | for vnf_iface in sce_vnf["interfaces"]: |
| 3861 | # print iface |
| 3862 | # print vnf_iface |
| 3863 | if vnf_iface['interface_id'] == iface['uuid']: |
| 3864 | netDict['net_id'] = "TASK-{}".format( |
| 3865 | net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3866 | instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id] |
| 3867 | task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3868 | break |
| 3869 | else: |
| 3870 | netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3871 | instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']] |
| 3872 | task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3873 | # skip bridge ifaces not connected to any net |
| 3874 | if 'net_id' not in netDict or netDict['net_id'] == None: |
| 3875 | continue |
| 3876 | myVMDict['networks'].append(netDict) |
| 3877 | db_vm_iface = { |
| 3878 | # "uuid" |
| 3879 | # 'instance_vm_id': instance_vm_uuid, |
| 3880 | "instance_net_id": instance_net_id, |
| 3881 | 'interface_id': iface['uuid'], |
| 3882 | # 'vim_interface_id': , |
| 3883 | 'type': 'external' if iface['external_name'] is not None else 'internal', |
| 3884 | 'ip_address': iface.get('ip_address'), |
| 3885 | 'mac_address': iface.get('mac'), |
| 3886 | 'floating_ip': int(iface.get('floating-ip', False)), |
| 3887 | 'port_security': int(iface.get('port-security', True)) |
| 3888 | } |
| 3889 | db_vm_ifaces.append(db_vm_iface) |
| 3890 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3891 | # print myVMDict['name'] |
| 3892 | # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 3893 | # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 3894 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3895 | |
| 3896 | # We add the RO key to cloud_config if vnf will need ssh access |
| 3897 | cloud_config_vm = cloud_config |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 3898 | if is_management_vm and params["instance_parameters"].get("mgmt_keys"): |
| 3899 | cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]}, |
| 3900 | cloud_config_vm) |
| 3901 | |
| 3902 | if vm.get("instance_parameters") and vm["instance_parameters"].get("mgmt_keys"): |
| 3903 | cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]}, |
| 3904 | cloud_config_vm) |
| 3905 | # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'): |
| 3906 | # RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]} |
| 3907 | # cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3908 | if vm.get("boot_data"): |
| 3909 | cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm) |
| 3910 | |
| 3911 | if myVMDict.get('availability_zone'): |
| 3912 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| 3913 | else: |
| 3914 | av_index = None |
| 3915 | for vm_index in range(0, vm.get('count', 1)): |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 3916 | vm_name = myVMDict['name'] + "-" + str(vm_index+1) |
| 3917 | task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3918 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm, |
| 3919 | myVMDict['disks'], av_index, vnf_availability_zones) |
| 3920 | # put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 3921 | for net in myVMDict['networks']: |
| 3922 | if "vim_id" in net: |
| 3923 | for iface in vm['interfaces']: |
| 3924 | if net["name"] == iface["internal_name"]: |
| 3925 | iface["vim_id"] = net["vim_id"] |
| 3926 | break |
| 3927 | vm_uuid = str(uuid4()) |
| 3928 | uuid_list.append(vm_uuid) |
| 3929 | db_vm = { |
| 3930 | "uuid": vm_uuid, |
| 3931 | 'instance_vnf_id': vnf_uuid, |
| 3932 | # TODO delete "vim_vm_id": vm_id, |
| 3933 | "vm_id": vm["uuid"], |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 3934 | "vim_name": vm_name, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3935 | # "status": |
| 3936 | } |
| 3937 | db_instance_vms.append(db_vm) |
| 3938 | |
| 3939 | iface_index = 0 |
| 3940 | for db_vm_iface in db_vm_ifaces: |
| 3941 | iface_uuid = str(uuid4()) |
| 3942 | uuid_list.append(iface_uuid) |
| 3943 | db_vm_iface_instance = { |
| 3944 | "uuid": iface_uuid, |
| 3945 | "instance_vm_id": vm_uuid |
| 3946 | } |
| 3947 | db_vm_iface_instance.update(db_vm_iface) |
| 3948 | if db_vm_iface_instance.get("ip_address"): # increment ip_address |
| 3949 | ip = db_vm_iface_instance.get("ip_address") |
| 3950 | i = ip.rfind(".") |
| 3951 | if i > 0: |
| 3952 | try: |
| 3953 | i += 1 |
| 3954 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 3955 | db_vm_iface_instance["ip_address"] = ip |
| 3956 | except: |
| 3957 | db_vm_iface_instance["ip_address"] = None |
| 3958 | db_instance_interfaces.append(db_vm_iface_instance) |
| 3959 | myVMDict['networks'][iface_index]["uuid"] = iface_uuid |
| 3960 | iface_index += 1 |
| 3961 | |
| 3962 | db_vim_action = { |
| 3963 | "instance_action_id": instance_action_id, |
| 3964 | "task_index": task_index, |
| 3965 | "datacenter_vim_id": myvim_thread_id, |
| 3966 | "action": "CREATE", |
| 3967 | "status": "SCHEDULED", |
| 3968 | "item": "instance_vms", |
| 3969 | "item_id": vm_uuid, |
| 3970 | "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on}, |
| 3971 | default_flow_style=True, width=256) |
| 3972 | } |
| 3973 | task_index += 1 |
| 3974 | db_vim_actions.append(db_vim_action) |
| 3975 | params_out["task_index"] = task_index |
| 3976 | params_out["uuid_list"] = uuid_list |
| 3977 | |
| 3978 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3979 | def delete_instance(mydb, tenant_id, instance_id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3980 | # print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3981 | instanceDict = mydb.get_instance_scenario(instance_id, tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3982 | # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3983 | tenant_id = instanceDict["tenant_id"] |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 3984 | |
| 3985 | # --> WIM |
| 3986 | # We need to retrieve the WIM Actions now, before the instance_scenario is |
| 3987 | # deleted. The reason for that is that: ON CASCADE rules will delete the |
| 3988 | # instance_wim_nets record in the database |
| 3989 | wim_actions = wim_engine.delete_actions(instance_scenario_id=instance_id) |
| 3990 | # <-- WIM |
| 3991 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3992 | # 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] | 3993 | # 1. Delete from Database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3994 | message = mydb.delete_instance_scenario(instance_id, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3995 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3996 | # 2. delete from VIM |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3997 | error_msg = "" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3998 | myvims = {} |
| 3999 | myvim_threads = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4000 | vimthread_affected = {} |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 4001 | net2vm_dependencies = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4002 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4003 | task_index = 0 |
| 4004 | instance_action_id = get_task_id() |
| 4005 | db_vim_actions = [] |
| 4006 | db_instance_action = { |
| 4007 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 4008 | "tenant_id": tenant_id, |
| 4009 | "instance_id": instance_id, |
| 4010 | "description": "DELETE", |
| 4011 | # "number_tasks": 0 # filled bellow |
| 4012 | } |
| 4013 | |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4014 | # 2.1 deleting VNFFGs |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 4015 | for sfp in instanceDict.get('sfps', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4016 | vimthread_affected[sfp["datacenter_tenant_id"]] = None |
| 4017 | datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"]) |
| 4018 | if datacenter_key not in myvims: |
| 4019 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4020 | _, 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] | 4021 | except NfvoException as e: |
| 4022 | logger.error(str(e)) |
| 4023 | myvim_thread = None |
| 4024 | myvim_threads[datacenter_key] = myvim_thread |
| 4025 | vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"], |
| 4026 | datacenter_tenant_id=sfp["datacenter_tenant_id"]) |
| 4027 | if len(vims) == 0: |
| 4028 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"])) |
| 4029 | myvims[datacenter_key] = None |
| 4030 | else: |
| 4031 | myvims[datacenter_key] = vims.values()[0] |
| 4032 | myvim = myvims[datacenter_key] |
| 4033 | myvim_thread = myvim_threads[datacenter_key] |
| 4034 | |
| 4035 | if not myvim: |
| 4036 | error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"]) |
| 4037 | continue |
| 4038 | extra = {"params": (sfp['vim_sfp_id'])} |
| 4039 | db_vim_action = { |
| 4040 | "instance_action_id": instance_action_id, |
| 4041 | "task_index": task_index, |
| 4042 | "datacenter_vim_id": sfp["datacenter_tenant_id"], |
| 4043 | "action": "DELETE", |
| 4044 | "status": "SCHEDULED", |
| 4045 | "item": "instance_sfps", |
| 4046 | "item_id": sfp["uuid"], |
| 4047 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4048 | } |
| 4049 | task_index += 1 |
| 4050 | db_vim_actions.append(db_vim_action) |
| 4051 | |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4052 | for classification in instanceDict['classifications']: |
| 4053 | vimthread_affected[classification["datacenter_tenant_id"]] = None |
| 4054 | datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 4055 | if datacenter_key not in myvims: |
| 4056 | try: |
| 4057 | _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 4058 | except NfvoException as e: |
| 4059 | logger.error(str(e)) |
| 4060 | myvim_thread = None |
| 4061 | myvim_threads[datacenter_key] = myvim_thread |
| 4062 | vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"], |
| 4063 | datacenter_tenant_id=classification["datacenter_tenant_id"]) |
| 4064 | if len(vims) == 0: |
| 4065 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"], |
| 4066 | classification["datacenter_tenant_id"])) |
| 4067 | myvims[datacenter_key] = None |
| 4068 | else: |
| 4069 | myvims[datacenter_key] = vims.values()[0] |
| 4070 | myvim = myvims[datacenter_key] |
| 4071 | myvim_thread = myvim_threads[datacenter_key] |
| 4072 | |
| 4073 | if not myvim: |
| 4074 | error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'], |
| 4075 | classification["datacenter_id"]) |
| 4076 | continue |
| 4077 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"] |
| 4078 | extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on} |
| 4079 | db_vim_action = { |
| 4080 | "instance_action_id": instance_action_id, |
| 4081 | "task_index": task_index, |
| 4082 | "datacenter_vim_id": classification["datacenter_tenant_id"], |
| 4083 | "action": "DELETE", |
| 4084 | "status": "SCHEDULED", |
| 4085 | "item": "instance_classifications", |
| 4086 | "item_id": classification["uuid"], |
| 4087 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4088 | } |
| 4089 | task_index += 1 |
| 4090 | db_vim_actions.append(db_vim_action) |
| 4091 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 4092 | for sf in instanceDict.get('sfs', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4093 | vimthread_affected[sf["datacenter_tenant_id"]] = None |
| 4094 | datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"]) |
| 4095 | if datacenter_key not in myvims: |
| 4096 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4097 | _, 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] | 4098 | except NfvoException as e: |
| 4099 | logger.error(str(e)) |
| 4100 | myvim_thread = None |
| 4101 | myvim_threads[datacenter_key] = myvim_thread |
| 4102 | vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"], |
| 4103 | datacenter_tenant_id=sf["datacenter_tenant_id"]) |
| 4104 | if len(vims) == 0: |
| 4105 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"])) |
| 4106 | myvims[datacenter_key] = None |
| 4107 | else: |
| 4108 | myvims[datacenter_key] = vims.values()[0] |
| 4109 | myvim = myvims[datacenter_key] |
| 4110 | myvim_thread = myvim_threads[datacenter_key] |
| 4111 | |
| 4112 | if not myvim: |
| 4113 | error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"]) |
| 4114 | continue |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4115 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"] |
| 4116 | extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4117 | db_vim_action = { |
| 4118 | "instance_action_id": instance_action_id, |
| 4119 | "task_index": task_index, |
| 4120 | "datacenter_vim_id": sf["datacenter_tenant_id"], |
| 4121 | "action": "DELETE", |
| 4122 | "status": "SCHEDULED", |
| 4123 | "item": "instance_sfs", |
| 4124 | "item_id": sf["uuid"], |
| 4125 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4126 | } |
| 4127 | task_index += 1 |
| 4128 | db_vim_actions.append(db_vim_action) |
| 4129 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 4130 | for sfi in instanceDict.get('sfis', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4131 | vimthread_affected[sfi["datacenter_tenant_id"]] = None |
| 4132 | datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"]) |
| 4133 | if datacenter_key not in myvims: |
| 4134 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4135 | _, 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] | 4136 | except NfvoException as e: |
| 4137 | logger.error(str(e)) |
| 4138 | myvim_thread = None |
| 4139 | myvim_threads[datacenter_key] = myvim_thread |
| 4140 | vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"], |
| 4141 | datacenter_tenant_id=sfi["datacenter_tenant_id"]) |
| 4142 | if len(vims) == 0: |
| 4143 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"])) |
| 4144 | myvims[datacenter_key] = None |
| 4145 | else: |
| 4146 | myvims[datacenter_key] = vims.values()[0] |
| 4147 | myvim = myvims[datacenter_key] |
| 4148 | myvim_thread = myvim_threads[datacenter_key] |
| 4149 | |
| 4150 | if not myvim: |
| 4151 | error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"]) |
| 4152 | continue |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4153 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"] |
| 4154 | extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4155 | db_vim_action = { |
| 4156 | "instance_action_id": instance_action_id, |
| 4157 | "task_index": task_index, |
| 4158 | "datacenter_vim_id": sfi["datacenter_tenant_id"], |
| 4159 | "action": "DELETE", |
| 4160 | "status": "SCHEDULED", |
| 4161 | "item": "instance_sfis", |
| 4162 | "item_id": sfi["uuid"], |
| 4163 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4164 | } |
| 4165 | task_index += 1 |
| 4166 | db_vim_actions.append(db_vim_action) |
| 4167 | |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4168 | # 2.2 deleting VMs |
| 4169 | # vm_fail_list=[] |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 4170 | for sce_vnf in instanceDict.get('vnfs', ()): |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4171 | datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4172 | vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4173 | if datacenter_key not in myvims: |
| 4174 | try: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4175 | _, 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] | 4176 | except NfvoException as e: |
| 4177 | logger.error(str(e)) |
| 4178 | myvim_thread = None |
| 4179 | myvim_threads[datacenter_key] = myvim_thread |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4180 | vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"], |
| 4181 | datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4182 | if len(vims) == 0: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4183 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], |
| 4184 | sce_vnf["datacenter_tenant_id"])) |
| 4185 | myvims[datacenter_key] = None |
| 4186 | else: |
| 4187 | myvims[datacenter_key] = vims.values()[0] |
| 4188 | myvim = myvims[datacenter_key] |
| 4189 | myvim_thread = myvim_threads[datacenter_key] |
| 4190 | |
| 4191 | for vm in sce_vnf['vms']: |
| 4192 | if not myvim: |
| 4193 | error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"]) |
| 4194 | continue |
| 4195 | sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"] |
| 4196 | db_vim_action = { |
| 4197 | "instance_action_id": instance_action_id, |
| 4198 | "task_index": task_index, |
| 4199 | "datacenter_vim_id": sce_vnf["datacenter_tenant_id"], |
| 4200 | "action": "DELETE", |
| 4201 | "status": "SCHEDULED", |
| 4202 | "item": "instance_vms", |
| 4203 | "item_id": vm["uuid"], |
| 4204 | "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies}, |
| 4205 | default_flow_style=True, width=256) |
| 4206 | } |
| 4207 | db_vim_actions.append(db_vim_action) |
| 4208 | for interface in vm["interfaces"]: |
| 4209 | if not interface.get("instance_net_id"): |
| 4210 | continue |
| 4211 | if interface["instance_net_id"] not in net2vm_dependencies: |
| 4212 | net2vm_dependencies[interface["instance_net_id"]] = [] |
| 4213 | net2vm_dependencies[interface["instance_net_id"]].append(task_index) |
| 4214 | task_index += 1 |
| 4215 | |
| 4216 | # 2.3 deleting NETS |
| 4217 | # net_fail_list=[] |
| 4218 | for net in instanceDict['nets']: |
| 4219 | vimthread_affected[net["datacenter_tenant_id"]] = None |
| 4220 | datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4221 | if datacenter_key not in myvims: |
| 4222 | try: |
| gcalvino | d6fac4d | 2018-11-05 10:42:06 +0100 | [diff] [blame] | 4223 | _,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] | 4224 | except NfvoException as e: |
| 4225 | logger.error(str(e)) |
| 4226 | myvim_thread = None |
| 4227 | myvim_threads[datacenter_key] = myvim_thread |
| 4228 | vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"], |
| 4229 | datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 4230 | if len(vims) == 0: |
| 4231 | 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] | 4232 | myvims[datacenter_key] = None |
| 4233 | else: |
| 4234 | myvims[datacenter_key] = vims.values()[0] |
| 4235 | myvim = myvims[datacenter_key] |
| 4236 | myvim_thread = myvim_threads[datacenter_key] |
| 4237 | |
| 4238 | if not myvim: |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4239 | 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] | 4240 | continue |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4241 | extra = {"params": (net['vim_net_id'], net['sdn_net_id'])} |
| 4242 | if net2vm_dependencies.get(net["uuid"]): |
| 4243 | extra["depends_on"] = net2vm_dependencies[net["uuid"]] |
| 4244 | sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"] |
| 4245 | if len(sfi_dependencies) > 0: |
| 4246 | if "depends_on" in extra: |
| 4247 | extra["depends_on"] += sfi_dependencies |
| 4248 | else: |
| 4249 | extra["depends_on"] = sfi_dependencies |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4250 | db_vim_action = { |
| 4251 | "instance_action_id": instance_action_id, |
| 4252 | "task_index": task_index, |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4253 | "datacenter_vim_id": net["datacenter_tenant_id"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4254 | "action": "DELETE", |
| 4255 | "status": "SCHEDULED", |
| Eduardo Sousa | ab24d8b | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4256 | "item": "instance_nets", |
| 4257 | "item_id": net["uuid"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4258 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4259 | } |
| 4260 | task_index += 1 |
| 4261 | db_vim_actions.append(db_vim_action) |
| 4262 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4263 | db_instance_action["number_tasks"] = task_index |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4264 | |
| 4265 | # --> WIM |
| 4266 | wim_actions, db_instance_action = ( |
| 4267 | wim_engine.incorporate_actions(wim_actions, db_instance_action)) |
| 4268 | # <-- WIM |
| 4269 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4270 | db_tables = [ |
| 4271 | {"instance_actions": db_instance_action}, |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4272 | {"vim_wim_actions": db_vim_actions + wim_actions} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4273 | ] |
| 4274 | |
| 4275 | logger.debug("delete_instance done DB tables: %s", |
| 4276 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4277 | mydb.new_rows(db_tables, ()) |
| 4278 | for myvim_thread_id in vimthread_affected.keys(): |
| 4279 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| 4280 | |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4281 | wim_engine.dispatch(wim_actions) |
| 4282 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4283 | if len(error_msg) > 0: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4284 | return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\ |
| 4285 | '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4286 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4287 | return "action_id={} instance {} deleted".format(instance_action_id, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4288 | |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 4289 | def get_instance_id(mydb, tenant_id, instance_id): |
| 4290 | global ovim |
| 4291 | #check valid tenant_id |
| 4292 | check_tenant(mydb, tenant_id) |
| 4293 | #obtain data |
| 4294 | |
| 4295 | instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True) |
| 4296 | for net in instance_dict["nets"]: |
| 4297 | if net.get("sdn_net_id"): |
| 4298 | net_sdn = ovim.show_network(net["sdn_net_id"]) |
| 4299 | net["sdn_info"] = { |
| 4300 | "admin_state_up": net_sdn.get("admin_state_up"), |
| 4301 | "flows": net_sdn.get("flows"), |
| 4302 | "last_error": net_sdn.get("last_error"), |
| 4303 | "ports": net_sdn.get("ports"), |
| 4304 | "type": net_sdn.get("type"), |
| 4305 | "status": net_sdn.get("status"), |
| 4306 | "vlan": net_sdn.get("vlan"), |
| 4307 | } |
| 4308 | return instance_dict |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4309 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 4310 | @deprecated("Instance is automatically refreshed by vim_threads") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4311 | def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None): |
| 4312 | '''Refreshes a scenario instance. It modifies instanceDict''' |
| 4313 | '''Returns: |
| 4314 | - 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 |
| 4315 | - error_msg |
| 4316 | ''' |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4317 | # # Assumption: nfvo_tenant and instance_id were checked before entering into this function |
| 4318 | # #print "nfvo.refresh_instance begins" |
| 4319 | # #print json.dumps(instanceDict, indent=4) |
| 4320 | # |
| 4321 | # #print "Getting the VIM URL and the VIM tenant_id" |
| 4322 | # myvims={} |
| 4323 | # |
| 4324 | # # 1. Getting VIM vm and net list |
| 4325 | # vms_updated = [] #List of VM instance uuids in openmano that were updated |
| 4326 | # vms_notupdated=[] |
| 4327 | # vm_list = {} |
| 4328 | # for sce_vnf in instanceDict['vnfs']: |
| 4329 | # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4330 | # if datacenter_key not in vm_list: |
| 4331 | # vm_list[datacenter_key] = [] |
| 4332 | # if datacenter_key not in myvims: |
| 4333 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"], |
| 4334 | # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 4335 | # if len(vims) == 0: |
| 4336 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])) |
| 4337 | # myvims[datacenter_key] = None |
| 4338 | # else: |
| 4339 | # myvims[datacenter_key] = vims.values()[0] |
| 4340 | # for vm in sce_vnf['vms']: |
| 4341 | # vm_list[datacenter_key].append(vm['vim_vm_id']) |
| 4342 | # vms_notupdated.append(vm["uuid"]) |
| 4343 | # |
| 4344 | # nets_updated = [] #List of VM instance uuids in openmano that were updated |
| 4345 | # nets_notupdated=[] |
| 4346 | # net_list = {} |
| 4347 | # for net in instanceDict['nets']: |
| 4348 | # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4349 | # if datacenter_key not in net_list: |
| 4350 | # net_list[datacenter_key] = [] |
| 4351 | # if datacenter_key not in myvims: |
| 4352 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"], |
| 4353 | # datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 4354 | # if len(vims) == 0: |
| 4355 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 4356 | # myvims[datacenter_key] = None |
| 4357 | # else: |
| 4358 | # myvims[datacenter_key] = vims.values()[0] |
| 4359 | # |
| 4360 | # net_list[datacenter_key].append(net['vim_net_id']) |
| 4361 | # nets_notupdated.append(net["uuid"]) |
| 4362 | # |
| 4363 | # # 1. Getting the status of all VMs |
| 4364 | # vm_dict={} |
| 4365 | # for datacenter_key in myvims: |
| 4366 | # if not vm_list.get(datacenter_key): |
| 4367 | # continue |
| 4368 | # failed = True |
| 4369 | # failed_message="" |
| 4370 | # if not myvims[datacenter_key]: |
| 4371 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4372 | # else: |
| 4373 | # try: |
| 4374 | # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) ) |
| 4375 | # failed = False |
| 4376 | # except vimconn.vimconnException as e: |
| 4377 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4378 | # failed_message = str(e) |
| 4379 | # if failed: |
| 4380 | # for vm in vm_list[datacenter_key]: |
| 4381 | # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4382 | # |
| 4383 | # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed |
| 4384 | # for sce_vnf in instanceDict['vnfs']: |
| 4385 | # for vm in sce_vnf['vms']: |
| 4386 | # vm_id = vm['vim_vm_id'] |
| 4387 | # interfaces = vm_dict[vm_id].pop('interfaces', []) |
| 4388 | # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE |
| 4389 | # has_mgmt_iface = False |
| 4390 | # for iface in vm["interfaces"]: |
| 4391 | # if iface["type"]=="mgmt": |
| 4392 | # has_mgmt_iface = True |
| 4393 | # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface: |
| 4394 | # vm_dict[vm_id]['status'] = "ACTIVE" |
| 4395 | # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024: |
| 4396 | # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:] |
| 4397 | # 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'): |
| 4398 | # vm['status'] = vm_dict[vm_id]['status'] |
| 4399 | # vm['error_msg'] = vm_dict[vm_id].get('error_msg') |
| 4400 | # vm['vim_info'] = vm_dict[vm_id].get('vim_info') |
| 4401 | # # 2.1. Update in openmano DB the VMs whose status changed |
| 4402 | # try: |
| 4403 | # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]}) |
| 4404 | # vms_notupdated.remove(vm["uuid"]) |
| 4405 | # if updates>0: |
| 4406 | # vms_updated.append(vm["uuid"]) |
| 4407 | # except db_base_Exception as e: |
| 4408 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4409 | # # 2.2. Update in openmano DB the interface VMs |
| 4410 | # for interface in interfaces: |
| 4411 | # #translate from vim_net_id to instance_net_id |
| 4412 | # network_id_list=[] |
| 4413 | # for net in instanceDict['nets']: |
| 4414 | # if net["vim_net_id"] == interface["vim_net_id"]: |
| 4415 | # network_id_list.append(net["uuid"]) |
| 4416 | # if not network_id_list: |
| 4417 | # continue |
| 4418 | # del interface["vim_net_id"] |
| 4419 | # try: |
| 4420 | # for network_id in network_id_list: |
| 4421 | # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id}) |
| 4422 | # except db_base_Exception as e: |
| 4423 | # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id) |
| 4424 | # |
| 4425 | # # 3. Getting the status of all nets |
| 4426 | # net_dict = {} |
| 4427 | # for datacenter_key in myvims: |
| 4428 | # if not net_list.get(datacenter_key): |
| 4429 | # continue |
| 4430 | # failed = True |
| 4431 | # failed_message = "" |
| 4432 | # if not myvims[datacenter_key]: |
| 4433 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4434 | # else: |
| 4435 | # try: |
| 4436 | # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) ) |
| 4437 | # failed = False |
| 4438 | # except vimconn.vimconnException as e: |
| 4439 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4440 | # failed_message = str(e) |
| 4441 | # if failed: |
| 4442 | # for net in net_list[datacenter_key]: |
| 4443 | # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4444 | # |
| 4445 | # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed |
| 4446 | # # TODO: update nets inside a vnf |
| 4447 | # for net in instanceDict['nets']: |
| 4448 | # net_id = net['vim_net_id'] |
| 4449 | # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024: |
| 4450 | # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:] |
| 4451 | # 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'): |
| 4452 | # net['status'] = net_dict[net_id]['status'] |
| 4453 | # net['error_msg'] = net_dict[net_id].get('error_msg') |
| 4454 | # net['vim_info'] = net_dict[net_id].get('vim_info') |
| 4455 | # # 5.1. Update in openmano DB the nets whose status changed |
| 4456 | # try: |
| 4457 | # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]}) |
| 4458 | # nets_notupdated.remove(net["uuid"]) |
| 4459 | # if updated>0: |
| 4460 | # nets_updated.append(net["uuid"]) |
| 4461 | # except db_base_Exception as e: |
| 4462 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4463 | # |
| 4464 | # # Returns appropriate output |
| 4465 | # #print "nfvo.refresh_instance finishes" |
| 4466 | # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s", |
| 4467 | # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4468 | instance_id = instanceDict['uuid'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4469 | # if len(vms_notupdated)+len(nets_notupdated)>0: |
| 4470 | # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated) |
| 4471 | # 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] | 4472 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4473 | return 0, 'Scenario instance ' + instance_id + ' refreshed.' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4474 | |
| 4475 | def instance_action(mydb,nfvo_tenant,instance_id, action_dict): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4476 | #print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4477 | instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4478 | #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| 4479 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4480 | #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] | 4481 | vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id']) |
| 4482 | if len(vims) == 0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4483 | raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), httperrors.Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4484 | myvim = vims.values()[0] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4485 | vm_result = {} |
| 4486 | vm_error = 0 |
| 4487 | vm_ok = 0 |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4488 | |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4489 | myvim_threads_id = {} |
| 4490 | if action_dict.get("vdu-scaling"): |
| 4491 | db_instance_vms = [] |
| 4492 | db_vim_actions = [] |
| 4493 | db_instance_interfaces = [] |
| 4494 | instance_action_id = get_task_id() |
| 4495 | db_instance_action = { |
| 4496 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 4497 | "tenant_id": nfvo_tenant, |
| 4498 | "instance_id": instance_id, |
| 4499 | "description": "SCALE", |
| 4500 | } |
| 4501 | vm_result["instance_action_id"] = instance_action_id |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4502 | vm_result["created"] = [] |
| 4503 | vm_result["deleted"] = [] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4504 | task_index = 0 |
| 4505 | for vdu in action_dict["vdu-scaling"]: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4506 | vdu_id = vdu.get("vdu-id") |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4507 | osm_vdu_id = vdu.get("osm_vdu_id") |
| 4508 | member_vnf_index = vdu.get("member-vnf-index") |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4509 | vdu_count = vdu.get("count", 1) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4510 | if vdu_id: |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4511 | target_vms = mydb.get_rows( |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4512 | FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid", |
| 4513 | WHERE={"vms.uuid": vdu_id}, |
| 4514 | ORDER_BY="vms.created_at" |
| 4515 | ) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4516 | if not target_vms: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4517 | 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] | 4518 | else: |
| 4519 | if not osm_vdu_id and not member_vnf_index: |
| tierno | a43bd9e | 2018-11-26 09:28:58 +0000 | [diff] [blame] | 4520 | 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] | 4521 | target_vms = mydb.get_rows( |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4522 | # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"), |
| 4523 | FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\ |
| 4524 | " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\ |
| 4525 | " join vms on ivms.vm_id=vms.uuid", |
| tierno | a43bd9e | 2018-11-26 09:28:58 +0000 | [diff] [blame] | 4526 | WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index, |
| 4527 | "ivnfs.instance_scenario_id": instance_id}, |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4528 | ORDER_BY="ivms.created_at" |
| 4529 | ) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4530 | if not target_vms: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4531 | 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] | 4532 | vdu_id = target_vms[-1]["uuid"] |
| 4533 | target_vm = target_vms[-1] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4534 | datacenter = target_vm["datacenter_id"] |
| 4535 | myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4536 | |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4537 | if vdu["type"] == "delete": |
| 4538 | for index in range(0, vdu_count): |
| 4539 | target_vm = target_vms[-1-index] |
| 4540 | vdu_id = target_vm["uuid"] |
| 4541 | # look for nm |
| 4542 | vm_interfaces = None |
| 4543 | for sce_vnf in instanceDict['vnfs']: |
| 4544 | for vm in sce_vnf['vms']: |
| 4545 | if vm["uuid"] == vdu_id: |
| 4546 | vm_interfaces = vm["interfaces"] |
| 4547 | break |
| 4548 | |
| 4549 | db_vim_action = { |
| 4550 | "instance_action_id": instance_action_id, |
| 4551 | "task_index": task_index, |
| 4552 | "datacenter_vim_id": target_vm["datacenter_tenant_id"], |
| 4553 | "action": "DELETE", |
| 4554 | "status": "SCHEDULED", |
| 4555 | "item": "instance_vms", |
| 4556 | "item_id": vdu_id, |
| 4557 | "extra": yaml.safe_dump({"params": vm_interfaces}, |
| 4558 | default_flow_style=True, width=256) |
| 4559 | } |
| 4560 | task_index += 1 |
| 4561 | db_vim_actions.append(db_vim_action) |
| 4562 | vm_result["deleted"].append(vdu_id) |
| 4563 | # delete from database |
| 4564 | db_instance_vms.append({"TO-DELETE": vdu_id}) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4565 | |
| 4566 | else: # vdu["type"] == "create": |
| 4567 | iface2iface = {} |
| 4568 | where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"} |
| 4569 | |
| garciadeblas | 72cd59f | 2018-12-05 10:59:40 +0100 | [diff] [blame] | 4570 | vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4571 | if not vim_action_to_clone: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4572 | 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] | 4573 | vim_action_to_clone = vim_action_to_clone[0] |
| 4574 | extra = yaml.safe_load(vim_action_to_clone["extra"]) |
| 4575 | |
| 4576 | # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y |
| 4577 | # TODO do the same for flavor and image when available |
| 4578 | task_depends_on = [] |
| 4579 | task_params = extra["params"] |
| 4580 | task_params_networks = deepcopy(task_params[5]) |
| 4581 | for iface in task_params[5]: |
| 4582 | if iface["net_id"].startswith("TASK-"): |
| 4583 | if "." not in iface["net_id"]: |
| 4584 | task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4585 | iface["net_id"][5:])) |
| 4586 | iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4587 | iface["net_id"][5:]) |
| 4588 | else: |
| 4589 | task_depends_on.append(iface["net_id"][5:]) |
| 4590 | if "mac_address" in iface: |
| 4591 | del iface["mac_address"] |
| 4592 | |
| 4593 | vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]}) |
| 4594 | for index in range(0, vdu_count): |
| 4595 | vm_uuid = str(uuid4()) |
| 4596 | vm_name = target_vm.get('vim_name') |
| 4597 | try: |
| 4598 | suffix = vm_name.rfind("-") |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4599 | 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] | 4600 | except Exception: |
| 4601 | pass |
| 4602 | db_instance_vm = { |
| 4603 | "uuid": vm_uuid, |
| 4604 | 'instance_vnf_id': target_vm['instance_vnf_id'], |
| 4605 | 'vm_id': target_vm['vm_id'], |
| 4606 | 'vim_name': vm_name |
| 4607 | } |
| 4608 | db_instance_vms.append(db_instance_vm) |
| 4609 | |
| 4610 | for vm_iface in vm_ifaces_to_clone: |
| 4611 | iface_uuid = str(uuid4()) |
| 4612 | iface2iface[vm_iface["uuid"]] = iface_uuid |
| 4613 | db_vm_iface = { |
| 4614 | "uuid": iface_uuid, |
| 4615 | 'instance_vm_id': vm_uuid, |
| 4616 | "instance_net_id": vm_iface["instance_net_id"], |
| 4617 | 'interface_id': vm_iface['interface_id'], |
| 4618 | 'type': vm_iface['type'], |
| 4619 | 'floating_ip': vm_iface['floating_ip'], |
| 4620 | 'port_security': vm_iface['port_security'] |
| 4621 | } |
| 4622 | db_instance_interfaces.append(db_vm_iface) |
| 4623 | task_params_copy = deepcopy(task_params) |
| 4624 | for iface in task_params_copy[5]: |
| 4625 | iface["uuid"] = iface2iface[iface["uuid"]] |
| 4626 | # increment ip_address |
| 4627 | if "ip_address" in iface: |
| 4628 | ip = iface.get("ip_address") |
| 4629 | i = ip.rfind(".") |
| 4630 | if i > 0: |
| 4631 | try: |
| 4632 | i += 1 |
| 4633 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 4634 | iface["ip_address"] = ip |
| 4635 | except: |
| 4636 | iface["ip_address"] = None |
| 4637 | if vm_name: |
| 4638 | task_params_copy[0] = vm_name |
| 4639 | db_vim_action = { |
| 4640 | "instance_action_id": instance_action_id, |
| 4641 | "task_index": task_index, |
| 4642 | "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"], |
| 4643 | "action": "CREATE", |
| 4644 | "status": "SCHEDULED", |
| 4645 | "item": "instance_vms", |
| 4646 | "item_id": vm_uuid, |
| 4647 | # ALF |
| 4648 | # ALF |
| 4649 | # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd. |
| 4650 | # ALF |
| 4651 | # ALF |
| 4652 | "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256) |
| 4653 | } |
| 4654 | task_index += 1 |
| 4655 | db_vim_actions.append(db_vim_action) |
| tierno | 67881db | 2018-10-24 18:46:03 +0200 | [diff] [blame] | 4656 | vm_result["created"].append(vm_uuid) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4657 | |
| 4658 | db_instance_action["number_tasks"] = task_index |
| 4659 | db_tables = [ |
| 4660 | {"instance_vms": db_instance_vms}, |
| 4661 | {"instance_interfaces": db_instance_interfaces}, |
| 4662 | {"instance_actions": db_instance_action}, |
| 4663 | # TODO revise sfps |
| 4664 | # {"instance_sfis": db_instance_sfis}, |
| 4665 | # {"instance_sfs": db_instance_sfs}, |
| 4666 | # {"instance_classifications": db_instance_classifications}, |
| 4667 | # {"instance_sfps": db_instance_sfps}, |
| garciadeblas | aba7a0d | 2018-12-05 12:42:35 +0100 | [diff] [blame] | 4668 | {"vim_wim_actions": db_vim_actions} |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4669 | ] |
| 4670 | logger.debug("create_vdu done DB tables: %s", |
| 4671 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4672 | mydb.new_rows(db_tables, []) |
| 4673 | for myvim_thread in myvim_threads_id.values(): |
| 4674 | vim_threads["running"][myvim_thread].insert_task(db_vim_actions) |
| 4675 | |
| 4676 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4677 | |
| 4678 | input_vnfs = action_dict.pop("vnfs", []) |
| 4679 | input_vms = action_dict.pop("vms", []) |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4680 | 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] | 4681 | for sce_vnf in instanceDict['vnfs']: |
| 4682 | for vm in sce_vnf['vms']: |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4683 | if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \ |
| 4684 | sce_vnf['member_vnf_index'] not in input_vnfs and \ |
| 4685 | vm['uuid'] not in input_vms and vm['name'] not in input_vms: |
| 4686 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4687 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4688 | if "add_public_key" in action_dict: |
| 4689 | mgmt_access = {} |
| 4690 | if sce_vnf.get('mgmt_access'): |
| 4691 | mgmt_access = yaml.load(sce_vnf['mgmt_access']) |
| 4692 | ssh_access = mgmt_access['config-access']['ssh-access'] |
| 4693 | tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4694 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4695 | if ssh_access['required'] and ssh_access['default-user']: |
| 4696 | if 'ip_address' in vm: |
| 4697 | mgmt_ip = vm['ip_address'].split(';') |
| 4698 | password = mgmt_access['config-access'].get('password') |
| 4699 | priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid']) |
| 4700 | myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'], |
| 4701 | action_dict['add_public_key'], |
| 4702 | password=password, ro_key=priv_RO_key) |
| 4703 | else: |
| 4704 | 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] | 4705 | httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4706 | except KeyError: |
| 4707 | 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] | 4708 | httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4709 | else: |
| 4710 | 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] | 4711 | httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4712 | else: |
| 4713 | data = myvim.action_vminstance(vm['vim_vm_id'], action_dict) |
| 4714 | if "console" in action_dict: |
| 4715 | if not global_config["http_console_proxy"]: |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4716 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4717 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4718 | protocol=data["protocol"], |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4719 | ip = data["server"], |
| 4720 | port = data["port"], |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4721 | suffix = data["suffix"]), |
| 4722 | "name":vm['name'] |
| 4723 | } |
| 4724 | vm_ok +=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4725 | elif data["server"]=="127.0.0.1" or data["server"]=="localhost": |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4726 | vm_result[ vm['uuid'] ] = {"vim_result": -httperrors.Unauthorized, |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4727 | "description": "this console is only reachable by local interface", |
| 4728 | "name":vm['name'] |
| 4729 | } |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4730 | vm_error+=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4731 | else: |
| 4732 | #print "console data", data |
| 4733 | try: |
| 4734 | console_thread = create_or_use_console_proxy_thread(data["server"], data["port"]) |
| 4735 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4736 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4737 | protocol=data["protocol"], |
| 4738 | ip = global_config["http_console_host"], |
| 4739 | port = console_thread.port, |
| 4740 | suffix = data["suffix"]), |
| 4741 | "name":vm['name'] |
| 4742 | } |
| 4743 | vm_ok +=1 |
| 4744 | except NfvoException as e: |
| 4745 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4746 | vm_error+=1 |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4747 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4748 | else: |
| 4749 | vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']} |
| 4750 | vm_ok +=1 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4751 | except vimconn.vimconnException as e: |
| 4752 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4753 | vm_error+=1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4754 | |
| 4755 | if vm_ok==0: #all goes wrong |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4756 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4757 | else: |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4758 | return vm_result |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4759 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4760 | def instance_action_get(mydb, nfvo_tenant, instance_id, action_id): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4761 | filter = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4762 | if nfvo_tenant and nfvo_tenant != "any": |
| 4763 | filter["tenant_id"] = nfvo_tenant |
| 4764 | if instance_id and instance_id != "any": |
| 4765 | filter["instance_id"] = instance_id |
| 4766 | if action_id: |
| 4767 | filter["uuid"] = action_id |
| 4768 | rows = mydb.get_rows(FROM="instance_actions", WHERE=filter) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4769 | if action_id: |
| 4770 | if not rows: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4771 | raise NfvoException("Not found any action with this criteria", httperrors.Not_Found) |
| 4772 | vim_wim_actions = mydb.get_rows(FROM="vim_wim_actions", WHERE={"instance_action_id": action_id}) |
| 4773 | rows[0]["vim_wim_actions"] = vim_wim_actions |
| tierno | 31e121f | 2018-12-03 12:04:48 +0000 | [diff] [blame] | 4774 | # for backward compatibility set vim_actions = vim_wim_actions |
| 4775 | rows[0]["vim_actions"] = vim_wim_actions |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4776 | return {"actions": rows} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4777 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4778 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4779 | def create_or_use_console_proxy_thread(console_server, console_port): |
| 4780 | #look for a non-used port |
| 4781 | console_thread_key = console_server + ":" + str(console_port) |
| 4782 | if console_thread_key in global_config["console_thread"]: |
| 4783 | #global_config["console_thread"][console_thread_key].start_timeout() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4784 | return global_config["console_thread"][console_thread_key] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4785 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4786 | for port in global_config["console_port_iterator"](): |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4787 | #print "create_or_use_console_proxy_thread() port:", port |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4788 | if port in global_config["console_ports"]: |
| 4789 | continue |
| 4790 | try: |
| 4791 | clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port) |
| 4792 | clithread.start() |
| 4793 | global_config["console_thread"][console_thread_key] = clithread |
| 4794 | global_config["console_ports"][port] = console_thread_key |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4795 | return clithread |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4796 | except cli.ConsoleProxyExceptionPortUsed as e: |
| 4797 | #port used, try with onoher |
| 4798 | continue |
| 4799 | except cli.ConsoleProxyException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4800 | raise NfvoException(str(e), httperrors.Bad_Request) |
| 4801 | raise NfvoException("Not found any free 'http_console_ports'", httperrors.Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4802 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4803 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4804 | def check_tenant(mydb, tenant_id): |
| 4805 | '''check that tenant exists at database''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4806 | tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id}) |
| 4807 | if not tenant: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4808 | raise NfvoException("tenant '{}' not found".format(tenant_id), httperrors.Not_Found) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4809 | return |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4810 | |
| 4811 | def new_tenant(mydb, tenant_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4812 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4813 | tenant_uuid = str(uuid4()) |
| 4814 | tenant_dict['uuid'] = tenant_uuid |
| 4815 | try: |
| 4816 | pub_key, priv_key = create_RO_keypair(tenant_uuid) |
| 4817 | tenant_dict['RO_pub_key'] = pub_key |
| 4818 | tenant_dict['encrypted_RO_priv_key'] = priv_key |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4819 | mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4820 | except db_base_Exception as e: |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 4821 | 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] | 4822 | return tenant_uuid |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4823 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4824 | def delete_tenant(mydb, tenant): |
| 4825 | #get nfvo_tenant info |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4826 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4827 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant') |
| 4828 | mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid']) |
| 4829 | return tenant_dict['uuid'] + " " + tenant_dict["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4830 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4831 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4832 | def new_datacenter(mydb, datacenter_descriptor): |
| tierno | 1c848c0 | 2018-05-21 16:40:33 +0200 | [diff] [blame] | 4833 | sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4834 | if "config" in datacenter_descriptor: |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4835 | sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None) |
| 4836 | datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True, |
| 4837 | width=256) |
| 4838 | # Check that datacenter-type is correct |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4839 | datacenter_type = datacenter_descriptor.get("type", "openvim"); |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4840 | # module_info = None |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4841 | try: |
| 4842 | module = "vimconn_" + datacenter_type |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4843 | pkg = __import__("osm_ro." + module) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4844 | # vim_conn = getattr(pkg, module) |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4845 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4846 | except (IOError, ImportError): |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4847 | # if module_info and module_info[0]: |
| 4848 | # file.close(module_info[0]) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4849 | raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type, |
| 4850 | module), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4851 | httperrors.Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4852 | |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4853 | 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] | 4854 | if sdn_port_mapping: |
| 4855 | try: |
| 4856 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping) |
| 4857 | except Exception as e: |
| 4858 | mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback |
| 4859 | raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4860 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4861 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4862 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4863 | def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4864 | # obtain data, check that only one exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4865 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4866 | |
| 4867 | # edit data |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4868 | datacenter_id = datacenter['uuid'] |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4869 | where = {'uuid': datacenter['uuid']} |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4870 | remove_port_mapping = False |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4871 | new_sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4872 | if "config" in datacenter_descriptor: |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4873 | if datacenter_descriptor['config'] != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4874 | try: |
| 4875 | new_config_dict = datacenter_descriptor["config"] |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4876 | if "sdn-port-mapping" in new_config_dict: |
| 4877 | remove_port_mapping = True |
| 4878 | new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping") |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4879 | # delete null fields |
| 4880 | to_delete = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4881 | for k in new_config_dict: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4882 | if new_config_dict[k] is None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4883 | to_delete.append(k) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4884 | if k == 'sdn-controller': |
| 4885 | remove_port_mapping = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4886 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4887 | config_text = datacenter.get("config") |
| 4888 | if not config_text: |
| 4889 | config_text = '{}' |
| 4890 | config_dict = yaml.load(config_text) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4891 | config_dict.update(new_config_dict) |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4892 | # delete null fields |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4893 | for k in to_delete: |
| 4894 | del config_dict[k] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4895 | except Exception as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4896 | raise NfvoException("Bad format at datacenter:config " + str(e), httperrors.Bad_Request) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4897 | if config_dict: |
| 4898 | datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256) |
| 4899 | else: |
| 4900 | datacenter_descriptor["config"] = None |
| 4901 | if remove_port_mapping: |
| 4902 | try: |
| 4903 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id) |
| 4904 | except ovimException as e: |
| Anderson Bravalheri | c5293de | 2018-11-28 17:21:26 +0000 | [diff] [blame] | 4905 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e), httperrors.Conflict) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4906 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4907 | mydb.update_rows('datacenters', datacenter_descriptor, where) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4908 | if new_sdn_port_mapping: |
| 4909 | try: |
| 4910 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping) |
| 4911 | except ovimException as e: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4912 | # Rollback |
| 4913 | mydb.update_rows('datacenters', datacenter, where) |
| Anderson Bravalheri | c5293de | 2018-11-28 17:21:26 +0000 | [diff] [blame] | 4914 | raise NfvoException("Error adding datacenter-port-mapping " + str(e), httperrors.Conflict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4915 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4916 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4917 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4918 | def delete_datacenter(mydb, datacenter): |
| 4919 | #get nfvo_tenant info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4920 | datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter') |
| 4921 | mydb.delete_row_by_id("datacenters", datacenter_dict['uuid']) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4922 | try: |
| 4923 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid']) |
| 4924 | except ovimException as e: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4925 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4926 | return datacenter_dict['uuid'] + " " + datacenter_dict['name'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4927 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4928 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4929 | def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None, |
| 4930 | vim_username=None, vim_password=None, config=None): |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 4931 | # get datacenter info |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4932 | try: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4933 | if not datacenter_id: |
| 4934 | if not vim_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4935 | raise NfvoException("You must provide 'vim_id", http_code=httperrors.Bad_Request) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4936 | datacenter_id = vim_id |
| 4937 | datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4938 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4939 | 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] | 4940 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4941 | # get nfvo_tenant info |
| 4942 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant) |
| 4943 | if vim_tenant_name==None: |
| 4944 | vim_tenant_name=tenant_dict['name'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4945 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4946 | tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id } |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4947 | # #check that this association does not exist before |
| 4948 | # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 4949 | # if len(tenants_datacenters)>0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4950 | # 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] | 4951 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4952 | vim_tenant_id_exist_atdb=False |
| 4953 | if not create_vim_tenant: |
| 4954 | where_={"datacenter_id": datacenter_id} |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4955 | if vim_tenant!=None: |
| 4956 | where_["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4957 | if vim_tenant_name!=None: |
| 4958 | where_["vim_tenant_name"] = vim_tenant_name |
| 4959 | #check if vim_tenant_id is already at database |
| 4960 | datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_) |
| 4961 | if len(datacenter_tenants_dict)>=1: |
| 4962 | datacenter_tenants_dict = datacenter_tenants_dict[0] |
| 4963 | vim_tenant_id_exist_atdb=True |
| 4964 | #TODO check if a field has changed and edit entry at datacenter_tenants at DB |
| 4965 | else: #result=0 |
| 4966 | datacenter_tenants_dict = {} |
| 4967 | #insert at table datacenter_tenants |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4968 | else: #if vim_tenant==None: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4969 | #create tenant at VIM if not provided |
| 4970 | try: |
| 4971 | _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, |
| 4972 | vim_passwd=vim_password) |
| 4973 | datacenter_name = myvim["name"] |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4974 | 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] | 4975 | except vimconn.vimconnException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 4976 | 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] | 4977 | datacenter_tenants_dict = {} |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4978 | datacenter_tenants_dict["created"]="true" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4979 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4980 | #fill datacenter_tenants table |
| 4981 | if not vim_tenant_id_exist_atdb: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4982 | datacenter_tenants_dict["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4983 | datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name |
| 4984 | datacenter_tenants_dict["user"] = vim_username |
| 4985 | datacenter_tenants_dict["passwd"] = vim_password |
| 4986 | datacenter_tenants_dict["datacenter_id"] = datacenter_id |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4987 | if name: |
| 4988 | datacenter_tenants_dict["name"] = name |
| 4989 | else: |
| 4990 | datacenter_tenants_dict["name"] = datacenter_name |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4991 | if config: |
| 4992 | datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256) |
| 4993 | id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True) |
| 4994 | datacenter_tenants_dict["uuid"] = id_ |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4995 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4996 | #fill tenants_datacenters table |
| 4997 | datacenter_tenant_id = datacenter_tenants_dict["uuid"] |
| 4998 | tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id |
| 4999 | mydb.new_row('tenants_datacenters', tenants_datacenter_dict) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5000 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5001 | # create thread |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5002 | 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] | 5003 | 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] | 5004 | db=db, db_lock=db_lock, ovim=ovim) |
| 5005 | new_thread.start() |
| 5006 | thread_id = datacenter_tenants_dict["uuid"] |
| 5007 | vim_threads["running"][thread_id] = new_thread |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5008 | return thread_id |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5009 | except vimconn.vimconnException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5010 | raise NfvoException(str(e), httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5011 | |
| tierno | 9931490 | 2017-04-26 13:23:09 +0200 | [diff] [blame] | 5012 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5013 | def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None, |
| 5014 | 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] | 5015 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5016 | # get vim_account; check is valid for this tenant |
| 5017 | from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id" |
| 5018 | where_ = {"td.nfvo_tenant_id": nfvo_tenant} |
| 5019 | if datacenter_tenant_id: |
| 5020 | where_["dt.uuid"] = datacenter_tenant_id |
| 5021 | if datacenter_id: |
| 5022 | where_["dt.datacenter_id"] = datacenter_id |
| 5023 | vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_) |
| 5024 | if not vim_accounts: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5025 | 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] | 5026 | elif len(vim_accounts) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5027 | 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] | 5028 | datacenter_tenant_id = vim_accounts[0]["uuid"] |
| 5029 | original_config = vim_accounts[0]["config"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5030 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5031 | update_ = {} |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5032 | if config: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5033 | original_config_dict = yaml.load(original_config) |
| 5034 | original_config_dict.update(config) |
| 5035 | update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256) |
| 5036 | if name: |
| 5037 | update_['name'] = name |
| 5038 | if vim_tenant: |
| 5039 | update_['vim_tenant_id'] = vim_tenant |
| 5040 | if vim_tenant_name: |
| 5041 | update_['vim_tenant_name'] = vim_tenant_name |
| 5042 | if vim_username: |
| 5043 | update_['user'] = vim_username |
| 5044 | if vim_password: |
| 5045 | update_['passwd'] = vim_password |
| 5046 | if update_: |
| 5047 | 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] | 5048 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5049 | vim_threads["running"][datacenter_tenant_id].insert_task("reload") |
| 5050 | return datacenter_tenant_id |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5051 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5052 | def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5053 | #get nfvo_tenant info |
| 5054 | if not tenant_id or tenant_id=="any": |
| 5055 | tenant_uuid = None |
| 5056 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5057 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5058 | tenant_uuid = tenant_dict['uuid'] |
| 5059 | |
| 5060 | #check that this association exist before |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 5061 | tenants_datacenter_dict = {} |
| 5062 | if datacenter: |
| 5063 | datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter) |
| 5064 | tenants_datacenter_dict["datacenter_id"] = datacenter_id |
| 5065 | elif vim_account_id: |
| 5066 | tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5067 | if tenant_uuid: |
| 5068 | tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5069 | tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 5070 | if len(tenant_datacenter_list)==0 and tenant_uuid: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5071 | 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] | 5072 | |
| 5073 | #delete this association |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5074 | mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5075 | |
| 5076 | #get vim_tenant info and deletes |
| 5077 | warning='' |
| 5078 | for tenant_datacenter_item in tenant_datacenter_list: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5079 | vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 5080 | #try to delete vim:tenant |
| 5081 | try: |
| 5082 | mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 5083 | if vim_tenant_dict['created']=='true': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5084 | #delete tenant at VIM if created by NFVO |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5085 | try: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 5086 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5087 | myvim.delete_tenant(vim_tenant_dict['vim_tenant_id']) |
| 5088 | except vimconn.vimconnException as e: |
| 5089 | warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e)) |
| 5090 | logger.warn(warning) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5091 | except db_base_Exception as e: |
| 5092 | logger.error("Cannot delete datacenter_tenants " + str(e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5093 | 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] | 5094 | thread_id = tenant_datacenter_item["datacenter_tenant_id"] |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 5095 | thread = vim_threads["running"].get(thread_id) |
| 5096 | if thread: |
| 5097 | thread.insert_task("exit") |
| 5098 | vim_threads["deleting"][thread_id] = thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5099 | return "datacenter {} detached. {}".format(datacenter_id, warning) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5100 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5101 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5102 | def datacenter_action(mydb, tenant_id, datacenter, action_dict): |
| 5103 | #DEPRECATED |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5104 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5105 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5106 | |
| 5107 | if 'net-update' in action_dict: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5108 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5109 | 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] | 5110 | #print content |
| 5111 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5112 | #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] | 5113 | raise NfvoException(str(e), httperrors.Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5114 | #update nets Change from VIM format to NFVO format |
| 5115 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5116 | for net in nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5117 | net_nfvo={'datacenter_id': datacenter_id} |
| 5118 | net_nfvo['name'] = net['name'] |
| 5119 | #net_nfvo['description']= net['name'] |
| 5120 | net_nfvo['vim_net_id'] = net['id'] |
| 5121 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 5122 | net_nfvo['shared'] = net['shared'] |
| 5123 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| 5124 | net_list.append(net_nfvo) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5125 | inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list) |
| 5126 | logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted) |
| 5127 | return inserted |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5128 | elif 'net-edit' in action_dict: |
| 5129 | net = action_dict['net-edit'].pop('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5130 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5131 | result = mydb.update_rows('datacenter_nets', action_dict['net-edit'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5132 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5133 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5134 | elif 'net-delete' in action_dict: |
| 5135 | net = action_dict['net-deelte'].get('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5136 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5137 | result = mydb.delete_row(FROM='datacenter_nets', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5138 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5139 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5140 | |
| 5141 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5142 | raise NfvoException("Unknown action " + str(action_dict), httperrors.Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5143 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5144 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5145 | def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): |
| 5146 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5147 | datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5148 | |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5149 | what = 'uuid' if utils.check_valid_uuid(netmap) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5150 | result = mydb.update_rows('datacenter_nets', action_dict['netmap'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5151 | WHERE={'datacenter_id':datacenter_id, what: netmap}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5152 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5153 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5154 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5155 | def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): |
| 5156 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5157 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5158 | filter_dict={} |
| 5159 | if action_dict: |
| 5160 | action_dict = action_dict["netmap"] |
| 5161 | if 'vim_id' in action_dict: |
| 5162 | filter_dict["id"] = action_dict['vim_id'] |
| 5163 | if 'vim_name' in action_dict: |
| 5164 | filter_dict["name"] = action_dict['vim_name'] |
| 5165 | else: |
| 5166 | filter_dict["shared"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5167 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5168 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5169 | vim_nets = myvim.get_network_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5170 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5171 | #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] | 5172 | raise NfvoException(str(e), httperrors.Internal_Server_Error) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5173 | if len(vim_nets)>1 and action_dict: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5174 | raise NfvoException("more than two networks found, specify with vim_id", httperrors.Conflict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5175 | elif len(vim_nets)==0: # and action_dict: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5176 | 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] | 5177 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5178 | for net in vim_nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5179 | net_nfvo={'datacenter_id': datacenter_id} |
| 5180 | if action_dict and "name" in action_dict: |
| 5181 | net_nfvo['name'] = action_dict['name'] |
| 5182 | else: |
| 5183 | net_nfvo['name'] = net['name'] |
| 5184 | #net_nfvo['description']= net['name'] |
| 5185 | net_nfvo['vim_net_id'] = net['id'] |
| 5186 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 5187 | net_nfvo['shared'] = net['shared'] |
| 5188 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5189 | try: |
| 5190 | net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5191 | net_nfvo["status"] = "OK" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5192 | net_nfvo["uuid"] = net_id |
| 5193 | except db_base_Exception as e: |
| 5194 | if action_dict: |
| 5195 | raise |
| 5196 | else: |
| 5197 | net_nfvo["status"] = "FAIL: " + str(e) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5198 | net_list.append(net_nfvo) |
| 5199 | return net_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5200 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5201 | def get_sdn_net_id(mydb, tenant_id, datacenter, network_id): |
| 5202 | # obtain all network data |
| 5203 | try: |
| 5204 | if utils.check_valid_uuid(network_id): |
| 5205 | filter_dict = {"id": network_id} |
| 5206 | else: |
| 5207 | filter_dict = {"name": network_id} |
| 5208 | |
| 5209 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 5210 | network = myvim.get_network_list(filter_dict=filter_dict) |
| 5211 | except vimconn.vimconnException as e: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 5212 | 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] | 5213 | |
| 5214 | # ensure the network is defined |
| 5215 | if len(network) == 0: |
| 5216 | raise NfvoException("Network {} is not present in the system".format(network_id), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5217 | httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5218 | |
| 5219 | # ensure there is only one network with the provided name |
| 5220 | if len(network) > 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5221 | 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] | 5222 | |
| 5223 | # ensure it is a dataplane network |
| 5224 | if network[0]['type'] != 'data': |
| 5225 | return None |
| 5226 | |
| 5227 | # ensure we use the id |
| 5228 | network_id = network[0]['id'] |
| 5229 | |
| 5230 | # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id |
| 5231 | # and with instance_scenario_id==NULL |
| 5232 | #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None} |
| 5233 | search_dict = {'vim_net_id': network_id} |
| 5234 | |
| 5235 | try: |
| 5236 | #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id'] |
| 5237 | result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict) |
| 5238 | except db_base_Exception as e: |
| 5239 | 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] | 5240 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5241 | |
| 5242 | sdn_net_counter = 0 |
| 5243 | for net in result: |
| 5244 | if net['sdn_net_id'] != None: |
| 5245 | sdn_net_counter+=1 |
| 5246 | sdn_net_id = net['sdn_net_id'] |
| 5247 | |
| 5248 | if sdn_net_counter == 0: |
| 5249 | return None |
| 5250 | elif sdn_net_counter == 1: |
| 5251 | return sdn_net_id |
| 5252 | else: |
| 5253 | 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] | 5254 | network_id), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5255 | |
| 5256 | def get_sdn_controller_id(mydb, datacenter): |
| 5257 | # Obtain sdn controller id |
| 5258 | config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}') |
| 5259 | if not config: |
| 5260 | return None |
| 5261 | |
| 5262 | return yaml.load(config).get('sdn-controller') |
| 5263 | |
| 5264 | def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor): |
| 5265 | try: |
| 5266 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5267 | if not sdn_network_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5268 | 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] | 5269 | |
| 5270 | #Obtain sdn controller id |
| 5271 | controller_id = get_sdn_controller_id(mydb, datacenter) |
| 5272 | if not controller_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5273 | 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] | 5274 | |
| 5275 | #Obtain sdn controller info |
| 5276 | sdn_controller = ovim.show_of_controller(controller_id) |
| 5277 | |
| 5278 | port_data = { |
| 5279 | 'name': 'external_port', |
| 5280 | 'net_id': sdn_network_id, |
| 5281 | 'ofc_id': controller_id, |
| 5282 | 'switch_dpid': sdn_controller['dpid'], |
| 5283 | 'switch_port': descriptor['port'] |
| 5284 | } |
| 5285 | |
| 5286 | if 'vlan' in descriptor: |
| 5287 | port_data['vlan'] = descriptor['vlan'] |
| 5288 | if 'mac' in descriptor: |
| 5289 | port_data['mac'] = descriptor['mac'] |
| 5290 | |
| 5291 | result = ovim.new_port(port_data) |
| 5292 | except ovimException as e: |
| 5293 | raise NfvoException("ovimException attaching SDN network {} to vim network {}".format( |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5294 | sdn_network_id, network_id) + str(e), httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5295 | except db_base_Exception as e: |
| 5296 | raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5297 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5298 | |
| 5299 | return 'Port uuid: '+ result |
| 5300 | |
| 5301 | def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None): |
| 5302 | if port_id: |
| 5303 | filter = {'uuid': port_id} |
| 5304 | else: |
| 5305 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5306 | if not sdn_network_id: |
| 5307 | 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] | 5308 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5309 | #in case no port_id is specified only ports marked as 'external_port' will be detached |
| 5310 | filter = {'name': 'external_port', 'net_id': sdn_network_id} |
| 5311 | |
| 5312 | try: |
| 5313 | port_list = ovim.get_ports(columns={'uuid'}, filter=filter) |
| 5314 | except ovimException as e: |
| 5315 | 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] | 5316 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5317 | |
| 5318 | if len(port_list) == 0: |
| 5319 | 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] | 5320 | httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5321 | |
| 5322 | port_uuid_list = [] |
| 5323 | for port in port_list: |
| 5324 | try: |
| 5325 | port_uuid_list.append(port['uuid']) |
| 5326 | ovim.delete_port(port['uuid']) |
| 5327 | except ovimException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5328 | 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] | 5329 | |
| 5330 | return 'Detached ports uuid: {}'.format(','.join(port_uuid_list)) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5331 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5332 | def vim_action_get(mydb, tenant_id, datacenter, item, name): |
| 5333 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5334 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5335 | filter_dict={} |
| 5336 | if name: |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5337 | if utils.check_valid_uuid(name): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5338 | filter_dict["id"] = name |
| 5339 | else: |
| 5340 | filter_dict["name"] = name |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5341 | try: |
| 5342 | if item=="networks": |
| 5343 | #filter_dict['tenant_id'] = myvim['tenant_id'] |
| 5344 | content = myvim.get_network_list(filter_dict=filter_dict) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5345 | |
| 5346 | if len(content) == 0: |
| 5347 | raise NfvoException("Network {} is not present in the system. ".format(name), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5348 | httperrors.Bad_Request) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5349 | |
| 5350 | #Update the networks with the attached ports |
| 5351 | for net in content: |
| 5352 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id']) |
| 5353 | if sdn_network_id != None: |
| 5354 | try: |
| 5355 | #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id}) |
| 5356 | port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id}) |
| 5357 | except ovimException as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5358 | 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] | 5359 | #Remove field name and if port name is external_port save it as 'type' |
| 5360 | for port in port_list: |
| 5361 | if port['name'] == 'external_port': |
| 5362 | port['type'] = "External" |
| 5363 | del port['name'] |
| 5364 | net['sdn_network_id'] = sdn_network_id |
| 5365 | net['sdn_attached_ports'] = port_list |
| 5366 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5367 | elif item=="tenants": |
| 5368 | content = myvim.get_tenant_list(filter_dict=filter_dict) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5369 | elif item == "images": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5370 | |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5371 | content = myvim.get_image_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5372 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5373 | raise NfvoException(item + "?", httperrors.Method_Not_Allowed) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 5374 | 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] | 5375 | if name and len(content)==1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5376 | return {item[:-1]: content[0]} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5377 | elif name and len(content)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5378 | 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] | 5379 | datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5380 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5381 | return {item: content} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5382 | except vimconn.vimconnException as e: |
| 5383 | 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] | 5384 | 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] | 5385 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5386 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5387 | def vim_action_delete(mydb, tenant_id, datacenter, item, name): |
| 5388 | #get datacenter info |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5389 | if tenant_id == "any": |
| 5390 | tenant_id=None |
| 5391 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5392 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5393 | #get uuid name |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5394 | content = vim_action_get(mydb, tenant_id, datacenter, item, name) |
| 5395 | logger.debug("vim_action_delete vim response: " + str(content)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5396 | items = content.values()[0] |
| 5397 | if type(items)==list and len(items)==0: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5398 | raise NfvoException("Not found " + item, httperrors.Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5399 | elif type(items)==list and len(items)>1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5400 | 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] | 5401 | else: # it is a dict |
| 5402 | item_id = items["id"] |
| 5403 | item_name = str(items.get("name")) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5404 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5405 | try: |
| 5406 | if item=="networks": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5407 | # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it |
| 5408 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id) |
| 5409 | if sdn_network_id != None: |
| 5410 | #Delete any port attachment to this network |
| 5411 | try: |
| 5412 | port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id}) |
| 5413 | except ovimException as e: |
| 5414 | raise NfvoException( |
| 5415 | "ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5416 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5417 | |
| 5418 | # By calling one by one all ports to be detached we ensure that not only the external_ports get detached |
| 5419 | for port in port_list: |
| 5420 | vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid']) |
| 5421 | |
| 5422 | #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id |
| 5423 | try: |
| 5424 | mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id}) |
| 5425 | except db_base_Exception as e: |
| 5426 | raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) + |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5427 | str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5428 | |
| 5429 | #Delete the SDN network |
| 5430 | try: |
| 5431 | ovim.delete_network(sdn_network_id) |
| 5432 | except ovimException as e: |
| 5433 | logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True) |
| 5434 | raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5435 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5436 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5437 | content = myvim.delete_network(item_id) |
| 5438 | elif item=="tenants": |
| 5439 | content = myvim.delete_tenant(item_id) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5440 | elif item == "images": |
| 5441 | content = myvim.delete_image(item_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5442 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5443 | raise NfvoException(item + "?", httperrors.Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5444 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5445 | #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e))) |
| 5446 | 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] | 5447 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5448 | return "{} {} {} deleted".format(item[:-1], item_id,item_name) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5449 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5450 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5451 | def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): |
| 5452 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5453 | logger.debug("vim_action_create descriptor %s", str(descriptor)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5454 | if tenant_id == "any": |
| 5455 | tenant_id=None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5456 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5457 | try: |
| 5458 | if item=="networks": |
| 5459 | net = descriptor["network"] |
| 5460 | net_name = net.pop("name") |
| 5461 | net_type = net.pop("type", "bridge") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 5462 | net_public = net.pop("shared", False) |
| 5463 | net_ipprofile = net.pop("ip_profile", None) |
| tierno | a7d34d0 | 2017-02-23 14:42:07 +0100 | [diff] [blame] | 5464 | net_vlan = net.pop("vlan", None) |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 5465 | 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] | 5466 | |
| 5467 | #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network |
| 5468 | 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] | 5469 | #obtain datacenter_tenant_id |
| 5470 | datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',), |
| 5471 | FROM='datacenter_tenants', |
| 5472 | WHERE={'datacenter_id': datacenter})[0]['uuid'] |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5473 | try: |
| 5474 | sdn_network = {} |
| 5475 | sdn_network['vlan'] = net_vlan |
| 5476 | sdn_network['type'] = net_type |
| 5477 | sdn_network['name'] = net_name |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5478 | sdn_network['region'] = datacenter_tenant_id |
| garciadeblas | ebd6672 | 2019-01-31 16:01:31 +0000 | [diff] [blame] | 5479 | ovim_content = ovim.new_network(sdn_network) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5480 | except ovimException as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5481 | logger.error("ovimException creating SDN network={} ".format( |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5482 | sdn_network) + str(e), exc_info=True) |
| 5483 | raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e), |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5484 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5485 | |
| 5486 | # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id |
| 5487 | # use instance_scenario_id=None to distinguish from real instaces of nets |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5488 | correspondence = {'instance_scenario_id': None, |
| 5489 | 'sdn_net_id': ovim_content, |
| 5490 | 'vim_net_id': content, |
| 5491 | 'datacenter_tenant_id': datacenter_tenant_id |
| 5492 | } |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5493 | try: |
| 5494 | mydb.new_row('instance_nets', correspondence, add_uuid=True) |
| 5495 | except db_base_Exception as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5496 | raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5497 | correspondence, e), e.http_code) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5498 | elif item=="tenants": |
| 5499 | tenant = descriptor["tenant"] |
| 5500 | content = myvim.new_tenant(tenant["name"], tenant.get("description")) |
| 5501 | else: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5502 | raise NfvoException(item + "?", httperrors.Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5503 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5504 | 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] | 5505 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5506 | return vim_action_get(mydb, tenant_id, datacenter, item, content) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5507 | |
| 5508 | def sdn_controller_create(mydb, tenant_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5509 | data = ovim.new_of_controller(sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5510 | logger.debug('New SDN controller created with uuid {}'.format(data)) |
| 5511 | return data |
| 5512 | |
| 5513 | def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5514 | data = ovim.edit_of_controller(controller_id, sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5515 | msg = 'SDN controller {} updated'.format(data) |
| 5516 | logger.debug(msg) |
| 5517 | return msg |
| 5518 | |
| 5519 | def sdn_controller_list(mydb, tenant_id, controller_id=None): |
| 5520 | if controller_id == None: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5521 | data = ovim.get_of_controllers() |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5522 | else: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5523 | data = ovim.show_of_controller(controller_id) |
| 5524 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5525 | msg = 'SDN controller list:\n {}'.format(data) |
| 5526 | logger.debug(msg) |
| 5527 | return data |
| 5528 | |
| 5529 | def sdn_controller_delete(mydb, tenant_id, controller_id): |
| 5530 | select_ = ('uuid', 'config') |
| 5531 | datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_) |
| 5532 | for datacenter in datacenters: |
| 5533 | if datacenter['config']: |
| 5534 | config = yaml.load(datacenter['config']) |
| 5535 | if 'sdn-controller' in config and config['sdn-controller'] == controller_id: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5536 | 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] | 5537 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5538 | data = ovim.delete_of_controller(controller_id) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5539 | msg = 'SDN controller {} deleted'.format(data) |
| 5540 | logger.debug(msg) |
| 5541 | return msg |
| 5542 | |
| 5543 | def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping): |
| 5544 | controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id}) |
| 5545 | if len(controller) < 1: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5546 | 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] | 5547 | |
| 5548 | try: |
| 5549 | sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"] |
| 5550 | except: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5551 | 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] | 5552 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5553 | sdn_controller = ovim.show_of_controller(sdn_controller_id) |
| 5554 | switch_dpid = sdn_controller["dpid"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5555 | |
| 5556 | maps = list() |
| 5557 | for compute_node in sdn_port_mapping: |
| 5558 | #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid} |
| 5559 | element = dict() |
| 5560 | element["compute_node"] = compute_node["compute_node"] |
| 5561 | for port in compute_node["ports"]: |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5562 | pci = port.get("pci") |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5563 | element["switch_port"] = port.get("switch_port") |
| 5564 | element["switch_mac"] = port.get("switch_mac") |
| tierno | 4070e44 | 2019-01-23 10:19:23 +0000 | [diff] [blame] | 5565 | if not element["switch_port"] and not element["switch_mac"]: |
| 5566 | 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] | 5567 | for pci_expanded in utils.expand_brackets(pci): |
| 5568 | element["pci"] = pci_expanded |
| 5569 | maps.append(dict(element)) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5570 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5571 | 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] | 5572 | |
| 5573 | def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5574 | maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id}) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5575 | |
| 5576 | result = { |
| 5577 | "sdn-controller": None, |
| 5578 | "datacenter-id": datacenter_id, |
| 5579 | "dpid": None, |
| 5580 | "ports_mapping": list() |
| 5581 | } |
| 5582 | |
| 5583 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id) |
| 5584 | if datacenter['config']: |
| 5585 | config = yaml.load(datacenter['config']) |
| 5586 | if 'sdn-controller' in config: |
| 5587 | controller_id = config['sdn-controller'] |
| 5588 | sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id) |
| 5589 | result["sdn-controller"] = controller_id |
| 5590 | result["dpid"] = sdn_controller["dpid"] |
| 5591 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5592 | if result["sdn-controller"] == None: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5593 | 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] | 5594 | if result["dpid"] == None: |
| 5595 | 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] | 5596 | httperrors.Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5597 | |
| 5598 | if len(maps) == 0: |
| 5599 | return result |
| 5600 | |
| 5601 | ports_correspondence_dict = dict() |
| 5602 | for link in maps: |
| 5603 | if result["sdn-controller"] != link["ofc_id"]: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5604 | 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] | 5605 | if result["dpid"] != link["switch_dpid"]: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5606 | 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] | 5607 | element = dict() |
| 5608 | element["pci"] = link["pci"] |
| 5609 | if link["switch_port"]: |
| 5610 | element["switch_port"] = link["switch_port"] |
| 5611 | if link["switch_mac"]: |
| 5612 | element["switch_mac"] = link["switch_mac"] |
| 5613 | |
| 5614 | if not link["compute_node"] in ports_correspondence_dict: |
| 5615 | content = dict() |
| 5616 | content["compute_node"] = link["compute_node"] |
| 5617 | content["ports"] = list() |
| 5618 | ports_correspondence_dict[link["compute_node"]] = content |
| 5619 | |
| 5620 | ports_correspondence_dict[link["compute_node"]]["ports"].append(element) |
| 5621 | |
| 5622 | for key in sorted(ports_correspondence_dict): |
| 5623 | result["ports_mapping"].append(ports_correspondence_dict[key]) |
| 5624 | |
| 5625 | return result |
| 5626 | |
| 5627 | def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id): |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 5628 | return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id}) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5629 | |
| 5630 | def create_RO_keypair(tenant_id): |
| 5631 | """ |
| 5632 | Creates a public / private keys for a RO tenant and returns their values |
| 5633 | Params: |
| 5634 | tenant_id: ID of the tenant |
| 5635 | Return: |
| 5636 | public_key: Public key for the RO tenant |
| 5637 | private_key: Encrypted private key for RO tenant |
| 5638 | """ |
| 5639 | |
| 5640 | bits = 2048 |
| 5641 | key = RSA.generate(bits) |
| 5642 | try: |
| 5643 | public_key = key.publickey().exportKey('OpenSSH') |
| 5644 | if isinstance(public_key, ValueError): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5645 | 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] | 5646 | private_key = key.exportKey(passphrase=tenant_id, pkcs=8) |
| 5647 | except (ValueError, NameError) as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5648 | raise NfvoException("Unable to create private key: {}".format(e), httperrors.Internal_Server_Error) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5649 | return public_key, private_key |
| 5650 | |
| 5651 | def decrypt_key (key, tenant_id): |
| 5652 | """ |
| 5653 | Decrypts an encrypted RSA key |
| 5654 | Params: |
| 5655 | key: Private key to be decrypted |
| 5656 | tenant_id: ID of the tenant |
| 5657 | Return: |
| 5658 | unencrypted_key: Unencrypted private key for RO tenant |
| 5659 | """ |
| 5660 | try: |
| 5661 | key = RSA.importKey(key,tenant_id) |
| 5662 | unencrypted_key = key.exportKey('PEM') |
| 5663 | if isinstance(unencrypted_key, ValueError): |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5664 | 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] | 5665 | except ValueError as e: |
| Anderson Bravalheri | 0446cd5 | 2018-08-17 15:26:19 +0100 | [diff] [blame] | 5666 | 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] | 5667 | return unencrypted_key |