| 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 |
| 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 | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 36 | from db_base import HTTP_Unauthorized, HTTP_Bad_Request, HTTP_Internal_Server_Error, HTTP_Not_Found,\ |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 37 | HTTP_Conflict, HTTP_Method_Not_Allowed |
| 38 | import console_proxy_thread as cli |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 39 | import vimconn |
| 40 | import logging |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 41 | import collections |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 42 | import math |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 43 | from uuid import uuid4 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 44 | from db_base import db_base_Exception |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 45 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 46 | import nfvo_db |
| 47 | from threading import Lock |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 48 | import time as t |
| tierno | 01b3e17 | 2017-04-21 10:52:34 +0200 | [diff] [blame] | 49 | from lib_osm_openvim import ovim as ovim_module |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 50 | from lib_osm_openvim.ovim import ovimException |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 51 | from Crypto.PublicKey import RSA |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 52 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 53 | import osm_im.vnfd as vnfd_catalog |
| 54 | import osm_im.nsd as nsd_catalog |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 55 | from pyangbind.lib.serialise import pybindJSONDecoder |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 56 | from copy import deepcopy |
| 57 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 58 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 59 | global global_config |
| 60 | global vimconn_imported |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 61 | global logger |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 62 | global default_volume_size |
| 63 | default_volume_size = '5' #size in GB |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 64 | global ovim |
| 65 | ovim = None |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 66 | global_config = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 67 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 68 | vimconn_imported = {} # dictionary with VIM type as key, loaded module as value |
| 69 | vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 70 | vim_persistent_info = {} |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 71 | logger = logging.getLogger('openmano.nfvo') |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 72 | task_lock = Lock() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 73 | last_task_id = 0.0 |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 74 | db = None |
| 75 | db_lock = Lock() |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 76 | |
| 77 | class NfvoException(Exception): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 78 | def __init__(self, message, http_code): |
| 79 | self.http_code = http_code |
| 80 | Exception.__init__(self, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 81 | |
| 82 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 83 | def get_task_id(): |
| 84 | global last_task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 85 | task_id = t.time() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 86 | if task_id <= last_task_id: |
| 87 | task_id = last_task_id + 0.000001 |
| 88 | last_task_id = task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 89 | return "ACTION-{:.6f}".format(task_id) |
| 90 | # 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] | 91 | |
| 92 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 93 | def new_task(name, params, depends=None): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 94 | """Deprected!!!""" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 95 | task_id = get_task_id() |
| 96 | task = {"status": "enqueued", "id": task_id, "name": name, "params": params} |
| 97 | if depends: |
| 98 | task["depends"] = depends |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 99 | return task |
| 100 | |
| 101 | |
| 102 | def is_task_id(id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 103 | return True if id[:5] == "TASK-" else False |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 104 | |
| 105 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 106 | def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id): |
| 107 | name = datacenter_name[:16] |
| 108 | if name not in vim_threads["names"]: |
| 109 | vim_threads["names"].append(name) |
| 110 | return name |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 111 | name = datacenter_name[:16] + "." + tenant_name[:16] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 112 | if name not in vim_threads["names"]: |
| 113 | vim_threads["names"].append(name) |
| 114 | return name |
| 115 | name = datacenter_id + "-" + tenant_id |
| 116 | vim_threads["names"].append(name) |
| 117 | return name |
| 118 | |
| 119 | |
| 120 | def start_service(mydb): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 121 | global db, global_config |
| 122 | db = nfvo_db.nfvo_db() |
| 123 | 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] | 124 | global ovim |
| 125 | |
| 126 | # Initialize openvim for SDN control |
| 127 | # TODO: Avoid static configuration by adding new parameters to openmanod.cfg |
| 128 | # TODO: review ovim.py to delete not needed configuration |
| 129 | ovim_configuration = { |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 130 | 'logger_name': 'openmano.ovim', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 131 | 'network_vlan_range_start': 1000, |
| 132 | 'network_vlan_range_end': 4096, |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 133 | 'db_name': global_config["db_ovim_name"], |
| 134 | 'db_host': global_config["db_ovim_host"], |
| 135 | 'db_user': global_config["db_ovim_user"], |
| 136 | 'db_passwd': global_config["db_ovim_passwd"], |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 137 | 'bridge_ifaces': {}, |
| 138 | 'mode': 'normal', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 139 | 'network_type': 'bridge', |
| 140 | #TODO: log_level_of should not be needed. To be modified in ovim |
| 141 | 'log_level_of': 'DEBUG' |
| 142 | } |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 143 | try: |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 144 | # starts ovim library |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 145 | ovim = ovim_module.ovim(ovim_configuration) |
| 146 | ovim.start_service() |
| 147 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 148 | #delete old unneeded vim_actions |
| 149 | clean_db(mydb) |
| 150 | |
| 151 | # starts vim_threads |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 152 | from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\ |
| 153 | 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid' |
| 154 | select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin', |
| 155 | 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id', |
| 156 | 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id', |
| 157 | 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id') |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 158 | vims = mydb.get_rows(FROM=from_, SELECT=select_) |
| 159 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 160 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 161 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 162 | if vim["config"]: |
| 163 | extra.update(yaml.load(vim["config"])) |
| 164 | if vim.get('dt_config'): |
| 165 | extra.update(yaml.load(vim["dt_config"])) |
| 166 | if vim["type"] not in vimconn_imported: |
| 167 | module_info=None |
| 168 | try: |
| 169 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 170 | pkg = __import__("osm_ro." + module) |
| 171 | vim_conn = getattr(pkg, module) |
| 172 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 173 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 174 | vimconn_imported[vim["type"]] = vim_conn |
| 175 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 176 | # if module_info and module_info[0]: |
| 177 | # file.close(module_info[0]) |
| tierno | cdee8cc | 2017-04-25 13:42:06 +0200 | [diff] [blame] | 178 | raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 179 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 180 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 181 | thread_id = vim['datacenter_tenant_id'] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 182 | vim_persistent_info[thread_id] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 183 | try: |
| 184 | #if not tenant: |
| 185 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 186 | myvim = vimconn_imported[ vim["type"] ].vimconnector( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 187 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| 188 | tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'], |
| 189 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| 190 | user=vim['user'], passwd=vim['passwd'], |
| 191 | config=extra, persistent_info=vim_persistent_info[thread_id] |
| 192 | ) |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 193 | except vimconn.vimconnException as e: |
| 194 | myvim = e |
| 195 | logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'], |
| 196 | vim['datacenter_id'], e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 197 | except Exception as e: |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 198 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e), |
| 199 | HTTP_Internal_Server_Error) |
| 200 | thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], |
| 201 | vim['vim_tenant_id']) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 202 | new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'], |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 203 | vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 204 | new_thread.start() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 205 | vim_threads["running"][thread_id] = new_thread |
| 206 | except db_base_Exception as e: |
| 207 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 208 | except ovim_module.ovimException as e: |
| 209 | message = str(e) |
| 210 | if message[:22] == "DATABASE wrong version": |
| 211 | message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\ |
| 212 | "at host {dbhost}".format( |
| 213 | msg=message[22:-3], dbname=global_config["db_ovim_name"], |
| 214 | dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"], |
| 215 | ver=message[-3:-1], dbhost=global_config["db_ovim_host"]) |
| 216 | raise NfvoException(message, HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 217 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 218 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 219 | def stop_service(): |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 220 | global ovim, global_config |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 221 | if ovim: |
| 222 | ovim.stop_service() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 223 | for thread_id,thread in vim_threads["running"].items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 224 | thread.insert_task("exit") |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 225 | vim_threads["deleting"][thread_id] = thread |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 226 | vim_threads["running"] = {} |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 227 | if global_config and global_config.get("console_thread"): |
| 228 | for thread in global_config["console_thread"]: |
| 229 | thread.terminate = True |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 230 | |
| tierno | 6ddeded | 2017-05-16 15:40:26 +0200 | [diff] [blame] | 231 | def get_version(): |
| 232 | return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"], |
| 233 | global_config["version_date"] )) |
| 234 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 235 | def clean_db(mydb): |
| 236 | """ |
| 237 | Clean unused or old entries at database to avoid unlimited growing |
| 238 | :param mydb: database connector |
| 239 | :return: None |
| 240 | """ |
| 241 | # get and delete unused vim_actions: all elements deleted, one week before, instance not present |
| 242 | now = t.time()-3600*24*7 |
| 243 | instance_action_id = None |
| 244 | nb_deleted = 0 |
| 245 | while True: |
| 246 | actions_to_delete = mydb.get_rows( |
| 247 | SELECT=("item", "item_id", "instance_action_id"), |
| 248 | FROM="vim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid " |
| 249 | "left join instance_scenarios as i on ia.instance_id=i.uuid", |
| 250 | WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None, |
| 251 | "va.status": ("DONE", "SUPERSEDED")}, |
| 252 | LIMIT=100 |
| 253 | ) |
| 254 | for to_delete in actions_to_delete: |
| 255 | mydb.delete_row(FROM="vim_actions", WHERE=to_delete) |
| 256 | if instance_action_id != to_delete["instance_action_id"]: |
| 257 | instance_action_id = to_delete["instance_action_id"] |
| 258 | mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id}) |
| 259 | nb_deleted += len(actions_to_delete) |
| 260 | if len(actions_to_delete) < 100: |
| 261 | break |
| 262 | if nb_deleted: |
| 263 | logger.debug("Removed {} unused vim_actions".format(nb_deleted)) |
| 264 | |
| 265 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 266 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 267 | def get_flavorlist(mydb, vnf_id, nfvo_tenant=None): |
| 268 | '''Obtain flavorList |
| 269 | return result, content: |
| 270 | <0, error_text upon error |
| 271 | nb_records, flavor_list on success |
| 272 | ''' |
| 273 | WHERE_dict={} |
| 274 | WHERE_dict['vnf_id'] = vnf_id |
| 275 | if nfvo_tenant is not None: |
| 276 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 277 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 278 | #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict ) |
| 279 | #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] | 280 | flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict ) |
| 281 | #print "get_flavor_list result:", result |
| 282 | #print "get_flavor_list content:", content |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 283 | flavorList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 284 | for flavor in flavors: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 285 | flavorList.append(flavor['flavor_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 286 | return flavorList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 287 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 288 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 289 | def get_imagelist(mydb, vnf_id, nfvo_tenant=None): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 290 | """ |
| 291 | Get used images of all vms belonging to this VNFD |
| 292 | :param mydb: database conector |
| 293 | :param vnf_id: vnfd uuid |
| 294 | :param nfvo_tenant: tenant, not used |
| 295 | :return: The list of image uuid used |
| 296 | """ |
| 297 | image_list = [] |
| 298 | vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id}) |
| 299 | for vm in vms: |
| 300 | if vm["image_id"] not in image_list: |
| 301 | image_list.append(vm["image_id"]) |
| 302 | if vm["image_list"]: |
| 303 | vm_image_list = yaml.load(vm["image_list"]) |
| 304 | for image_dict in vm_image_list: |
| 305 | if image_dict["image_id"] not in image_list: |
| 306 | image_list.append(image_dict["image_id"]) |
| 307 | return image_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 308 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 309 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 310 | 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] | 311 | 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] | 312 | '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 313 | return dictionary with {datacenter_id: vim_class, ... }. vim_class contain: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 314 | '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] | 315 | raise exception upon error |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 316 | ''' |
| 317 | WHERE_dict={} |
| 318 | if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| 319 | if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 320 | 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] | 321 | if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name |
| 322 | 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] | 323 | if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name |
| 324 | 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] | 325 | 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] | 326 | 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] | 327 | '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] | 328 | 'user','passwd', 'dt.config as dt_config') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 329 | else: |
| 330 | from_ = 'datacenters as d' |
| 331 | 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] | 332 | try: |
| 333 | vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict ) |
| 334 | vim_dict={} |
| 335 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 336 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 337 | 'datacenter_id': vim.get('datacenter_id'), |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 338 | '_vim_type_internal': vim.get('type')} |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 339 | if vim["config"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 340 | extra.update(yaml.load(vim["config"])) |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 341 | if vim.get('dt_config'): |
| 342 | extra.update(yaml.load(vim["dt_config"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 343 | if vim["type"] not in vimconn_imported: |
| 344 | module_info=None |
| 345 | try: |
| 346 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 347 | pkg = __import__("osm_ro." + module) |
| 348 | vim_conn = getattr(pkg, module) |
| 349 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 350 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 351 | vimconn_imported[vim["type"]] = vim_conn |
| 352 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 353 | # if module_info and module_info[0]: |
| 354 | # file.close(module_info[0]) |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 355 | if ignore_errors: |
| 356 | logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 357 | vim["type"], module, type(e).__name__, str(e))) |
| 358 | continue |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 359 | raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 360 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 361 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 362 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 363 | if 'datacenter_tenant_id' in vim: |
| 364 | thread_id = vim["datacenter_tenant_id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 365 | if thread_id not in vim_persistent_info: |
| 366 | vim_persistent_info[thread_id] = {} |
| 367 | persistent_info = vim_persistent_info[thread_id] |
| 368 | else: |
| 369 | persistent_info = {} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 370 | #if not tenant: |
| 371 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 372 | vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector( |
| 373 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 374 | tenant_id=vim.get('vim_tenant_id',vim_tenant), |
| 375 | tenant_name=vim.get('vim_tenant_name',vim_tenant_name), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 376 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 377 | user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd), |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 378 | config=extra, persistent_info=persistent_info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 379 | ) |
| 380 | except Exception as e: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 381 | if ignore_errors: |
| 382 | logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e))) |
| 383 | continue |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 384 | http_code = HTTP_Internal_Server_Error |
| 385 | if isinstance(e, vimconn.vimconnException): |
| 386 | http_code = e.http_code |
| 387 | 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] | 388 | return vim_dict |
| 389 | except db_base_Exception as e: |
| 390 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 391 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 392 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 393 | def rollback(mydb, vims, rollback_list): |
| 394 | undeleted_items=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 395 | #delete things by reverse order |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 396 | for i in range(len(rollback_list)-1, -1, -1): |
| 397 | item = rollback_list[i] |
| 398 | if item["where"]=="vim": |
| 399 | if item["vim_id"] not in vims: |
| 400 | continue |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 401 | if is_task_id(item["uuid"]): |
| 402 | continue |
| 403 | vim = vims[item["vim_id"]] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 404 | try: |
| 405 | if item["what"]=="image": |
| 406 | vim.delete_image(item["uuid"]) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 407 | 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] | 408 | elif item["what"]=="flavor": |
| 409 | vim.delete_flavor(item["uuid"]) |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 410 | 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] | 411 | elif item["what"]=="network": |
| 412 | vim.delete_network(item["uuid"]) |
| 413 | elif item["what"]=="vm": |
| 414 | vim.delete_vminstance(item["uuid"]) |
| 415 | except vimconn.vimconnException as e: |
| 416 | logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e)) |
| 417 | undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 418 | except db_base_Exception as e: |
| 419 | 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] | 420 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 421 | else: # where==mano |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 422 | try: |
| 423 | if item["what"]=="image": |
| 424 | mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]}) |
| 425 | elif item["what"]=="flavor": |
| 426 | mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]}) |
| 427 | except db_base_Exception as e: |
| 428 | logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e)) |
| 429 | undeleted_items.append("{} '{}'".format(item['what'], item["uuid"])) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 430 | if len(undeleted_items)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 431 | return True," Rollback successful." |
| 432 | else: |
| 433 | return False," Rollback fails to delete: " + str(undeleted_items) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 434 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 435 | |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 436 | def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 437 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 438 | #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 439 | vnfc_interfaces={} |
| 440 | for vnfc in vnf_descriptor["vnf"]["VNFC"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 441 | name_dict = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 442 | #dataplane interfaces |
| 443 | for numa in vnfc.get("numas",() ): |
| 444 | for interface in numa.get("interfaces",()): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 445 | if interface["name"] in name_dict: |
| 446 | raise NfvoException( |
| 447 | "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format( |
| 448 | vnfc["name"], interface["name"]), |
| 449 | HTTP_Bad_Request) |
| 450 | name_dict[ interface["name"] ] = "underlay" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 451 | #bridge interfaces |
| 452 | for interface in vnfc.get("bridge-ifaces",() ): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 453 | if interface["name"] in name_dict: |
| 454 | raise NfvoException( |
| 455 | "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format( |
| 456 | vnfc["name"], interface["name"]), |
| 457 | HTTP_Bad_Request) |
| 458 | name_dict[ interface["name"] ] = "overlay" |
| 459 | vnfc_interfaces[ vnfc["name"] ] = name_dict |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 460 | # check bood-data info |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 461 | # if "boot-data" in vnfc: |
| 462 | # # check that user-data is incompatible with users and config-files |
| 463 | # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"): |
| 464 | # raise NfvoException( |
| 465 | # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'", |
| 466 | # HTTP_Bad_Request) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 467 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 468 | #check if the info in external_connections matches with the one in the vnfcs |
| 469 | name_list=[] |
| 470 | for external_connection in vnf_descriptor["vnf"].get("external-connections",() ): |
| 471 | if external_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 472 | raise NfvoException( |
| 473 | "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format( |
| 474 | external_connection["name"]), |
| 475 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 476 | name_list.append(external_connection["name"]) |
| 477 | if external_connection["VNFC"] not in vnfc_interfaces: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 478 | raise NfvoException( |
| 479 | "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format( |
| 480 | external_connection["name"], external_connection["VNFC"]), |
| 481 | HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 482 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 483 | if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 484 | raise NfvoException( |
| 485 | "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 486 | external_connection["name"], |
| 487 | external_connection["local_iface_name"]), |
| 488 | HTTP_Bad_Request ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 489 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 490 | #check if the info in internal_connections matches with the one in the vnfcs |
| 491 | name_list=[] |
| 492 | for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ): |
| 493 | if internal_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 494 | raise NfvoException( |
| 495 | "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format( |
| 496 | internal_connection["name"]), |
| 497 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 498 | name_list.append(internal_connection["name"]) |
| 499 | #We should check that internal-connections of type "ptp" have only 2 elements |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 500 | |
| 501 | if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"): |
| 502 | raise NfvoException( |
| 503 | "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format( |
| 504 | internal_connection["name"], |
| 505 | 'ptp' if vnf_descriptor_version==1 else 'e-line', |
| 506 | 'data' if vnf_descriptor_version==1 else "e-lan"), |
| 507 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 508 | for port in internal_connection["elements"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 509 | vnf = port["VNFC"] |
| 510 | iface = port["local_iface_name"] |
| 511 | if vnf not in vnfc_interfaces: |
| 512 | raise NfvoException( |
| 513 | "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format( |
| 514 | internal_connection["name"], vnf), |
| 515 | HTTP_Bad_Request) |
| 516 | if iface not in vnfc_interfaces[ vnf ]: |
| 517 | raise NfvoException( |
| 518 | "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 519 | internal_connection["name"], iface), |
| 520 | HTTP_Bad_Request) |
| 521 | return -HTTP_Bad_Request, |
| 522 | if vnf_descriptor_version==1 and "type" not in internal_connection: |
| 523 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 524 | internal_connection["type"] = "bridge" |
| 525 | else: |
| 526 | internal_connection["type"] = "data" |
| 527 | if vnf_descriptor_version==2 and "implementation" not in internal_connection: |
| 528 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 529 | internal_connection["implementation"] = "overlay" |
| 530 | else: |
| 531 | internal_connection["implementation"] = "underlay" |
| 532 | if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \ |
| 533 | internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay": |
| 534 | raise NfvoException( |
| 535 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 536 | internal_connection["name"], |
| 537 | iface, 'bridge' if vnf_descriptor_version==1 else 'overlay', |
| 538 | 'data' if vnf_descriptor_version==1 else 'underlay'), |
| 539 | HTTP_Bad_Request) |
| 540 | if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \ |
| 541 | vnfc_interfaces[vnf][iface] == "underlay": |
| 542 | raise NfvoException( |
| 543 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 544 | internal_connection["name"], iface, |
| 545 | 'data' if vnf_descriptor_version==1 else 'underlay', |
| 546 | 'bridge' if vnf_descriptor_version==1 else 'overlay'), |
| 547 | HTTP_Bad_Request) |
| 548 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 549 | |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 550 | 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] | 551 | #look if image exist |
| 552 | if only_create_at_vim: |
| 553 | image_mano_id = image_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 554 | if return_on_error == None: |
| 555 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 556 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 557 | if image_dict['location']: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 558 | images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']}) |
| 559 | else: |
| 560 | 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] | 561 | if len(images)>=1: |
| 562 | image_mano_id = images[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 563 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 564 | #create image in MANO DB |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 565 | temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None), |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 566 | 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None), |
| 567 | 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 568 | } |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 569 | #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] | 570 | image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True) |
| 571 | rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 572 | #create image at every vim |
| 573 | for vim_id,vim in vims.iteritems(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 574 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 575 | image_created="false" |
| 576 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 577 | image_db = mydb.get_rows(FROM="datacenters_images", |
| 578 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 579 | #look at VIM if this image exist |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 580 | try: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 581 | if image_dict['location'] is not None: |
| 582 | image_vim_id = vim.get_image_id_from_path(image_dict['location']) |
| 583 | else: |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 584 | filter_dict = {} |
| 585 | filter_dict['name'] = image_dict['universal_name'] |
| 586 | if image_dict.get('checksum') != None: |
| 587 | filter_dict['checksum'] = image_dict['checksum'] |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 588 | #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 589 | vim_images = vim.get_image_list(filter_dict) |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 590 | #logger.debug('>>>>>>>> VIM images: %s', str(vim_images)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 591 | if len(vim_images) > 1: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 592 | raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict) |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 593 | elif len(vim_images) == 0: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 594 | 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] | 595 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 596 | #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0])) |
| 597 | image_vim_id = vim_images[0]['id'] |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 598 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 599 | except vimconn.vimconnNotFoundException as e: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 600 | #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] | 601 | try: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 602 | #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None |
| 603 | if image_dict['location']: |
| 604 | image_vim_id = vim.new_image(image_dict) |
| 605 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id}) |
| 606 | image_created="true" |
| 607 | else: |
| garciadeblas | b6153a2 | 2017-02-06 15:38:33 +0100 | [diff] [blame] | 608 | #If we reach this point, then the image has image name, and optionally checksum, and could not be found |
| 609 | raise vimconn.vimconnException(str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 610 | except vimconn.vimconnException as e: |
| 611 | if return_on_error: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 612 | logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 613 | raise |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 614 | image_vim_id = None |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 615 | logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 616 | continue |
| 617 | except vimconn.vimconnException as e: |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 618 | if return_on_error: |
| 619 | logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e)) |
| 620 | raise |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 621 | 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] | 622 | image_vim_id = None |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 623 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 624 | #if we reach here, the image has been created or existed |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 625 | if len(image_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 626 | #add new vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 627 | mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id, |
| 628 | 'image_id':image_mano_id, |
| 629 | 'vim_id': image_vim_id, |
| 630 | 'created':image_created}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 631 | elif image_db[0]["vim_id"]!=image_vim_id: |
| 632 | #modify existing vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 633 | 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] | 634 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 635 | return image_vim_id if only_create_at_vim else image_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 636 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 637 | |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 638 | 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] | 639 | temp_flavor_dict= {'disk':flavor_dict.get('disk',0), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 640 | 'ram':flavor_dict.get('ram'), |
| 641 | 'vcpus':flavor_dict.get('vcpus'), |
| 642 | } |
| 643 | if 'extended' in flavor_dict and flavor_dict['extended']==None: |
| 644 | del flavor_dict['extended'] |
| 645 | if 'extended' in flavor_dict: |
| 646 | temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256) |
| 647 | |
| 648 | #look if flavor exist |
| 649 | if only_create_at_vim: |
| 650 | flavor_mano_id = flavor_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 651 | if return_on_error == None: |
| 652 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 653 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 654 | flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 655 | if len(flavors)>=1: |
| 656 | flavor_mano_id = flavors[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 657 | else: |
| 658 | #create flavor |
| 659 | #create one by one the images of aditional disks |
| 660 | dev_image_list=[] #list of images |
| 661 | if 'extended' in flavor_dict and flavor_dict['extended']!=None: |
| 662 | dev_nb=0 |
| 663 | for device in flavor_dict['extended'].get('devices',[]): |
| garciadeblas | 41f18be | 2016-10-04 09:09:58 +0200 | [diff] [blame] | 664 | if "image" not in device and "image name" not in device: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 665 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 666 | image_dict={} |
| 667 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 668 | image_dict['universal_name']=device.get('image name') |
| 669 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 670 | image_dict['location']=device.get('image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 671 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 672 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 673 | image_metadata_dict = device.get('image metadata', None) |
| 674 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 675 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 676 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 677 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 678 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 679 | #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] | 680 | dev_image_list.append(image_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 681 | dev_nb += 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 682 | temp_flavor_dict['name'] = flavor_dict['name'] |
| 683 | temp_flavor_dict['description'] = flavor_dict.get('description',None) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 684 | content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True) |
| 685 | flavor_mano_id= content |
| 686 | rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 687 | #create flavor at every vim |
| 688 | if 'uuid' in flavor_dict: |
| 689 | del flavor_dict['uuid'] |
| 690 | flavor_vim_id=None |
| 691 | for vim_id,vim in vims.items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 692 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 693 | flavor_created="false" |
| 694 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 695 | flavor_db = mydb.get_rows(FROM="datacenters_flavors", |
| 696 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 697 | #look at VIM if this flavor exist SKIPPED |
| 698 | #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location']) |
| 699 | #if res_vim < 0: |
| 700 | # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id |
| 701 | # continue |
| 702 | #elif res_vim==0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 703 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 704 | # Create the flavor in VIM |
| 705 | # Translate images at devices from MANO id to VIM id |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 706 | disk_list = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 707 | 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] | 708 | # make a copy of original devices |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 709 | devices_original=[] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 710 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 711 | for device in flavor_dict["extended"].get("devices",[]): |
| 712 | dev={} |
| 713 | dev.update(device) |
| 714 | devices_original.append(dev) |
| 715 | if 'image' in device: |
| 716 | del device['image'] |
| 717 | if 'image metadata' in device: |
| 718 | del device['image metadata'] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 719 | if 'image checksum' in device: |
| 720 | del device['image checksum'] |
| 721 | dev_nb = 0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 722 | for index in range(0,len(devices_original)) : |
| 723 | device=devices_original[index] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 724 | if "image" not in device and "image name" not in device: |
| tierno | ecc6839 | 2018-09-06 13:47:11 +0200 | [diff] [blame] | 725 | # if 'size' in device: |
| 726 | 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] | 727 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 728 | image_dict={} |
| 729 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 730 | image_dict['universal_name']=device.get('image name') |
| 731 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 732 | image_dict['location']=device.get('image') |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 733 | # image_dict['new_location']=device.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 734 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 735 | image_metadata_dict = device.get('image metadata', None) |
| 736 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 737 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 738 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 739 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 740 | 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] | 741 | image_dict["uuid"]=image_mano_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 742 | 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] | 743 | |
| 744 | #save disk information (image must be based on and size |
| 745 | disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)}) |
| 746 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 747 | flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id |
| 748 | dev_nb += 1 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 749 | if len(flavor_db)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 750 | #check that this vim_id exist in VIM, if not create |
| 751 | flavor_vim_id=flavor_db[0]["vim_id"] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 752 | try: |
| 753 | vim.get_flavor(flavor_vim_id) |
| 754 | continue #flavor exist |
| 755 | except vimconn.vimconnException: |
| 756 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 757 | #create flavor at vim |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 758 | logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"]) |
| 759 | try: |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 760 | flavor_vim_id = None |
| 761 | flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict) |
| 762 | flavor_create="false" |
| 763 | except vimconn.vimconnException as e: |
| 764 | pass |
| 765 | try: |
| 766 | if not flavor_vim_id: |
| 767 | flavor_vim_id = vim.new_flavor(flavor_dict) |
| 768 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id}) |
| 769 | flavor_created="true" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 770 | except vimconn.vimconnException as e: |
| 771 | if return_on_error: |
| 772 | logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 773 | raise |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 774 | logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 775 | flavor_vim_id = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 776 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 777 | #if reach here the flavor has been create or exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 778 | if len(flavor_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 779 | #add new vim_id at datacenters_flavors |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 780 | extended_devices_yaml = None |
| 781 | if len(disk_list) > 0: |
| 782 | extended_devices = dict() |
| 783 | extended_devices['disks'] = disk_list |
| 784 | extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256) |
| 785 | mydb.new_row('datacenters_flavors', |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 786 | {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id, |
| 787 | 'created': flavor_created, 'extended': extended_devices_yaml}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 788 | elif flavor_db[0]["vim_id"]!=flavor_vim_id: |
| 789 | #modify existing vim_id at datacenters_flavors |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 790 | mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, |
| 791 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 792 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 793 | return flavor_vim_id if only_create_at_vim else flavor_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 794 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 795 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 796 | def get_str(obj, field, length): |
| 797 | """ |
| 798 | Obtain the str value, |
| 799 | :param obj: |
| 800 | :param length: |
| 801 | :return: |
| 802 | """ |
| 803 | value = obj.get(field) |
| 804 | if value is not None: |
| 805 | value = str(value)[:length] |
| 806 | return value |
| 807 | |
| 808 | def _lookfor_or_create_image(db_image, mydb, descriptor): |
| 809 | """ |
| 810 | fill image content at db_image dictionary. Check if the image with this image and checksum exist |
| 811 | :param db_image: dictionary to insert data |
| 812 | :param mydb: database connector |
| 813 | :param descriptor: yang descriptor |
| 814 | :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image |
| 815 | """ |
| 816 | |
| 817 | db_image["name"] = get_str(descriptor, "image", 255) |
| 818 | db_image["checksum"] = get_str(descriptor, "image-checksum", 32) |
| 819 | if not db_image["checksum"]: # Ensure that if empty string, None is stored |
| 820 | db_image["checksum"] = None |
| 821 | if db_image["name"].startswith("/"): |
| 822 | db_image["location"] = db_image["name"] |
| 823 | existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]}) |
| 824 | else: |
| 825 | db_image["universal_name"] = db_image["name"] |
| 826 | existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'], |
| 827 | 'checksum': db_image['checksum']}) |
| 828 | if existing_images: |
| 829 | return existing_images[0]["uuid"] |
| 830 | else: |
| 831 | image_uuid = str(uuid4()) |
| 832 | db_image["uuid"] = image_uuid |
| 833 | return None |
| 834 | |
| 835 | def new_vnfd_v3(mydb, tenant_id, vnf_descriptor): |
| 836 | """ |
| 837 | Parses an OSM IM vnfd_catalog and insert at DB |
| 838 | :param mydb: |
| 839 | :param tenant_id: |
| 840 | :param vnf_descriptor: |
| 841 | :return: The list of cretated vnf ids |
| 842 | """ |
| 843 | try: |
| 844 | myvnfd = vnfd_catalog.vnfd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 845 | try: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 846 | pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True) |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 847 | except Exception as e: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 848 | raise NfvoException("Error. Invalid VNF descriptor format " + str(e), HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 849 | db_vnfs = [] |
| 850 | db_nets = [] |
| 851 | db_vms = [] |
| 852 | db_vms_index = 0 |
| 853 | db_interfaces = [] |
| 854 | db_images = [] |
| 855 | db_flavors = [] |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 856 | db_ip_profiles_index = 0 |
| 857 | db_ip_profiles = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 858 | uuid_list = [] |
| 859 | vnfd_uuid_list = [] |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 860 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog") |
| 861 | if not vnfd_catalog_descriptor: |
| 862 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog") |
| 863 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd") |
| 864 | if not vnfd_descriptor_list: |
| 865 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd") |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 866 | for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues(): |
| 867 | vnfd = vnfd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 868 | |
| 869 | # table vnf |
| 870 | vnf_uuid = str(uuid4()) |
| 871 | uuid_list.append(vnf_uuid) |
| 872 | vnfd_uuid_list.append(vnf_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 873 | vnfd_id = get_str(vnfd, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 874 | db_vnf = { |
| 875 | "uuid": vnf_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 876 | "osm_id": vnfd_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 877 | "name": get_str(vnfd, "name", 255), |
| 878 | "description": get_str(vnfd, "description", 255), |
| 879 | "tenant_id": tenant_id, |
| 880 | "vendor": get_str(vnfd, "vendor", 255), |
| 881 | "short_name": get_str(vnfd, "short-name", 255), |
| 882 | "descriptor": str(vnf_descriptor)[:60000] |
| 883 | } |
| 884 | |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 885 | for vnfd_descriptor in vnfd_descriptor_list: |
| 886 | if vnfd_descriptor["id"] == str(vnfd["id"]): |
| 887 | break |
| 888 | |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 889 | # table ip_profiles (ip-profiles) |
| 890 | ip_profile_name2db_table_index = {} |
| 891 | for ip_profile in vnfd.get("ip-profiles").itervalues(): |
| 892 | db_ip_profile = { |
| 893 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 894 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 895 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 896 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 897 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 898 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 899 | } |
| 900 | dns_list = [] |
| 901 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 902 | dns_list.append(str(dns.get("address"))) |
| 903 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 904 | if ip_profile["ip-profile-params"].get('security-group'): |
| 905 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 906 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 907 | db_ip_profiles_index += 1 |
| 908 | db_ip_profiles.append(db_ip_profile) |
| 909 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 910 | # table nets (internal-vld) |
| 911 | net_id2uuid = {} # for mapping interface with network |
| 912 | for vld in vnfd.get("internal-vld").itervalues(): |
| 913 | net_uuid = str(uuid4()) |
| 914 | uuid_list.append(net_uuid) |
| 915 | db_net = { |
| 916 | "name": get_str(vld, "name", 255), |
| 917 | "vnf_id": vnf_uuid, |
| 918 | "uuid": net_uuid, |
| 919 | "description": get_str(vld, "description", 255), |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 920 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 921 | "type": "bridge", # TODO adjust depending on connection point type |
| 922 | } |
| 923 | net_id2uuid[vld.get("id")] = net_uuid |
| 924 | db_nets.append(db_net) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 925 | # ip-profile, link db_ip_profile with db_sce_net |
| 926 | if vld.get("ip-profile-ref"): |
| 927 | ip_profile_name = vld.get("ip-profile-ref") |
| 928 | if ip_profile_name not in ip_profile_name2db_table_index: |
| 929 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':" |
| 930 | "'{}'. Reference to a non-existing 'ip_profiles'".format( |
| 931 | str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| 932 | HTTP_Bad_Request) |
| 933 | db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid |
| 934 | else: #check no ip-address has been defined |
| tierno | 45140f5 | 2018-03-26 12:11:46 +0200 | [diff] [blame] | 935 | for icp in vld.get("internal-connection-point").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 936 | if icp.get("ip-address"): |
| 937 | raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' " |
| 938 | "contains an ip-address but no ip-profile has been defined at VLD".format( |
| 939 | str(vnfd["id"]), str(vld["id"]), str(icp["id"])), |
| 940 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 941 | |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 942 | # connection points vaiable declaration |
| 943 | cp_name2iface_uuid = {} |
| 944 | cp_name2vm_uuid = {} |
| 945 | cp_name2db_interface = {} |
| 946 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 947 | # table vms (vdus) |
| 948 | vdu_id2uuid = {} |
| 949 | vdu_id2db_table_index = {} |
| 950 | for vdu in vnfd.get("vdu").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 951 | |
| 952 | for vdu_descriptor in vnfd_descriptor["vdu"]: |
| 953 | if vdu_descriptor["id"] == str(vdu["id"]): |
| 954 | break |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 955 | vm_uuid = str(uuid4()) |
| 956 | uuid_list.append(vm_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 957 | vdu_id = get_str(vdu, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 958 | db_vm = { |
| 959 | "uuid": vm_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 960 | "osm_id": vdu_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 961 | "name": get_str(vdu, "name", 255), |
| 962 | "description": get_str(vdu, "description", 255), |
| 963 | "vnf_id": vnf_uuid, |
| 964 | } |
| 965 | vdu_id2uuid[db_vm["osm_id"]] = vm_uuid |
| 966 | vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index |
| 967 | if vdu.get("count"): |
| 968 | db_vm["count"] = int(vdu["count"]) |
| 969 | |
| 970 | # table image |
| 971 | image_present = False |
| 972 | if vdu.get("image"): |
| 973 | image_present = True |
| 974 | db_image = {} |
| 975 | image_uuid = _lookfor_or_create_image(db_image, mydb, vdu) |
| 976 | if not image_uuid: |
| 977 | image_uuid = db_image["uuid"] |
| 978 | db_images.append(db_image) |
| 979 | db_vm["image_id"] = image_uuid |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 980 | if vdu.get("alternative-images"): |
| 981 | vm_alternative_images = [] |
| 982 | for alt_image in vdu.get("alternative-images").itervalues(): |
| 983 | db_image = {} |
| 984 | image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image) |
| 985 | if not image_uuid: |
| 986 | image_uuid = db_image["uuid"] |
| 987 | db_images.append(db_image) |
| 988 | vm_alternative_images.append({ |
| 989 | "image_id": image_uuid, |
| 990 | "vim_type": str(alt_image["vim-type"]), |
| 991 | # "universal_name": str(alt_image["image"]), |
| 992 | # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None |
| 993 | }) |
| 994 | |
| 995 | 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] | 996 | |
| 997 | # volumes |
| 998 | devices = [] |
| 999 | if vdu.get("volumes"): |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1000 | for volume_key in vdu["volumes"]: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1001 | volume = vdu["volumes"][volume_key] |
| 1002 | if not image_present: |
| 1003 | # Convert the first volume to vnfc.image |
| 1004 | image_present = True |
| 1005 | db_image = {} |
| 1006 | image_uuid = _lookfor_or_create_image(db_image, mydb, volume) |
| 1007 | if not image_uuid: |
| 1008 | image_uuid = db_image["uuid"] |
| 1009 | db_images.append(db_image) |
| 1010 | db_vm["image_id"] = image_uuid |
| 1011 | else: |
| 1012 | # Add Openmano devices |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1013 | device = {"name": str(volume.get("name"))} |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1014 | device["type"] = str(volume.get("device-type")) |
| 1015 | if volume.get("size"): |
| 1016 | device["size"] = int(volume["size"]) |
| 1017 | if volume.get("image"): |
| 1018 | device["image name"] = str(volume["image"]) |
| 1019 | if volume.get("image-checksum"): |
| 1020 | device["image checksum"] = str(volume["image-checksum"]) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1021 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1022 | devices.append(device) |
| 1023 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1024 | # cloud-init |
| 1025 | boot_data = {} |
| 1026 | if vdu.get("cloud-init"): |
| 1027 | boot_data["user-data"] = str(vdu["cloud-init"]) |
| 1028 | elif vdu.get("cloud-init-file"): |
| 1029 | # TODO Where this file content is present??? |
| 1030 | # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]] |
| 1031 | boot_data["user-data"] = str(vdu["cloud-init-file"]) |
| 1032 | |
| 1033 | if vdu.get("supplemental-boot-data"): |
| 1034 | if vdu["supplemental-boot-data"].get('boot-data-drive'): |
| 1035 | boot_data['boot-data-drive'] = True |
| 1036 | if vdu["supplemental-boot-data"].get('config-file'): |
| 1037 | om_cfgfile_list = list() |
| 1038 | for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues(): |
| 1039 | # TODO Where this file content is present??? |
| 1040 | cfg_source = str(custom_config_file["source"]) |
| 1041 | om_cfgfile_list.append({"dest": custom_config_file["dest"], |
| 1042 | "content": cfg_source}) |
| 1043 | boot_data['config-files'] = om_cfgfile_list |
| 1044 | if boot_data: |
| 1045 | db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256) |
| 1046 | |
| 1047 | db_vms.append(db_vm) |
| 1048 | db_vms_index += 1 |
| 1049 | |
| 1050 | # table interfaces (internal/external interfaces) |
| 1051 | flavor_epa_interfaces = [] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1052 | vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU |
| 1053 | # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()): |
| 1054 | for iface in vdu.get("interface").itervalues(): |
| 1055 | flavor_epa_interface = {} |
| 1056 | iface_uuid = str(uuid4()) |
| 1057 | uuid_list.append(iface_uuid) |
| 1058 | db_interface = { |
| 1059 | "uuid": iface_uuid, |
| 1060 | "internal_name": get_str(iface, "name", 255), |
| 1061 | "vm_id": vm_uuid, |
| 1062 | } |
| 1063 | flavor_epa_interface["name"] = db_interface["internal_name"] |
| 1064 | if iface.get("virtual-interface").get("vpci"): |
| 1065 | db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12) |
| 1066 | flavor_epa_interface["vpci"] = db_interface["vpci"] |
| 1067 | |
| 1068 | if iface.get("virtual-interface").get("bandwidth"): |
| 1069 | bps = int(iface.get("virtual-interface").get("bandwidth")) |
| 1070 | db_interface["bw"] = int(math.ceil(bps/1000000.0)) |
| 1071 | flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"]) |
| 1072 | |
| 1073 | if iface.get("virtual-interface").get("type") == "OM-MGMT": |
| 1074 | db_interface["type"] = "mgmt" |
| 1075 | elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000"): |
| 1076 | db_interface["type"] = "bridge" |
| 1077 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1078 | elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 1079 | db_interface["type"] = "data" |
| 1080 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1081 | flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \ |
| 1082 | else "yes" |
| 1083 | flavor_epa_interfaces.append(flavor_epa_interface) |
| 1084 | else: |
| 1085 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual" |
| 1086 | "-interface':'type':'{}'. Interface type is not supported".format( |
| 1087 | vnfd_id, vdu_id, iface.get("virtual-interface").get("type")), |
| 1088 | HTTP_Bad_Request) |
| 1089 | |
| tierno | e72710b | 2018-07-23 16:16:00 +0200 | [diff] [blame] | 1090 | if iface.get("mgmt-interface"): |
| 1091 | db_interface["type"] = "mgmt" |
| 1092 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1093 | if iface.get("external-connection-point-ref"): |
| 1094 | try: |
| 1095 | cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")] |
| 1096 | db_interface["external_name"] = get_str(cp, "name", 255) |
| 1097 | cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid |
| 1098 | cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid |
| 1099 | cp_name2db_interface[db_interface["external_name"]] = db_interface |
| 1100 | for cp_descriptor in vnfd_descriptor["connection-point"]: |
| 1101 | if cp_descriptor["name"] == db_interface["external_name"]: |
| 1102 | break |
| 1103 | else: |
| 1104 | raise KeyError() |
| 1105 | |
| 1106 | if vdu_id in vdu_id2cp_name: |
| 1107 | vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU |
| 1108 | else: |
| 1109 | vdu_id2cp_name[vdu_id] = db_interface["external_name"] |
| 1110 | |
| 1111 | # port security |
| 1112 | if str(cp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1113 | db_interface["port_security"] = 0 |
| 1114 | elif str(cp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1115 | db_interface["port_security"] = 1 |
| 1116 | except KeyError: |
| 1117 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| 1118 | "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present" |
| 1119 | " at connection-point".format( |
| 1120 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| 1121 | cp=iface.get("vnfd-connection-point-ref")), |
| 1122 | HTTP_Bad_Request) |
| 1123 | elif iface.get("internal-connection-point-ref"): |
| 1124 | try: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1125 | for icp_descriptor in vdu_descriptor["internal-connection-point"]: |
| 1126 | if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")): |
| 1127 | break |
| 1128 | else: |
| 1129 | raise KeyError("does not exist at vdu:internal-connection-point") |
| 1130 | icp = None |
| 1131 | icp_vld = None |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1132 | for vld in vnfd.get("internal-vld").itervalues(): |
| 1133 | for cp in vld.get("internal-connection-point").itervalues(): |
| 1134 | if cp.get("id-ref") == iface.get("internal-connection-point-ref"): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1135 | if icp: |
| 1136 | raise KeyError("is referenced by more than one 'internal-vld'") |
| 1137 | icp = cp |
| 1138 | icp_vld = vld |
| 1139 | if not icp: |
| 1140 | raise KeyError("is not referenced by any 'internal-vld'") |
| 1141 | |
| 1142 | db_interface["net_id"] = net_id2uuid[icp_vld.get("id")] |
| 1143 | if str(icp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1144 | db_interface["port_security"] = 0 |
| 1145 | elif str(icp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1146 | db_interface["port_security"] = 1 |
| 1147 | if icp.get("ip-address"): |
| 1148 | if not icp_vld.get("ip-profile-ref"): |
| 1149 | raise NfvoException |
| 1150 | db_interface["ip_address"] = str(icp.get("ip-address")) |
| 1151 | except KeyError as e: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1152 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1153 | "'interface[{iface}]':'internal-connection-point-ref':'{cp}'" |
| 1154 | " {msg}".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1155 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1156 | cp=iface.get("internal-connection-point-ref"), msg=str(e)), |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1157 | HTTP_Bad_Request) |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 1158 | if iface.get("position"): |
| 1159 | db_interface["created_at"] = int(iface.get("position")) * 50 |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1160 | if iface.get("mac-address"): |
| 1161 | db_interface["mac"] = str(iface.get("mac-address")) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1162 | db_interfaces.append(db_interface) |
| 1163 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1164 | # table flavors |
| 1165 | db_flavor = { |
| 1166 | "name": get_str(vdu, "name", 250) + "-flv", |
| 1167 | "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)), |
| 1168 | "ram": int(vdu["vm-flavor"].get("memory-mb", 1)), |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1169 | "disk": int(vdu["vm-flavor"].get("storage-gb", 0)), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1170 | } |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1171 | # TODO revise the case of several numa-node-policy node |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1172 | extended = {} |
| 1173 | numa = {} |
| 1174 | if devices: |
| 1175 | extended["devices"] = devices |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1176 | if flavor_epa_interfaces: |
| 1177 | numa["interfaces"] = flavor_epa_interfaces |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1178 | if vdu.get("guest-epa"): # TODO or dedicated_int: |
| 1179 | epa_vcpu_set = False |
| 1180 | if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int: |
| 1181 | numa_node_policy = vdu["guest-epa"].get("numa-node-policy") |
| 1182 | if numa_node_policy.get("node"): |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1183 | numa_node = numa_node_policy["node"].values()[0] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1184 | if numa_node.get("num-cores"): |
| 1185 | numa["cores"] = numa_node["num-cores"] |
| 1186 | epa_vcpu_set = True |
| 1187 | if numa_node.get("paired-threads"): |
| 1188 | if numa_node["paired-threads"].get("num-paired-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1189 | numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1190 | epa_vcpu_set = True |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1191 | if len(numa_node["paired-threads"].get("paired-thread-ids")): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1192 | numa["paired-threads-id"] = [] |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1193 | for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues(): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1194 | numa["paired-threads-id"].append( |
| 1195 | (str(pair["thread-a"]), str(pair["thread-b"])) |
| 1196 | ) |
| 1197 | if numa_node.get("num-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1198 | numa["threads"] = int(numa_node["num-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1199 | epa_vcpu_set = True |
| 1200 | if numa_node.get("memory-mb"): |
| 1201 | numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1) |
| 1202 | if vdu["guest-epa"].get("mempage-size"): |
| 1203 | if vdu["guest-epa"]["mempage-size"] != "SMALL": |
| 1204 | numa["memory"] = max(int(db_flavor["ram"] / 1024), 1) |
| 1205 | if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set: |
| 1206 | if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED": |
| 1207 | if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \ |
| 1208 | vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER": |
| 1209 | numa["cores"] = max(db_flavor["vcpus"], 1) |
| 1210 | else: |
| 1211 | numa["threads"] = max(db_flavor["vcpus"], 1) |
| 1212 | if numa: |
| 1213 | extended["numas"] = [numa] |
| 1214 | if extended: |
| 1215 | extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256) |
| 1216 | db_flavor["extended"] = extended_text |
| 1217 | # look if flavor exist |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1218 | temp_flavor_dict = {'disk': db_flavor.get('disk', 0), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1219 | 'ram': db_flavor.get('ram'), |
| 1220 | 'vcpus': db_flavor.get('vcpus'), |
| 1221 | 'extended': db_flavor.get('extended') |
| 1222 | } |
| 1223 | existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 1224 | if existing_flavors: |
| 1225 | flavor_uuid = existing_flavors[0]["uuid"] |
| 1226 | else: |
| 1227 | flavor_uuid = str(uuid4()) |
| 1228 | uuid_list.append(flavor_uuid) |
| 1229 | db_flavor["uuid"] = flavor_uuid |
| 1230 | db_flavors.append(db_flavor) |
| 1231 | db_vm["flavor_id"] = flavor_uuid |
| 1232 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1233 | # VNF affinity and antiaffinity |
| 1234 | for pg in vnfd.get("placement-groups").itervalues(): |
| 1235 | pg_name = get_str(pg, "name", 255) |
| 1236 | for vdu in pg.get("member-vdus").itervalues(): |
| 1237 | vdu_id = get_str(vdu, "member-vdu-ref", 255) |
| 1238 | if vdu_id not in vdu_id2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1239 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':" |
| 1240 | "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1241 | vnf=vnfd_id, pg=pg_name, vdu=vdu_id), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1242 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1243 | db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name |
| 1244 | # TODO consider the case of isolation and not colocation |
| 1245 | # if pg.get("strategy") == "ISOLATION": |
| 1246 | |
| 1247 | # VNF mgmt configuration |
| 1248 | mgmt_access = {} |
| 1249 | if vnfd["mgmt-interface"].get("vdu-id"): |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1250 | mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255) |
| 1251 | if mgmt_vdu_id not in vdu_id2uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1252 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':" |
| 1253 | "'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1254 | vnf=vnfd_id, vdu=mgmt_vdu_id), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1255 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1256 | mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1257 | # if only one cp is defined by this VDU, mark this interface as of type "mgmt" |
| 1258 | if vdu_id2cp_name.get(mgmt_vdu_id): |
| 1259 | cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt" |
| 1260 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1261 | if vnfd["mgmt-interface"].get("ip-address"): |
| 1262 | mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address")) |
| 1263 | if vnfd["mgmt-interface"].get("cp"): |
| 1264 | if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1265 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. " |
| 1266 | "Reference to a non-existing connection-point".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1267 | vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1268 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1269 | mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]] |
| 1270 | mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]] |
| tierno | e2ff1ce | 2017-11-02 17:01:10 +0100 | [diff] [blame] | 1271 | # mark this interface as of type mgmt |
| 1272 | cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt" |
| 1273 | |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 1274 | 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] | 1275 | "default-user", 64) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1276 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1277 | if default_user: |
| 1278 | mgmt_access["default_user"] = default_user |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1279 | required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}), |
| 1280 | "required", 6) |
| 1281 | if required: |
| 1282 | mgmt_access["required"] = required |
| 1283 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1284 | if mgmt_access: |
| 1285 | db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256) |
| 1286 | |
| 1287 | db_vnfs.append(db_vnf) |
| 1288 | db_tables=[ |
| 1289 | {"vnfs": db_vnfs}, |
| 1290 | {"nets": db_nets}, |
| 1291 | {"images": db_images}, |
| 1292 | {"flavors": db_flavors}, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1293 | {"ip_profiles": db_ip_profiles}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1294 | {"vms": db_vms}, |
| 1295 | {"interfaces": db_interfaces}, |
| 1296 | ] |
| 1297 | |
| 1298 | logger.debug("create_vnf Deployment done vnfDict: %s", |
| 1299 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 1300 | mydb.new_rows(db_tables, uuid_list) |
| 1301 | return vnfd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1302 | except NfvoException: |
| 1303 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1304 | except Exception as e: |
| 1305 | logger.error("Exception {}".format(e)) |
| 1306 | raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request) |
| 1307 | |
| 1308 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1309 | @deprecated("Use new_vnfd_v3") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1310 | def new_vnf(mydb, tenant_id, vnf_descriptor): |
| 1311 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1312 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1313 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1314 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1315 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1316 | vims = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1317 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1318 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1319 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1320 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1321 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 1322 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1323 | else: |
| 1324 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1325 | # 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] | 1326 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1327 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1328 | |
| 1329 | # Step 4. Review the descriptor and add missing fields |
| 1330 | #print vnf_descriptor |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1331 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1332 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1333 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1334 | if "physical" in vnf_descriptor['vnf']: |
| 1335 | del vnf_descriptor['vnf']['physical'] |
| 1336 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1337 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1338 | # 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] | 1339 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1340 | 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] | 1341 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1342 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1343 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1344 | 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] | 1345 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1346 | 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] | 1347 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1348 | VNFCitem={} |
| 1349 | VNFCitem["name"] = vnfc['name'] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1350 | VNFCitem["availability_zone"] = vnfc.get('availability_zone') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1351 | 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] | 1352 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1353 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1354 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1355 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1356 | 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] | 1357 | myflavorDict["description"] = VNFCitem["description"] |
| 1358 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1359 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1360 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1361 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1362 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1363 | devices = vnfc.get("devices") |
| 1364 | if devices != None: |
| 1365 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1366 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1367 | # TODO: |
| 1368 | # 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] | 1369 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1370 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1371 | # Previous code has been commented |
| 1372 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1373 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1374 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1375 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1376 | #else: |
| 1377 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1378 | # if result2: |
| 1379 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 1380 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 1381 | # else: |
| 1382 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 1383 | 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] | 1384 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1385 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1386 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1387 | |
| 1388 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1389 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1390 | # Step 6.2 New flavors are created in the VIM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1391 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1392 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1393 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1394 | VNFCitem["flavor_id"] = flavor_id |
| 1395 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1396 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1397 | logger.debug("Creating new images in the VIM for each VNFC") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1398 | # Step 6.3 New images are created in the VIM |
| 1399 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1400 | #This "for" loop might be integrated with the previous one |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1401 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1402 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1403 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1404 | image_dict={} |
| 1405 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1406 | image_dict['universal_name']=vnfc.get('image name') |
| 1407 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1408 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1409 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1410 | image_dict['checksum']=vnfc.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1411 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1412 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1413 | if image_metadata_dict is not None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1414 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1415 | image_dict['metadata']=image_metadata_str |
| 1416 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1417 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1418 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1419 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1420 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1421 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1422 | if vnfc.get("boot-data"): |
| 1423 | 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] | 1424 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1425 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1426 | # Step 7. Storing the VNF descriptor in the repository |
| 1427 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1428 | 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] | 1429 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1430 | # Step 8. Adding the VNF to the NFVO DB |
| 1431 | vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1432 | return vnf_id |
| 1433 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1434 | _, message = rollback(mydb, vims, rollback_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1435 | if isinstance(e, db_base_Exception): |
| 1436 | error_text = "Exception at database" |
| 1437 | elif isinstance(e, KeyError): |
| 1438 | error_text = "KeyError exception " |
| 1439 | e.http_code = HTTP_Internal_Server_Error |
| 1440 | else: |
| 1441 | error_text = "Exception at VIM" |
| 1442 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1443 | #logger.error("start_scenario %s", error_text) |
| 1444 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1445 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1446 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1447 | @deprecated("Use new_vnfd_v3") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1448 | def new_vnf_v02(mydb, tenant_id, vnf_descriptor): |
| 1449 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1450 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1451 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1452 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1453 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1454 | vims = {} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1455 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1456 | check_tenant(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1457 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1458 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| 1459 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 1460 | HTTP_Unauthorized) |
| 1461 | else: |
| 1462 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1463 | # 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] | 1464 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1465 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1466 | |
| 1467 | # Step 4. Review the descriptor and add missing fields |
| 1468 | #print vnf_descriptor |
| 1469 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| 1470 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1471 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1472 | if "physical" in vnf_descriptor['vnf']: |
| 1473 | del vnf_descriptor['vnf']['physical'] |
| 1474 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1475 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1476 | # 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] | 1477 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1478 | 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] | 1479 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1480 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1481 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1482 | rollback_list = [] # It will contain the new images created in mano. It is used for rollback |
| 1483 | try: |
| 1484 | logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC") |
| 1485 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1486 | VNFCitem={} |
| 1487 | VNFCitem["name"] = vnfc['name'] |
| 1488 | 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] | 1489 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1490 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1491 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1492 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1493 | 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] | 1494 | myflavorDict["description"] = VNFCitem["description"] |
| 1495 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1496 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1497 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1498 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1499 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1500 | devices = vnfc.get("devices") |
| 1501 | if devices != None: |
| 1502 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1503 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1504 | # TODO: |
| 1505 | # 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] | 1506 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1507 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1508 | # Previous code has been commented |
| 1509 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1510 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1511 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1512 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1513 | #else: |
| 1514 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1515 | # if result2: |
| 1516 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 1517 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 1518 | # else: |
| 1519 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 1520 | 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] | 1521 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1522 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1523 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1524 | |
| 1525 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1526 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1527 | # Step 6.2 New flavors are created in the VIM |
| 1528 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| 1529 | |
| 1530 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| 1531 | VNFCitem["flavor_id"] = flavor_id |
| 1532 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1533 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1534 | logger.debug("Creating new images in the VIM for each VNFC") |
| 1535 | # Step 6.3 New images are created in the VIM |
| 1536 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1537 | #This "for" loop might be integrated with the previous one |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1538 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1539 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1540 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1541 | image_dict={} |
| 1542 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1543 | image_dict['universal_name']=vnfc.get('image name') |
| 1544 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1545 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1546 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1547 | image_dict['checksum']=vnfc.get('image checksum') |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1548 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1549 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1550 | if image_metadata_dict is not None: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1551 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1552 | image_dict['metadata']=image_metadata_str |
| 1553 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| 1554 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1555 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| 1556 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1557 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1558 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1559 | if vnfc.get("boot-data"): |
| 1560 | 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] | 1561 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1562 | # Step 7. Storing the VNF descriptor in the repository |
| 1563 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1564 | 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] | 1565 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1566 | # Step 8. Adding the VNF to the NFVO DB |
| 1567 | vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1568 | return vnf_id |
| 1569 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| 1570 | _, message = rollback(mydb, vims, rollback_list) |
| 1571 | if isinstance(e, db_base_Exception): |
| 1572 | error_text = "Exception at database" |
| 1573 | elif isinstance(e, KeyError): |
| 1574 | error_text = "KeyError exception " |
| 1575 | e.http_code = HTTP_Internal_Server_Error |
| 1576 | else: |
| 1577 | error_text = "Exception at VIM" |
| 1578 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1579 | #logger.error("start_scenario %s", error_text) |
| 1580 | raise NfvoException(error_text, e.http_code) |
| 1581 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1582 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1583 | def get_vnf_id(mydb, tenant_id, vnf_id): |
| 1584 | #check valid tenant_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1585 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1586 | #obtain data |
| 1587 | where_or = {} |
| 1588 | if tenant_id != "any": |
| 1589 | where_or["tenant_id"] = tenant_id |
| 1590 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1591 | vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") |
| 1592 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1593 | vnf_id = vnf["uuid"] |
| 1594 | filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1595 | 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] | 1596 | #change_keys_http2db(filtered_content, http2db_vnf, reverse=True) |
| 1597 | data={'vnf' : filtered_content} |
| 1598 | #GET VM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1599 | 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] | 1600 | SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description', |
| 1601 | 'boot_data'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1602 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1603 | if len(content)==0: |
| 1604 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1605 | # change boot_data into boot-data |
| 1606 | for vm in content: |
| 1607 | if vm.get("boot_data"): |
| 1608 | vm["boot-data"] = yaml.safe_load(vm["boot_data"]) |
| 1609 | del vm["boot_data"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1610 | |
| 1611 | data['vnf']['VNFC'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1612 | #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] | 1613 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1614 | #GET NET |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1615 | 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] | 1616 | SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'), |
| 1617 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1618 | data['vnf']['nets'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1619 | |
| 1620 | #GET ip-profile for each net |
| 1621 | for net in data['vnf']['nets']: |
| 1622 | ipprofiles = mydb.get_rows(FROM='ip_profiles', |
| 1623 | SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'), |
| 1624 | WHERE={'net_id': net["uuid"]} ) |
| 1625 | if len(ipprofiles)==1: |
| 1626 | net["ip_profile"] = ipprofiles[0] |
| 1627 | elif len(ipprofiles)>1: |
| 1628 | raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1629 | |
| 1630 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1631 | #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] | 1632 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1633 | #GET External Interfaces |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1634 | 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] | 1635 | SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \ |
| 1636 | '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] | 1637 | WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1638 | #print content |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1639 | data['vnf']['external-connections'] = content |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1640 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1641 | return data |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1642 | |
| 1643 | |
| 1644 | def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None): |
| 1645 | # Check tenant exist |
| 1646 | if tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1647 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1648 | # Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1649 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1650 | else: |
| 1651 | vims={} |
| 1652 | |
| 1653 | # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided" |
| 1654 | where_or = {} |
| 1655 | if tenant_id != "any": |
| 1656 | where_or["tenant_id"] = tenant_id |
| 1657 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1658 | 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] | 1659 | vnf_id = vnf["uuid"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1660 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1661 | # "Getting the list of flavors and tenants of the VNF" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1662 | flavorList = get_flavorlist(mydb, vnf_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1663 | if len(flavorList)==0: |
| 1664 | 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] | 1665 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1666 | imageList = get_imagelist(mydb, vnf_id) |
| 1667 | if len(imageList)==0: |
| 1668 | 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] | 1669 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1670 | deleted = mydb.delete_row_by_id('vnfs', vnf_id) |
| 1671 | if deleted == 0: |
| 1672 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1673 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1674 | undeletedItems = [] |
| 1675 | for flavor in flavorList: |
| 1676 | #check if flavor is used by other vnf |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1677 | try: |
| 1678 | c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} ) |
| 1679 | if len(c) > 0: |
| 1680 | logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor) |
| 1681 | continue |
| 1682 | #flavor not used, must be deleted |
| 1683 | #delelte at VIM |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1684 | c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1685 | for flavor_vim in c: |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1686 | if not flavor_vim['created']: # skip this flavor because not created by openmano |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1687 | continue |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1688 | # look for vim |
| 1689 | myvim = None |
| 1690 | for vim in vims.values(): |
| 1691 | if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]: |
| 1692 | myvim = vim |
| 1693 | break |
| 1694 | if not myvim: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1695 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1696 | try: |
| 1697 | myvim.delete_flavor(flavor_vim["vim_id"]) |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1698 | except vimconn.vimconnNotFoundException: |
| 1699 | logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], |
| 1700 | flavor_vim["datacenter_vim_id"] ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1701 | except vimconn.vimconnException as e: |
| 1702 | 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] | 1703 | flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e)) |
| 1704 | undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], |
| 1705 | flavor_vim["datacenter_vim_id"])) |
| 1706 | # 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] | 1707 | mydb.delete_row_by_id('flavors', flavor) |
| 1708 | except db_base_Exception as e: |
| 1709 | 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] | 1710 | undeletedItems.append("flavor {}".format(flavor)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1711 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1712 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1713 | for image in imageList: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1714 | try: |
| 1715 | #check if image is used by other vnf |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 1716 | 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] | 1717 | if len(c) > 0: |
| 1718 | logger.debug("Image '%s' not deleted because it is being used by another VNF", image) |
| 1719 | continue |
| 1720 | #image not used, must be deleted |
| 1721 | #delelte at VIM |
| 1722 | c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1723 | for image_vim in c: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 1724 | 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] | 1725 | continue |
| 1726 | if image_vim['created']=='false': #skip this image because not created by openmano |
| 1727 | continue |
| 1728 | myvim=vims[ image_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1729 | try: |
| 1730 | myvim.delete_image(image_vim["vim_id"]) |
| 1731 | except vimconn.vimconnNotFoundException as e: |
| 1732 | logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] ) |
| 1733 | except vimconn.vimconnException as e: |
| 1734 | logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s", |
| 1735 | image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1736 | 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] | 1737 | #delete image from Database, using table images and with cascade foreign key also at datacenters_images |
| 1738 | mydb.delete_row_by_id('images', image) |
| 1739 | except db_base_Exception as e: |
| 1740 | 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] | 1741 | undeletedItems.append("image %s" % image) |
| 1742 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1743 | return vnf_id + " " + vnf["name"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1744 | #if undeletedItems: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1745 | # return "delete_vnf. Undeleted: %s" %(undeletedItems) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1746 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1747 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1748 | @deprecated("Not used") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1749 | def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None): |
| 1750 | result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name) |
| 1751 | if result < 0: |
| 1752 | return result, vims |
| 1753 | elif result == 0: |
| 1754 | return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name |
| 1755 | myvim = vims.values()[0] |
| 1756 | result,servers = myvim.get_hosts_info() |
| 1757 | if result < 0: |
| 1758 | return result, servers |
| 1759 | topology = {'name':myvim['name'] , 'servers': servers} |
| 1760 | return result, topology |
| 1761 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1762 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1763 | def get_hosts(mydb, nfvo_tenant_id): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1764 | vims = get_vim(mydb, nfvo_tenant_id) |
| 1765 | if len(vims) == 0: |
| 1766 | raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found) |
| 1767 | elif len(vims)>1: |
| 1768 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 1769 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1770 | myvim = vims.values()[0] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1771 | try: |
| 1772 | hosts = myvim.get_hosts() |
| 1773 | 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] | 1774 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1775 | datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] } |
| 1776 | for host in hosts: |
| 1777 | server={'name':host['name'], 'vms':[]} |
| 1778 | for vm in host['instances']: |
| 1779 | #get internal name and model |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1780 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1781 | c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\ |
| 1782 | WHERE={'vim_vm_id':vm['id']} ) |
| 1783 | if len(c) == 0: |
| 1784 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id'])) |
| 1785 | continue |
| 1786 | server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1787 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1788 | except db_base_Exception as e: |
| 1789 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e))) |
| 1790 | datacenter['Datacenters'][0]['servers'].append(server) |
| 1791 | #return -400, "en construccion" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1792 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1793 | #print 'datacenters '+ json.dumps(datacenter, indent=4) |
| 1794 | return datacenter |
| 1795 | except vimconn.vimconnException as e: |
| 1796 | 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] | 1797 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1798 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 1799 | @deprecated("Use new_nsd_v3") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1800 | def new_scenario(mydb, tenant_id, topo): |
| 1801 | |
| 1802 | # result, vims = get_vim(mydb, tenant_id) |
| 1803 | # if result < 0: |
| 1804 | # return result, vims |
| 1805 | #1: parse input |
| 1806 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1807 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1808 | if "tenant_id" in topo: |
| 1809 | if topo["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1810 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id), |
| 1811 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1812 | else: |
| 1813 | tenant_id=None |
| 1814 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1815 | #1.1: get VNFs and external_networks (other_nets). |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1816 | vnfs={} |
| 1817 | other_nets={} #external_networks, bridge_networks and data_networkds |
| 1818 | nodes = topo['topology']['nodes'] |
| 1819 | for k in nodes.keys(): |
| 1820 | if nodes[k]['type'] == 'VNF': |
| 1821 | vnfs[k] = nodes[k] |
| 1822 | vnfs[k]['ifaces'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1823 | elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1824 | other_nets[k] = nodes[k] |
| 1825 | other_nets[k]['external']=True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1826 | elif nodes[k]['type'] == 'network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1827 | other_nets[k] = nodes[k] |
| 1828 | other_nets[k]['external']=False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1829 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1830 | |
| 1831 | #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces |
| 1832 | for name,vnf in vnfs.items(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1833 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1834 | error_text = "" |
| 1835 | error_pos = "'topology':'nodes':'" + name + "'" |
| 1836 | if 'vnf_id' in vnf: |
| 1837 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1838 | where['uuid'] = vnf['vnf_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1839 | if 'VNF model' in vnf: |
| 1840 | error_text += " 'VNF model' " + vnf['VNF model'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1841 | where['name'] = vnf['VNF model'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1842 | if len(where) == 1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1843 | raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1844 | |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1845 | vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), |
| 1846 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1847 | WHERE=where) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1848 | if len(vnf_db)==0: |
| 1849 | raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| 1850 | elif len(vnf_db)>1: |
| 1851 | raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1852 | vnf['uuid']=vnf_db[0]['uuid'] |
| 1853 | vnf['description']=vnf_db[0]['description'] |
| 1854 | #get external interfaces |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1855 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), |
| 1856 | 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] | 1857 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1858 | for ext_iface in ext_ifaces: |
| 1859 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']} |
| 1860 | |
| 1861 | #1.4 get list of connections |
| 1862 | conections = topo['topology']['connections'] |
| 1863 | conections_list = [] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1864 | conections_list_name = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1865 | for k in conections.keys(): |
| 1866 | if type(conections[k]['nodes'])==dict: #dict with node:iface pairs |
| 1867 | ifaces_list = conections[k]['nodes'].items() |
| 1868 | elif type(conections[k]['nodes'])==list: #list with dictionary |
| 1869 | ifaces_list=[] |
| 1870 | conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] ) |
| 1871 | for k2 in conection_pair_list: |
| 1872 | ifaces_list += k2 |
| 1873 | |
| 1874 | con_type = conections[k].get("type", "link") |
| 1875 | if con_type != "link": |
| 1876 | if k in other_nets: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1877 | raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1878 | other_nets[k] = {'external': False} |
| 1879 | if conections[k].get("graph"): |
| 1880 | other_nets[k]["graph"] = conections[k]["graph"] |
| 1881 | ifaces_list.append( (k, None) ) |
| 1882 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1883 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1884 | if con_type == "external_network": |
| 1885 | other_nets[k]['external'] = True |
| 1886 | if conections[k].get("model"): |
| 1887 | other_nets[k]["model"] = conections[k]["model"] |
| 1888 | else: |
| 1889 | other_nets[k]["model"] = k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1890 | if con_type == "dataplane_net" or con_type == "bridge_net": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1891 | other_nets[k]["model"] = con_type |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1892 | |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1893 | conections_list_name.append(k) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1894 | 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) |
| 1895 | #print set(ifaces_list) |
| 1896 | #check valid VNF and iface names |
| 1897 | for iface in ifaces_list: |
| 1898 | if iface[0] not in vnfs and iface[0] not in other_nets : |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1899 | raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format( |
| 1900 | str(k), iface[0]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1901 | 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] | 1902 | raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format( |
| 1903 | str(k), iface[0], iface[1]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1904 | |
| 1905 | #1.5 unify connections from the pair list to a consolidated list |
| 1906 | index=0 |
| 1907 | while index < len(conections_list): |
| 1908 | index2 = index+1 |
| 1909 | while index2 < len(conections_list): |
| 1910 | if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets |
| 1911 | conections_list[index] |= conections_list[index2] |
| 1912 | del conections_list[index2] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1913 | del conections_list_name[index2] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1914 | else: |
| 1915 | index2 += 1 |
| 1916 | conections_list[index] = list(conections_list[index]) # from set to list again |
| 1917 | index += 1 |
| 1918 | #for k in conections_list: |
| 1919 | # print k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1920 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1921 | |
| 1922 | |
| 1923 | #1.6 Delete non external nets |
| 1924 | # for k in other_nets.keys(): |
| 1925 | # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net': |
| 1926 | # for con in conections_list: |
| 1927 | # delete_indexes=[] |
| 1928 | # for index in range(0,len(con)): |
| 1929 | # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower |
| 1930 | # for index in delete_indexes: |
| 1931 | # del con[index] |
| 1932 | # del other_nets[k] |
| 1933 | #1.7: Check external_ports are present at database table datacenter_nets |
| 1934 | for k,net in other_nets.items(): |
| 1935 | error_pos = "'topology':'nodes':'" + k + "'" |
| 1936 | if net['external']==False: |
| 1937 | if 'name' not in net: |
| 1938 | net['name']=k |
| 1939 | if 'model' not in net: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1940 | raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1941 | if net['model']=='bridge_net': |
| 1942 | net['type']='bridge'; |
| 1943 | elif net['model']=='dataplane_net': |
| 1944 | net['type']='data'; |
| 1945 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1946 | raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1947 | else: #external |
| 1948 | #IF we do not want to check that external network exist at datacenter |
| 1949 | pass |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1950 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1951 | # error_text = "" |
| 1952 | # WHERE_={} |
| 1953 | # if 'net_id' in net: |
| 1954 | # error_text += " 'net_id' " + net['net_id'] |
| 1955 | # WHERE_['uuid'] = net['net_id'] |
| 1956 | # if 'model' in net: |
| 1957 | # error_text += " 'model' " + net['model'] |
| 1958 | # WHERE_['name'] = net['model'] |
| 1959 | # if len(WHERE_) == 0: |
| 1960 | # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos |
| 1961 | # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'), |
| 1962 | # FROM='datacenter_nets', WHERE=WHERE_ ) |
| 1963 | # if r<0: |
| 1964 | # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db |
| 1965 | # elif r==0: |
| 1966 | # print "nfvo.new_scenario Error" +error_text+ " is not present at database" |
| 1967 | # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos |
| 1968 | # elif r>1: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1969 | # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database" |
| 1970 | # return -HTTP_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] | 1971 | # other_nets[k].update(net_db[0]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1972 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1973 | net_list={} |
| 1974 | net_nb=0 #Number of nets |
| 1975 | for con in conections_list: |
| 1976 | #check if this is connected to a external net |
| 1977 | other_net_index=-1 |
| 1978 | #print |
| 1979 | #print "con", con |
| 1980 | for index in range(0,len(con)): |
| 1981 | #check if this is connected to a external net |
| 1982 | for net_key in other_nets.keys(): |
| 1983 | if con[index][0]==net_key: |
| 1984 | if other_net_index>=0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1985 | 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] | 1986 | #print "nfvo.new_scenario " + error_text |
| 1987 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1988 | else: |
| 1989 | other_net_index = index |
| 1990 | net_target = net_key |
| 1991 | break |
| 1992 | #print "other_net_index", other_net_index |
| 1993 | try: |
| 1994 | if other_net_index>=0: |
| 1995 | del con[other_net_index] |
| 1996 | #IF we do not want to check that external network exist at datacenter |
| 1997 | if other_nets[net_target]['external'] : |
| 1998 | if "name" not in other_nets[net_target]: |
| 1999 | other_nets[net_target]['name'] = other_nets[net_target]['model'] |
| 2000 | if other_nets[net_target]["type"] == "external_network": |
| 2001 | if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data": |
| 2002 | other_nets[net_target]["type"] = "data" |
| 2003 | else: |
| 2004 | other_nets[net_target]["type"] = "bridge" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2005 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2006 | # if other_nets[net_target]['external'] : |
| 2007 | # 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 |
| 2008 | # if type_=='data' and other_nets[net_target]['type']=="ptp": |
| 2009 | # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target) |
| 2010 | # print "nfvo.new_scenario " + error_text |
| 2011 | # return -HTTP_Bad_Request, error_text |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2012 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2013 | for iface in con: |
| 2014 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2015 | else: |
| 2016 | #create a net |
| 2017 | net_type_bridge=False |
| 2018 | net_type_data=False |
| 2019 | net_target = "__-__net"+str(net_nb) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2020 | 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] | 2021 | 'description':"net-%s in scenario %s" %(net_nb,topo['name']), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2022 | 'external':False} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2023 | for iface in con: |
| 2024 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2025 | iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type'] |
| 2026 | if iface_type=='mgmt' or iface_type=='bridge': |
| 2027 | net_type_bridge = True |
| 2028 | else: |
| 2029 | net_type_data = True |
| 2030 | if net_type_bridge and net_type_data: |
| 2031 | 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] | 2032 | #print "nfvo.new_scenario " + error_text |
| 2033 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2034 | elif net_type_bridge: |
| 2035 | type_='bridge' |
| 2036 | else: |
| 2037 | type_='data' if len(con)>2 else 'ptp' |
| 2038 | net_list[net_target]['type'] = type_ |
| 2039 | net_nb+=1 |
| 2040 | except Exception: |
| 2041 | 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] | 2042 | #print "nfvo.new_scenario " + error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2043 | #raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2044 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2045 | |
| 2046 | #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] | 2047 | #1.8.1 obtain management net |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2048 | mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2049 | FROM='datacenter_nets', WHERE={'name':'mgmt'} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2050 | #1.8.2 check all interfaces from all vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2051 | if len(mgmt_net)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2052 | add_mgmt_net = False |
| 2053 | for vnf in vnfs.values(): |
| 2054 | for iface in vnf['ifaces'].values(): |
| 2055 | if iface['type']=='mgmt' and 'net_key' not in iface: |
| 2056 | #iface not connected |
| 2057 | iface['net_key'] = 'mgmt' |
| 2058 | add_mgmt_net = True |
| 2059 | if add_mgmt_net and 'mgmt' not in net_list: |
| 2060 | net_list['mgmt']=mgmt_net[0] |
| 2061 | net_list['mgmt']['external']=True |
| 2062 | net_list['mgmt']['graph']={'visible':False} |
| 2063 | |
| 2064 | net_list.update(other_nets) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2065 | #print |
| 2066 | #print 'net_list', net_list |
| 2067 | #print |
| 2068 | #print 'vnfs', vnfs |
| 2069 | #print |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2070 | |
| 2071 | #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2072 | c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list, |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2073 | 'tenant_id':tenant_id, 'name':topo['name'], |
| 2074 | 'description':topo.get('description',topo['name']), |
| 2075 | 'public': topo.get('public', False) |
| 2076 | }) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2077 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2078 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2079 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2080 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 2081 | @deprecated("Use new_nsd_v3") |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2082 | def new_scenario_v02(mydb, tenant_id, scenario_dict, version): |
| 2083 | """ This creates a new scenario for version 0.2 and 0.3""" |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2084 | scenario = scenario_dict["scenario"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2085 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2086 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2087 | if "tenant_id" in scenario: |
| 2088 | if scenario["tenant_id"] != tenant_id: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2089 | # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2090 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format( |
| 2091 | scenario["tenant_id"], tenant_id), HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2092 | else: |
| 2093 | tenant_id=None |
| 2094 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2095 | # 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] | 2096 | for name,vnf in scenario["vnfs"].iteritems(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2097 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2098 | error_text = "" |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 2099 | error_pos = "'scenario':'vnfs':'" + name + "'" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2100 | if 'vnf_id' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2101 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2102 | where['uuid'] = vnf['vnf_id'] |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2103 | if 'vnf_name' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2104 | error_text += " 'vnf_name' " + vnf['vnf_name'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2105 | where['name'] = vnf['vnf_name'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2106 | if len(where) == 1: |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 2107 | raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2108 | vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'), |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2109 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2110 | WHERE=where) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2111 | if len(vnf_db) == 0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2112 | raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2113 | elif len(vnf_db) > 1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2114 | raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2115 | vnf['uuid'] = vnf_db[0]['uuid'] |
| 2116 | vnf['description'] = vnf_db[0]['description'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2117 | vnf['ifaces'] = {} |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2118 | # get external interfaces |
| 2119 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'), |
| 2120 | 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] | 2121 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2122 | for ext_iface in ext_ifaces: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2123 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']} |
| 2124 | # 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] | 2125 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2126 | # 2: Insert net_key and ip_address at every vnf interface |
| 2127 | for net_name, net in scenario["networks"].items(): |
| 2128 | net_type_bridge = False |
| 2129 | net_type_data = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2130 | for iface_dict in net["interfaces"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2131 | if version == "0.2": |
| 2132 | temp_dict = iface_dict |
| 2133 | ip_address = None |
| 2134 | elif version == "0.3": |
| 2135 | temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]} |
| 2136 | ip_address = iface_dict.get('ip_address', None) |
| 2137 | for vnf, iface in temp_dict.items(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2138 | if vnf not in scenario["vnfs"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2139 | error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format( |
| 2140 | net_name, vnf) |
| 2141 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2142 | raise NfvoException(error_text, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2143 | if iface not in scenario["vnfs"][vnf]['ifaces']: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2144 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\ |
| 2145 | .format(net_name, iface) |
| 2146 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2147 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2148 | if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2149 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\ |
| 2150 | "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key']) |
| 2151 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2152 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2153 | scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2154 | scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2155 | iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type'] |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2156 | if iface_type == 'mgmt' or iface_type == 'bridge': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2157 | net_type_bridge = True |
| 2158 | else: |
| 2159 | net_type_data = True |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2160 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2161 | if net_type_bridge and net_type_data: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2162 | error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\ |
| 2163 | .format(net_name) |
| 2164 | # logger.debug("nfvo.new_scenario " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2165 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2166 | elif net_type_bridge: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2167 | type_ = 'bridge' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2168 | else: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2169 | type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp' |
| 2170 | |
| 2171 | if net.get("implementation"): # for v0.3 |
| 2172 | if type_ == "bridge" and net["implementation"] == "underlay": |
| 2173 | error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\ |
| 2174 | "'network':'{}'".format(net_name) |
| 2175 | # logger.debug(error_text) |
| 2176 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2177 | elif type_ != "bridge" and net["implementation"] == "overlay": |
| 2178 | error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\ |
| 2179 | "'network':'{}'".format(net_name) |
| 2180 | # logger.debug(error_text) |
| 2181 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2182 | net.pop("implementation") |
| 2183 | if "type" in net and version == "0.3": # for v0.3 |
| 2184 | if type_ == "data" and net["type"] == "e-line": |
| 2185 | error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\ |
| 2186 | "'e-line' at 'network':'{}'".format(net_name) |
| 2187 | # logger.debug(error_text) |
| 2188 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2189 | elif type_ == "ptp" and net["type"] == "e-lan": |
| 2190 | type_ = "data" |
| 2191 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2192 | net['type'] = type_ |
| 2193 | net['name'] = net_name |
| 2194 | net['external'] = net.get('external', False) |
| 2195 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2196 | # 3: insert at database |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2197 | scenario["nets"] = scenario["networks"] |
| 2198 | scenario['tenant_id'] = tenant_id |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2199 | scenario_id = mydb.new_scenario(scenario) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2200 | return scenario_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2201 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2202 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2203 | def new_nsd_v3(mydb, tenant_id, nsd_descriptor): |
| 2204 | """ |
| 2205 | Parses an OSM IM nsd_catalog and insert at DB |
| 2206 | :param mydb: |
| 2207 | :param tenant_id: |
| 2208 | :param nsd_descriptor: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2209 | :return: The list of created NSD ids |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2210 | """ |
| 2211 | try: |
| 2212 | mynsd = nsd_catalog.nsd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 2213 | try: |
| 2214 | pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd) |
| 2215 | except Exception as e: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2216 | raise NfvoException("Error. Invalid NS descriptor format: " + str(e), HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2217 | db_scenarios = [] |
| 2218 | db_sce_nets = [] |
| 2219 | db_sce_vnfs = [] |
| 2220 | db_sce_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2221 | db_sce_vnffgs = [] |
| 2222 | db_sce_rsps = [] |
| 2223 | db_sce_rsp_hops = [] |
| 2224 | db_sce_classifiers = [] |
| 2225 | db_sce_classifier_matches = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2226 | db_ip_profiles = [] |
| 2227 | db_ip_profiles_index = 0 |
| 2228 | uuid_list = [] |
| 2229 | nsd_uuid_list = [] |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2230 | for nsd_yang in mynsd.nsd_catalog.nsd.itervalues(): |
| 2231 | nsd = nsd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2232 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2233 | # table scenarios |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2234 | scenario_uuid = str(uuid4()) |
| 2235 | uuid_list.append(scenario_uuid) |
| 2236 | nsd_uuid_list.append(scenario_uuid) |
| 2237 | db_scenario = { |
| 2238 | "uuid": scenario_uuid, |
| 2239 | "osm_id": get_str(nsd, "id", 255), |
| 2240 | "name": get_str(nsd, "name", 255), |
| 2241 | "description": get_str(nsd, "description", 255), |
| 2242 | "tenant_id": tenant_id, |
| 2243 | "vendor": get_str(nsd, "vendor", 255), |
| 2244 | "short_name": get_str(nsd, "short-name", 255), |
| 2245 | "descriptor": str(nsd_descriptor)[:60000], |
| 2246 | } |
| 2247 | db_scenarios.append(db_scenario) |
| 2248 | |
| 2249 | # table sce_vnfs (constituent-vnfd) |
| 2250 | vnf_index2scevnf_uuid = {} |
| 2251 | vnf_index2vnf_uuid = {} |
| 2252 | for vnf in nsd.get("constituent-vnfd").itervalues(): |
| 2253 | existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255], |
| 2254 | 'tenant_id': tenant_id}) |
| 2255 | if not existing_vnf: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2256 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':" |
| 2257 | "'{}'. Reference to a non-existing VNFD in the catalog".format( |
| 2258 | str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]), |
| 2259 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2260 | sce_vnf_uuid = str(uuid4()) |
| 2261 | uuid_list.append(sce_vnf_uuid) |
| 2262 | db_sce_vnf = { |
| 2263 | "uuid": sce_vnf_uuid, |
| 2264 | "scenario_id": scenario_uuid, |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 2265 | # "name": get_str(vnf, "member-vnf-index", 255), |
| 2266 | "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2267 | "vnf_id": existing_vnf[0]["uuid"], |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2268 | "member_vnf_index": str(vnf["member-vnf-index"]), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2269 | # TODO 'start-by-default': True |
| 2270 | } |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2271 | vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid |
| 2272 | vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2273 | db_sce_vnfs.append(db_sce_vnf) |
| 2274 | |
| 2275 | # table ip_profiles (ip-profiles) |
| 2276 | ip_profile_name2db_table_index = {} |
| 2277 | for ip_profile in nsd.get("ip-profiles").itervalues(): |
| 2278 | db_ip_profile = { |
| 2279 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 2280 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 2281 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 2282 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 2283 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 2284 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 2285 | } |
| 2286 | dns_list = [] |
| 2287 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 2288 | dns_list.append(str(dns.get("address"))) |
| 2289 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 2290 | if ip_profile["ip-profile-params"].get('security-group'): |
| 2291 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 2292 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 2293 | db_ip_profiles_index += 1 |
| 2294 | db_ip_profiles.append(db_ip_profile) |
| 2295 | |
| 2296 | # table sce_nets (internal-vld) |
| 2297 | for vld in nsd.get("vld").itervalues(): |
| 2298 | sce_net_uuid = str(uuid4()) |
| 2299 | uuid_list.append(sce_net_uuid) |
| 2300 | db_sce_net = { |
| 2301 | "uuid": sce_net_uuid, |
| 2302 | "name": get_str(vld, "name", 255), |
| 2303 | "scenario_id": scenario_uuid, |
| 2304 | # "type": #TODO |
| 2305 | "multipoint": not vld.get("type") == "ELINE", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 2306 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2307 | # "external": #TODO |
| 2308 | "description": get_str(vld, "description", 255), |
| 2309 | } |
| 2310 | # guess type of network |
| 2311 | if vld.get("mgmt-network"): |
| 2312 | db_sce_net["type"] = "bridge" |
| 2313 | db_sce_net["external"] = True |
| 2314 | elif vld.get("provider-network").get("overlay-type") == "VLAN": |
| 2315 | db_sce_net["type"] = "data" |
| 2316 | else: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2317 | # later on it will be fixed to bridge or data depending on the type of interfaces attached to it |
| 2318 | db_sce_net["type"] = None |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2319 | db_sce_nets.append(db_sce_net) |
| 2320 | |
| 2321 | # ip-profile, link db_ip_profile with db_sce_net |
| 2322 | if vld.get("ip-profile-ref"): |
| 2323 | ip_profile_name = vld.get("ip-profile-ref") |
| 2324 | if ip_profile_name not in ip_profile_name2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2325 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'." |
| 2326 | " Reference to a non-existing 'ip_profiles'".format( |
| 2327 | str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| 2328 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2329 | 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] | 2330 | elif vld.get("vim-network-name"): |
| 2331 | db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2332 | |
| 2333 | # table sce_interfaces (vld:vnfd-connection-point-ref) |
| 2334 | for iface in vld.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2335 | vnf_index = str(iface['member-vnf-index-ref']) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2336 | # check correct parameters |
| 2337 | if vnf_index not in vnf_index2vnf_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2338 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2339 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2340 | "'nsd':'constituent-vnfd'".format( |
| 2341 | str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])), |
| 2342 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2343 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2344 | 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] | 2345 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2346 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2347 | 'external_name': get_str(iface, "vnfd-connection-point-ref", |
| 2348 | 255)}) |
| 2349 | if not existing_ifaces: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2350 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2351 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2352 | "connection-point name at VNFD '{}'".format( |
| 2353 | str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2354 | str(iface.get("vnfd-id-ref"))[:255]), |
| 2355 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2356 | interface_uuid = existing_ifaces[0]["uuid"] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2357 | if existing_ifaces[0]["iface_type"] == "data" and not db_sce_net["type"]: |
| 2358 | db_sce_net["type"] = "data" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2359 | sce_interface_uuid = str(uuid4()) |
| 2360 | uuid_list.append(sce_net_uuid) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2361 | iface_ip_address = None |
| 2362 | if iface.get("ip-address"): |
| 2363 | iface_ip_address = str(iface.get("ip-address")) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2364 | db_sce_interface = { |
| 2365 | "uuid": sce_interface_uuid, |
| 2366 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2367 | "sce_net_id": sce_net_uuid, |
| 2368 | "interface_id": interface_uuid, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2369 | "ip_address": iface_ip_address, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2370 | } |
| 2371 | db_sce_interfaces.append(db_sce_interface) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2372 | if not db_sce_net["type"]: |
| 2373 | db_sce_net["type"] = "bridge" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2374 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2375 | # table sce_vnffgs (vnffgd) |
| 2376 | for vnffg in nsd.get("vnffgd").itervalues(): |
| 2377 | sce_vnffg_uuid = str(uuid4()) |
| 2378 | uuid_list.append(sce_vnffg_uuid) |
| 2379 | db_sce_vnffg = { |
| 2380 | "uuid": sce_vnffg_uuid, |
| 2381 | "name": get_str(vnffg, "name", 255), |
| 2382 | "scenario_id": scenario_uuid, |
| 2383 | "vendor": get_str(vnffg, "vendor", 255), |
| 2384 | "description": get_str(vld, "description", 255), |
| 2385 | } |
| 2386 | db_sce_vnffgs.append(db_sce_vnffg) |
| 2387 | |
| 2388 | # deal with rsps |
| 2389 | db_sce_rsps = [] |
| 2390 | for rsp in vnffg.get("rsp").itervalues(): |
| 2391 | sce_rsp_uuid = str(uuid4()) |
| 2392 | uuid_list.append(sce_rsp_uuid) |
| 2393 | db_sce_rsp = { |
| 2394 | "uuid": sce_rsp_uuid, |
| 2395 | "name": get_str(rsp, "name", 255), |
| 2396 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2397 | "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code |
| 2398 | } |
| 2399 | db_sce_rsps.append(db_sce_rsp) |
| 2400 | db_sce_rsp_hops = [] |
| 2401 | for iface in rsp.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2402 | vnf_index = str(iface['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2403 | if_order = int(iface['order']) |
| 2404 | # check correct parameters |
| 2405 | if vnf_index not in vnf_index2vnf_uuid: |
| 2406 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2407 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2408 | "'nsd':'constituent-vnfd'".format( |
| 2409 | str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])), |
| 2410 | HTTP_Bad_Request) |
| 2411 | |
| 2412 | existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2413 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2414 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2415 | 'external_name': get_str(iface, "vnfd-connection-point-ref", |
| 2416 | 255)}) |
| 2417 | if not existing_ifaces: |
| 2418 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2419 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2420 | "connection-point name at VNFD '{}'".format( |
| 2421 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2422 | str(iface.get("vnfd-id-ref"))[:255]), |
| 2423 | HTTP_Bad_Request) |
| 2424 | interface_uuid = existing_ifaces[0]["uuid"] |
| 2425 | sce_rsp_hop_uuid = str(uuid4()) |
| 2426 | uuid_list.append(sce_rsp_hop_uuid) |
| 2427 | db_sce_rsp_hop = { |
| 2428 | "uuid": sce_rsp_hop_uuid, |
| 2429 | "if_order": if_order, |
| 2430 | "interface_id": interface_uuid, |
| 2431 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2432 | "sce_rsp_id": sce_rsp_uuid, |
| 2433 | } |
| 2434 | db_sce_rsp_hops.append(db_sce_rsp_hop) |
| 2435 | |
| 2436 | # deal with classifiers |
| 2437 | db_sce_classifiers = [] |
| 2438 | for classifier in vnffg.get("classifier").itervalues(): |
| 2439 | sce_classifier_uuid = str(uuid4()) |
| 2440 | uuid_list.append(sce_classifier_uuid) |
| 2441 | |
| 2442 | # source VNF |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2443 | vnf_index = str(classifier['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2444 | if vnf_index not in vnf_index2vnf_uuid: |
| 2445 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point" |
| 2446 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2447 | "'nsd':'constituent-vnfd'".format( |
| 2448 | str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])), |
| 2449 | HTTP_Bad_Request) |
| 2450 | existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2451 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2452 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2453 | 'external_name': get_str(classifier, "vnfd-connection-point-ref", |
| 2454 | 255)}) |
| 2455 | if not existing_ifaces: |
| 2456 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2457 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2458 | "connection-point name at VNFD '{}'".format( |
| 2459 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2460 | str(iface.get("vnfd-id-ref"))[:255]), |
| 2461 | HTTP_Bad_Request) |
| 2462 | interface_uuid = existing_ifaces[0]["uuid"] |
| 2463 | |
| 2464 | db_sce_classifier = { |
| 2465 | "uuid": sce_classifier_uuid, |
| 2466 | "name": get_str(classifier, "name", 255), |
| 2467 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2468 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2469 | "interface_id": interface_uuid, |
| 2470 | } |
| 2471 | rsp_id = get_str(classifier, "rsp-id-ref", 255) |
| 2472 | rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None) |
| 2473 | db_sce_classifier["sce_rsp_id"] = rsp["uuid"] |
| 2474 | db_sce_classifiers.append(db_sce_classifier) |
| 2475 | |
| 2476 | db_sce_classifier_matches = [] |
| 2477 | for match in classifier.get("match-attributes").itervalues(): |
| 2478 | sce_classifier_match_uuid = str(uuid4()) |
| 2479 | uuid_list.append(sce_classifier_match_uuid) |
| 2480 | db_sce_classifier_match = { |
| 2481 | "uuid": sce_classifier_match_uuid, |
| 2482 | "ip_proto": get_str(match, "ip-proto", 2), |
| 2483 | "source_ip": get_str(match, "source-ip-address", 16), |
| 2484 | "destination_ip": get_str(match, "destination-ip-address", 16), |
| 2485 | "source_port": get_str(match, "source-port", 5), |
| 2486 | "destination_port": get_str(match, "destination-port", 5), |
| 2487 | "sce_classifier_id": sce_classifier_uuid, |
| 2488 | } |
| 2489 | db_sce_classifier_matches.append(db_sce_classifier_match) |
| 2490 | # TODO: vnf/cp keys |
| 2491 | |
| 2492 | # remove unneeded id's in sce_rsps |
| 2493 | for rsp in db_sce_rsps: |
| 2494 | rsp.pop('id') |
| 2495 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2496 | db_tables = [ |
| 2497 | {"scenarios": db_scenarios}, |
| 2498 | {"sce_nets": db_sce_nets}, |
| 2499 | {"ip_profiles": db_ip_profiles}, |
| 2500 | {"sce_vnfs": db_sce_vnfs}, |
| 2501 | {"sce_interfaces": db_sce_interfaces}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2502 | {"sce_vnffgs": db_sce_vnffgs}, |
| 2503 | {"sce_rsps": db_sce_rsps}, |
| 2504 | {"sce_rsp_hops": db_sce_rsp_hops}, |
| 2505 | {"sce_classifiers": db_sce_classifiers}, |
| 2506 | {"sce_classifier_matches": db_sce_classifier_matches}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2507 | ] |
| 2508 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2509 | logger.debug("new_nsd_v3 done: %s", |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2510 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 2511 | mydb.new_rows(db_tables, uuid_list) |
| 2512 | return nsd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2513 | except NfvoException: |
| 2514 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2515 | except Exception as e: |
| 2516 | logger.error("Exception {}".format(e)) |
| 2517 | raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request) |
| 2518 | |
| 2519 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2520 | def edit_scenario(mydb, tenant_id, scenario_id, data): |
| 2521 | data["uuid"] = scenario_id |
| 2522 | data["tenant_id"] = tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2523 | c = mydb.edit_scenario( data ) |
| 2524 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2525 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2526 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 2527 | @deprecated("Use create_instance") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2528 | 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] | 2529 | #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] | 2530 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant) |
| 2531 | vims = {datacenter_id: myvim} |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2532 | myvim_tenant = myvim['tenant_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2533 | datacenter_name = myvim['name'] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2534 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2535 | rollbackList=[] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2536 | try: |
| 2537 | #print "Checking that the scenario_id exists and getting the scenario dictionary" |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2538 | scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2539 | scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] } |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2540 | scenarioDict['datacenter_id'] = datacenter_id |
| 2541 | #print '================scenarioDict=======================' |
| 2542 | #print json.dumps(scenarioDict, indent=4) |
| 2543 | #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] | 2544 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2545 | logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs'])) |
| 2546 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2547 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2548 | auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id |
| 2549 | auxNetDict['scenario'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2550 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2551 | logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM") |
| 2552 | for sce_net in scenarioDict['nets']: |
| 2553 | #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2554 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2555 | myNetName = "%s.%s" % (instance_scenario_name, sce_net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2556 | myNetName = myNetName[0:255] #limit length |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2557 | myNetType = sce_net['type'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2558 | myNetDict = {} |
| 2559 | myNetDict["name"] = myNetName |
| 2560 | myNetDict["type"] = myNetType |
| 2561 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2562 | myNetIPProfile = sce_net.get('ip_profile', None) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2563 | #TODO: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2564 | #We should use the dictionary as input parameter for new_network |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2565 | #print myNetDict |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2566 | if not sce_net["external"]: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2567 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2568 | #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id) |
| 2569 | sce_net['vim_id'] = network_id |
| 2570 | auxNetDict['scenario'][sce_net['uuid']] = network_id |
| 2571 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2572 | sce_net["created"] = True |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2573 | else: |
| 2574 | if sce_net['vim_id'] == None: |
| 2575 | error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name']) |
| 2576 | _, message = rollback(mydb, vims, rollbackList) |
| 2577 | logger.error("nfvo.start_scenario: %s", error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2578 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2579 | logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id']) |
| 2580 | auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2581 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2582 | logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM") |
| 2583 | #For each vnf net, we create it and we add it to instanceNetlist. |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2584 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2585 | for sce_vnf in scenarioDict['vnfs']: |
| 2586 | for net in sce_vnf['nets']: |
| 2587 | #print "Net name: %s. Description: %s" % (net["name"], net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2588 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2589 | myNetName = "%s.%s" % (instance_scenario_name,net['name']) |
| 2590 | myNetName = myNetName[0:255] #limit length |
| 2591 | myNetType = net['type'] |
| 2592 | myNetDict = {} |
| 2593 | myNetDict["name"] = myNetName |
| 2594 | myNetDict["type"] = myNetType |
| 2595 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2596 | myNetIPProfile = net.get('ip_profile', None) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2597 | #print myNetDict |
| 2598 | #TODO: |
| 2599 | #We should use the dictionary as input parameter for new_network |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2600 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2601 | #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id) |
| 2602 | net['vim_id'] = network_id |
| 2603 | if sce_vnf['uuid'] not in auxNetDict: |
| 2604 | auxNetDict[sce_vnf['uuid']] = {} |
| 2605 | auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id |
| 2606 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2607 | net["created"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2608 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2609 | #print "auxNetDict:" |
| 2610 | #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2611 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2612 | logger.debug("start_scenario 3. Creating new vm instances in the VIM") |
| 2613 | #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 2614 | i = 0 |
| 2615 | for sce_vnf in scenarioDict['vnfs']: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2616 | vnf_availability_zones = [] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2617 | for vm in sce_vnf['vms']: |
| 2618 | vm_av = vm.get('availability_zone') |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2619 | if vm_av and vm_av not in vnf_availability_zones: |
| 2620 | vnf_availability_zones.append(vm_av) |
| 2621 | |
| 2622 | # check if there is enough availability zones available at vim level. |
| 2623 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 2624 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| 2625 | raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request) |
| 2626 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2627 | for vm in sce_vnf['vms']: |
| 2628 | i += 1 |
| 2629 | myVMDict = {} |
| 2630 | #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name']) |
| tierno | ae65a48 | 2016-11-24 16:20:05 +0100 | [diff] [blame] | 2631 | myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2632 | #myVMDict['description'] = vm['description'] |
| 2633 | myVMDict['description'] = myVMDict['name'][0:99] |
| 2634 | if not startvms: |
| 2635 | myVMDict['start'] = "no" |
| 2636 | myVMDict['name'] = myVMDict['name'][0:255] #limit name length |
| 2637 | #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2638 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2639 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2640 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2641 | image_id = create_or_use_image(mydb, vims, image_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2642 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2643 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2644 | #create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2645 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2646 | if flavor_dict['extended']!=None: |
| 2647 | flavor_dict['extended']= yaml.load(flavor_dict['extended']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2648 | flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2649 | vm['vim_flavor_id'] = flavor_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2650 | |
| 2651 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2652 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 2653 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 2654 | myVMDict['networks'] = [] |
| 2655 | for iface in vm['interfaces']: |
| 2656 | netDict = {} |
| 2657 | if iface['type']=="data": |
| 2658 | netDict['type'] = iface['model'] |
| 2659 | elif "model" in iface and iface["model"]!=None: |
| 2660 | netDict['model']=iface['model'] |
| 2661 | #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model |
| 2662 | #discover type of interface looking at flavor |
| 2663 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 2664 | for flavor_iface in numa.get('interfaces',[]): |
| 2665 | if flavor_iface.get('name') == iface['internal_name']: |
| 2666 | if flavor_iface['dedicated'] == 'yes': |
| 2667 | netDict['type']="PF" #passthrough |
| 2668 | elif flavor_iface['dedicated'] == 'no': |
| 2669 | netDict['type']="VF" #siov |
| 2670 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 2671 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 2672 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 2673 | break; |
| 2674 | netDict["use"]=iface['type'] |
| 2675 | if netDict["use"]=="data" and not netDict.get("type"): |
| 2676 | #print "netDict", netDict |
| 2677 | #print "iface", iface |
| 2678 | 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']) |
| 2679 | if flavor_dict.get('extended')==None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2680 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 2681 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2682 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2683 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2684 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 2685 | netDict["type"]="virtual" |
| 2686 | if "vpci" in iface and iface["vpci"] is not None: |
| 2687 | netDict['vpci'] = iface['vpci'] |
| 2688 | if "mac" in iface and iface["mac"] is not None: |
| 2689 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 2690 | if "port-security" in iface and iface["port-security"] is not None: |
| 2691 | netDict['port_security'] = iface['port-security'] |
| 2692 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 2693 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2694 | netDict['name'] = iface['internal_name'] |
| 2695 | if iface['net_id'] is None: |
| 2696 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2697 | #print iface |
| 2698 | #print vnf_iface |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2699 | if vnf_iface['interface_id']==iface['uuid']: |
| 2700 | netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ] |
| 2701 | break |
| 2702 | else: |
| 2703 | netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| 2704 | #skip bridge ifaces not connected to any net |
| 2705 | #if 'net_id' not in netDict or netDict['net_id']==None: |
| 2706 | # continue |
| 2707 | myVMDict['networks'].append(netDict) |
| 2708 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 2709 | #print myVMDict['name'] |
| 2710 | #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 2711 | #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 2712 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2713 | |
| 2714 | if 'availability_zone' in myVMDict: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2715 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2716 | else: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2717 | av_index = None |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2718 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2719 | vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None), |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2720 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2721 | availability_zone_index=av_index, |
| 2722 | availability_zone_list=vnf_availability_zones) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2723 | #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id) |
| 2724 | vm['vim_id'] = vm_id |
| 2725 | rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id}) |
| 2726 | #put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 2727 | for net in myVMDict['networks']: |
| 2728 | if "vim_id" in net: |
| 2729 | for iface in vm['interfaces']: |
| 2730 | if net["name"]==iface["internal_name"]: |
| 2731 | iface["vim_id"]=net["vim_id"] |
| 2732 | break |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2733 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2734 | logger.debug("start scenario Deployment done") |
| 2735 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| 2736 | #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] | 2737 | instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict) |
| 2738 | return mydb.get_instance_scenario(instance_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2739 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2740 | except (db_base_Exception, vimconn.vimconnException) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2741 | _, message = rollback(mydb, vims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2742 | if isinstance(e, db_base_Exception): |
| 2743 | error_text = "Exception at database" |
| 2744 | else: |
| 2745 | error_text = "Exception at VIM" |
| 2746 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 2747 | #logger.error("start_scenario %s", error_text) |
| 2748 | raise NfvoException(error_text, e.http_code) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2749 | |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2750 | def unify_cloud_config(cloud_config_preserve, cloud_config): |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2751 | """ join the cloud config information into cloud_config_preserve. |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2752 | In case of conflict cloud_config_preserve preserves |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2753 | None is allowed |
| 2754 | """ |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2755 | if not cloud_config_preserve and not cloud_config: |
| 2756 | return None |
| 2757 | |
| 2758 | new_cloud_config = {"key-pairs":[], "users":[]} |
| 2759 | # key-pairs |
| 2760 | if cloud_config_preserve: |
| 2761 | for key in cloud_config_preserve.get("key-pairs", () ): |
| 2762 | if key not in new_cloud_config["key-pairs"]: |
| 2763 | new_cloud_config["key-pairs"].append(key) |
| 2764 | if cloud_config: |
| 2765 | for key in cloud_config.get("key-pairs", () ): |
| 2766 | if key not in new_cloud_config["key-pairs"]: |
| 2767 | new_cloud_config["key-pairs"].append(key) |
| 2768 | if not new_cloud_config["key-pairs"]: |
| 2769 | del new_cloud_config["key-pairs"] |
| 2770 | |
| 2771 | # users |
| 2772 | if cloud_config: |
| 2773 | new_cloud_config["users"] += cloud_config.get("users", () ) |
| 2774 | if cloud_config_preserve: |
| 2775 | new_cloud_config["users"] += cloud_config_preserve.get("users", () ) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2776 | index_to_delete = [] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2777 | users = new_cloud_config.get("users", []) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2778 | for index0 in range(0,len(users)): |
| 2779 | if index0 in index_to_delete: |
| 2780 | continue |
| 2781 | for index1 in range(index0+1,len(users)): |
| 2782 | if index1 in index_to_delete: |
| 2783 | continue |
| 2784 | if users[index0]["name"] == users[index1]["name"]: |
| 2785 | index_to_delete.append(index1) |
| 2786 | for key in users[index1].get("key-pairs",()): |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2787 | if "key-pairs" not in users[index0]: |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2788 | users[index0]["key-pairs"] = [key] |
| 2789 | elif key not in users[index0]["key-pairs"]: |
| 2790 | users[index0]["key-pairs"].append(key) |
| 2791 | index_to_delete.sort(reverse=True) |
| 2792 | for index in index_to_delete: |
| 2793 | del users[index] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2794 | if not new_cloud_config["users"]: |
| 2795 | del new_cloud_config["users"] |
| 2796 | |
| 2797 | #boot-data-drive |
| 2798 | if cloud_config and cloud_config.get("boot-data-drive") != None: |
| 2799 | new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"] |
| 2800 | if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None: |
| 2801 | new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"] |
| 2802 | |
| 2803 | # user-data |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2804 | new_cloud_config["user-data"] = [] |
| 2805 | if cloud_config and cloud_config.get("user-data"): |
| 2806 | if isinstance(cloud_config["user-data"], list): |
| 2807 | new_cloud_config["user-data"] += cloud_config["user-data"] |
| 2808 | else: |
| 2809 | new_cloud_config["user-data"].append(cloud_config["user-data"]) |
| 2810 | if cloud_config_preserve and cloud_config_preserve.get("user-data"): |
| 2811 | if isinstance(cloud_config_preserve["user-data"], list): |
| 2812 | new_cloud_config["user-data"] += cloud_config_preserve["user-data"] |
| 2813 | else: |
| 2814 | new_cloud_config["user-data"].append(cloud_config_preserve["user-data"]) |
| 2815 | if not new_cloud_config["user-data"]: |
| 2816 | del new_cloud_config["user-data"] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2817 | |
| 2818 | # config files |
| 2819 | new_cloud_config["config-files"] = [] |
| 2820 | if cloud_config and cloud_config.get("config-files") != None: |
| 2821 | new_cloud_config["config-files"] += cloud_config["config-files"] |
| 2822 | if cloud_config_preserve: |
| 2823 | for file in cloud_config_preserve.get("config-files", ()): |
| 2824 | for index in range(0, len(new_cloud_config["config-files"])): |
| 2825 | if new_cloud_config["config-files"][index]["dest"] == file["dest"]: |
| 2826 | new_cloud_config["config-files"][index] = file |
| 2827 | break |
| 2828 | else: |
| 2829 | new_cloud_config["config-files"].append(file) |
| 2830 | if not new_cloud_config["config-files"]: |
| 2831 | del new_cloud_config["config-files"] |
| 2832 | return new_cloud_config |
| 2833 | |
| 2834 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2835 | 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] | 2836 | datacenter_id = None |
| 2837 | datacenter_name = None |
| 2838 | thread = None |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2839 | try: |
| 2840 | if datacenter_tenant_id: |
| 2841 | thread_id = datacenter_tenant_id |
| 2842 | thread = vim_threads["running"].get(datacenter_tenant_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2843 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2844 | where_={"td.nfvo_tenant_id": tenant_id} |
| 2845 | if datacenter_id_name: |
| 2846 | if utils.check_valid_uuid(datacenter_id_name): |
| 2847 | datacenter_id = datacenter_id_name |
| 2848 | where_["dt.datacenter_id"] = datacenter_id |
| 2849 | else: |
| 2850 | datacenter_name = datacenter_id_name |
| 2851 | where_["d.name"] = datacenter_name |
| 2852 | if datacenter_tenant_id: |
| 2853 | where_["dt.uuid"] = datacenter_tenant_id |
| 2854 | datacenters = mydb.get_rows( |
| 2855 | SELECT=("dt.uuid as datacenter_tenant_id",), |
| 2856 | FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id " |
| 2857 | "join datacenters as d on d.uuid=dt.datacenter_id", |
| 2858 | WHERE=where_) |
| 2859 | if len(datacenters) > 1: |
| 2860 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 2861 | elif datacenters: |
| 2862 | thread_id = datacenters[0]["datacenter_tenant_id"] |
| 2863 | thread = vim_threads["running"].get(thread_id) |
| 2864 | if not thread: |
| 2865 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2866 | return thread_id, thread |
| 2867 | except db_base_Exception as e: |
| 2868 | raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2869 | |
| tierno | f575596 | 2017-07-13 15:44:34 +0200 | [diff] [blame] | 2870 | |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2871 | def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name): |
| 2872 | WHERE_dict={} |
| 2873 | if utils.check_valid_uuid(datacenter_id_name): |
| 2874 | WHERE_dict['d.uuid'] = datacenter_id_name |
| 2875 | else: |
| 2876 | WHERE_dict['d.name'] = datacenter_id_name |
| 2877 | |
| 2878 | if tenant_id: |
| 2879 | WHERE_dict['nfvo_tenant_id'] = tenant_id |
| 2880 | from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \ |
| 2881 | " dt on td.datacenter_tenant_id=dt.uuid" |
| 2882 | else: |
| 2883 | from_ = 'datacenters as d' |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 2884 | 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] | 2885 | if len(vimaccounts) == 0: |
| 2886 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2887 | elif len(vimaccounts)>1: |
| 2888 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 2889 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 2890 | return vimaccounts[0]["uuid"], vimaccounts[0]["name"] |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2891 | |
| 2892 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2893 | 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] | 2894 | datacenter_id = None |
| 2895 | datacenter_name = None |
| 2896 | if datacenter_id_name: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2897 | if utils.check_valid_uuid(datacenter_id_name): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2898 | datacenter_id = datacenter_id_name |
| 2899 | else: |
| 2900 | datacenter_name = datacenter_id_name |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2901 | vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2902 | if len(vims) == 0: |
| 2903 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2904 | elif len(vims)>1: |
| 2905 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 2906 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 2907 | return vims.keys()[0], vims.values()[0] |
| 2908 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2909 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2910 | def update(d, u): |
| 2911 | '''Takes dict d and updates it with the values in dict u.''' |
| 2912 | '''It merges all depth levels''' |
| 2913 | for k, v in u.iteritems(): |
| 2914 | if isinstance(v, collections.Mapping): |
| 2915 | r = update(d.get(k, {}), v) |
| 2916 | d[k] = r |
| 2917 | else: |
| 2918 | d[k] = u[k] |
| 2919 | return d |
| 2920 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2921 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2922 | def create_instance(mydb, tenant_id, instance_dict): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2923 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| 2924 | # logger.debug("Creating instance...") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2925 | scenario = instance_dict["scenario"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2926 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2927 | # find main datacenter |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2928 | myvims = {} |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2929 | myvim_threads_id = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2930 | datacenter = instance_dict.get("datacenter") |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2931 | default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 2932 | myvims[default_datacenter_id] = vim |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2933 | 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] | 2934 | tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2935 | # myvim_tenant = myvim['tenant_id'] |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2936 | rollbackList = [] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2937 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2938 | # print "Checking that the scenario exists and getting the scenario dictionary" |
| 2939 | scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id], |
| 2940 | datacenter_id=default_datacenter_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2941 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2942 | # logger.debug(">>>>>> Dictionaries before merging") |
| 2943 | # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256))) |
| 2944 | # 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] | 2945 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2946 | db_instance_vnfs = [] |
| 2947 | db_instance_vms = [] |
| 2948 | db_instance_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2949 | db_instance_sfis = [] |
| 2950 | db_instance_sfs = [] |
| 2951 | db_instance_classifications = [] |
| 2952 | db_instance_sfps = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2953 | db_ip_profiles = [] |
| 2954 | db_vim_actions = [] |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2955 | uuid_list = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2956 | task_index = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2957 | instance_name = instance_dict["name"] |
| 2958 | instance_uuid = str(uuid4()) |
| 2959 | uuid_list.append(instance_uuid) |
| 2960 | db_instance_scenario = { |
| 2961 | "uuid": instance_uuid, |
| 2962 | "name": instance_name, |
| 2963 | "tenant_id": tenant_id, |
| 2964 | "scenario_id": scenarioDict['uuid'], |
| 2965 | "datacenter_id": default_datacenter_id, |
| 2966 | # filled bellow 'datacenter_tenant_id' |
| 2967 | "description": instance_dict.get("description"), |
| 2968 | } |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2969 | if scenarioDict.get("cloud-config"): |
| 2970 | db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], |
| 2971 | default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2972 | instance_action_id = get_task_id() |
| 2973 | db_instance_action = { |
| 2974 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 2975 | "tenant_id": tenant_id, |
| 2976 | "instance_id": instance_uuid, |
| 2977 | "description": "CREATE", |
| 2978 | } |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2979 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2980 | # Auxiliary dictionaries from x to y |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2981 | sce_net2instance = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2982 | net2task_id = {'scenario': {}} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2983 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 2984 | def ip_profile_IM2RO(ip_profile_im): |
| 2985 | # translate from input format to database format |
| 2986 | ip_profile_ro = {} |
| 2987 | if 'subnet-address' in ip_profile_im: |
| 2988 | ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address'] |
| 2989 | if 'ip-version' in ip_profile_im: |
| 2990 | ip_profile_ro['ip_version'] = ip_profile_im['ip-version'] |
| 2991 | if 'gateway-address' in ip_profile_im: |
| 2992 | ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address'] |
| 2993 | if 'dns-address' in ip_profile_im: |
| 2994 | ip_profile_ro['dns_address'] = ip_profile_im['dns-address'] |
| 2995 | if isinstance(ip_profile_ro['dns_address'], (list, tuple)): |
| 2996 | ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address']) |
| 2997 | if 'dhcp' in ip_profile_im: |
| 2998 | ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address') |
| 2999 | ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True) |
| 3000 | ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count') |
| 3001 | return ip_profile_ro |
| 3002 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3003 | # logger.debug("Creating instance from scenario-dict:\n%s", |
| 3004 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3005 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3006 | # 0 check correct parameters |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3007 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3008 | for scenario_net in scenarioDict['nets']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3009 | 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] | 3010 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3011 | else: |
| 3012 | raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name), |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3013 | HTTP_Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3014 | if "sites" not in net_instance_desc: |
| 3015 | net_instance_desc["sites"] = [ {} ] |
| 3016 | site_without_datacenter_field = False |
| 3017 | for site in net_instance_desc["sites"]: |
| 3018 | if site.get("datacenter"): |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3019 | site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"]) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3020 | if site["datacenter"] not in myvims: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3021 | # Add this datacenter to myvims |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3022 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"]) |
| 3023 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3024 | myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"]) |
| 3025 | site["datacenter"] = d # change name to id |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3026 | else: |
| 3027 | if site_without_datacenter_field: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3028 | raise NfvoException("Found more than one entries without datacenter field at " |
| 3029 | "instance:networks:{}:sites".format(net_name), HTTP_Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3030 | site_without_datacenter_field = True |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3031 | site["datacenter"] = default_datacenter_id # change name to id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3032 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3033 | for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3034 | for scenario_vnf in scenarioDict['vnfs']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3035 | 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] | 3036 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3037 | else: |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 3038 | raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), HTTP_Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3039 | if "datacenter" in vnf_instance_desc: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3040 | # Add this datacenter to myvims |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3041 | 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] | 3042 | if vnf_instance_desc["datacenter"] not in myvims: |
| 3043 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| 3044 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3045 | 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] | 3046 | scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"] |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 3047 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3048 | for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems(): |
| 3049 | for scenario_net in scenario_vnf['nets']: |
| 3050 | if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]: |
| 3051 | break |
| 3052 | else: |
| 3053 | raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), HTTP_Bad_Request) |
| 3054 | if net_instance_desc.get("vim-network-name"): |
| 3055 | scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"] |
| 3056 | if net_instance_desc.get("name"): |
| 3057 | scenario_net["name"] = net_instance_desc["name"] |
| 3058 | if 'ip-profile' in net_instance_desc: |
| 3059 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| 3060 | if 'ip_profile' not in scenario_net: |
| 3061 | scenario_net['ip_profile'] = ipprofile_db |
| 3062 | else: |
| 3063 | update(scenario_net['ip_profile'], ipprofile_db) |
| 3064 | |
| 3065 | for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems(): |
| 3066 | for scenario_vm in scenario_vnf['vms']: |
| 3067 | if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]: |
| 3068 | break |
| 3069 | else: |
| 3070 | raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), HTTP_Bad_Request) |
| 3071 | scenario_vm["instance_parameters"] = vdu_instance_desc |
| 3072 | for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems(): |
| 3073 | for scenario_interface in scenario_vm['interfaces']: |
| 3074 | if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]: |
| 3075 | scenario_interface.update(iface_instance_desc) |
| 3076 | break |
| 3077 | else: |
| 3078 | raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), HTTP_Bad_Request) |
| 3079 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3080 | # 0.1 parse cloud-config parameters |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 3081 | 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] | 3082 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3083 | # 0.2 merge instance information into scenario |
| 3084 | # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict) |
| 3085 | # However, this is not possible yet. |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3086 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3087 | for scenario_net in scenarioDict['nets']: |
| 3088 | if net_name == scenario_net["name"]: |
| 3089 | if 'ip-profile' in net_instance_desc: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3090 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3091 | if 'ip_profile' not in scenario_net: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3092 | scenario_net['ip_profile'] = ipprofile_db |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3093 | else: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3094 | update(scenario_net['ip_profile'], ipprofile_db) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3095 | for interface in net_instance_desc.get('interfaces', ()): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3096 | if 'ip_address' in interface: |
| 3097 | for vnf in scenarioDict['vnfs']: |
| 3098 | if interface['vnf'] == vnf['name']: |
| 3099 | for vnf_interface in vnf['interfaces']: |
| 3100 | if interface['vnf_interface'] == vnf_interface['external_name']: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3101 | vnf_interface['ip_address'] = interface['ip_address'] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3102 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3103 | # logger.debug(">>>>>>>> Merged dictionary") |
| 3104 | # logger.debug("Creating instance scenario-dict MERGED:\n%s", |
| 3105 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3106 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3107 | # 1. Creating new nets (sce_nets) in the VIM" |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3108 | number_mgmt_networks = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3109 | db_instance_nets = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3110 | for sce_net in scenarioDict['nets']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3111 | # get involved datacenters where this network need to be created |
| 3112 | involved_datacenters = [] |
| 3113 | for sce_vnf in scenarioDict.get("vnfs"): |
| 3114 | vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id) |
| 3115 | if vnf_datacenter in involved_datacenters: |
| 3116 | continue |
| 3117 | if sce_vnf.get("interfaces"): |
| 3118 | for sce_vnf_ifaces in sce_vnf["interfaces"]: |
| 3119 | if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]: |
| 3120 | involved_datacenters.append(vnf_datacenter) |
| 3121 | break |
| 3122 | |
| 3123 | descriptor_net = {} |
| 3124 | if instance_dict.get("networks") and instance_dict["networks"].get(sce_net["name"]): |
| 3125 | descriptor_net = instance_dict["networks"][sce_net["name"]] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3126 | net_name = descriptor_net.get("vim-network-name") |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3127 | sce_net2instance[sce_net['uuid']] = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3128 | net2task_id['scenario'][sce_net['uuid']] = {} |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3129 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3130 | if sce_net["external"]: |
| 3131 | number_mgmt_networks += 1 |
| 3132 | |
| 3133 | for datacenter_id in involved_datacenters: |
| 3134 | netmap_use = None |
| 3135 | netmap_create = None |
| 3136 | if descriptor_net.get("sites"): |
| 3137 | for site in descriptor_net["sites"]: |
| 3138 | if site.get("datacenter") == datacenter_id: |
| 3139 | netmap_use = site.get("netmap-use") |
| 3140 | netmap_create = site.get("netmap-create") |
| 3141 | break |
| 3142 | |
| 3143 | vim = myvims[datacenter_id] |
| 3144 | myvim_thread_id = myvim_threads_id[datacenter_id] |
| 3145 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3146 | net_type = sce_net['type'] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3147 | lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3148 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3149 | if not net_name: |
| 3150 | if sce_net["external"]: |
| 3151 | net_name = sce_net["name"] |
| 3152 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3153 | net_name = "{}-{}".format(instance_name, sce_net["name"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3154 | net_name = net_name[:255] # limit length |
| 3155 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3156 | if netmap_use or netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3157 | create_network = False |
| 3158 | lookfor_network = False |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3159 | if netmap_use: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3160 | lookfor_network = True |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3161 | if utils.check_valid_uuid(netmap_use): |
| 3162 | lookfor_filter["id"] = netmap_use |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3163 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3164 | lookfor_filter["name"] = netmap_use |
| 3165 | if netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3166 | create_network = True |
| 3167 | net_vim_name = net_name |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3168 | if isinstance(netmap_create, str): |
| 3169 | net_vim_name = netmap_create |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3170 | elif sce_net.get("vim_network_name"): |
| 3171 | create_network = False |
| 3172 | lookfor_network = True |
| 3173 | lookfor_filter["name"] = sce_net.get("vim_network_name") |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3174 | elif sce_net["external"]: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3175 | if sce_net['vim_id'] is not None: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3176 | # there is a netmap at datacenter_nets database # TODO REVISE!!!! |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3177 | create_network = False |
| 3178 | lookfor_network = True |
| 3179 | lookfor_filter["id"] = sce_net['vim_id'] |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3180 | elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"): |
| 3181 | if number_mgmt_networks > 1: |
| 3182 | raise NfvoException("Found several VLD of type mgmt. " |
| 3183 | "You must concrete what vim-network must be use for each one", |
| 3184 | HTTP_Bad_Request) |
| 3185 | create_network = False |
| 3186 | lookfor_network = True |
| 3187 | if vim["config"].get("management_network_id"): |
| 3188 | lookfor_filter["id"] = vim["config"]["management_network_id"] |
| 3189 | else: |
| 3190 | lookfor_filter["name"] = vim["config"]["management_network_name"] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3191 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3192 | # 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] | 3193 | create_network = True |
| 3194 | lookfor_network = True |
| 3195 | lookfor_filter["name"] = sce_net["name"] |
| 3196 | net_vim_name = sce_net["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3197 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3198 | net_vim_name = net_name |
| 3199 | create_network = True |
| 3200 | lookfor_network = False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3201 | |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3202 | task_extra = {} |
| 3203 | if create_network: |
| 3204 | task_action = "CREATE" |
| 3205 | task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None)) |
| 3206 | if lookfor_network: |
| 3207 | task_extra["find"] = (lookfor_filter,) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3208 | elif lookfor_network: |
| 3209 | task_action = "FIND" |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3210 | task_extra["params"] = (lookfor_filter,) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3211 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3212 | # fill database content |
| 3213 | net_uuid = str(uuid4()) |
| 3214 | uuid_list.append(net_uuid) |
| 3215 | sce_net2instance[sce_net['uuid']][datacenter_id] = net_uuid |
| 3216 | db_net = { |
| 3217 | "uuid": net_uuid, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3218 | 'vim_net_id': None, |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3219 | "instance_scenario_id": instance_uuid, |
| 3220 | "sce_net_id": sce_net["uuid"], |
| 3221 | "created": create_network, |
| 3222 | 'datacenter_id': datacenter_id, |
| 3223 | 'datacenter_tenant_id': myvim_thread_id, |
| tierno | d2836fc | 2018-05-30 15:03:27 +0200 | [diff] [blame] | 3224 | 'status': 'BUILD' # if create_network else "ACTIVE" |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3225 | } |
| 3226 | db_instance_nets.append(db_net) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3227 | db_vim_action = { |
| 3228 | "instance_action_id": instance_action_id, |
| 3229 | "status": "SCHEDULED", |
| 3230 | "task_index": task_index, |
| 3231 | "datacenter_vim_id": myvim_thread_id, |
| 3232 | "action": task_action, |
| 3233 | "item": "instance_nets", |
| 3234 | "item_id": net_uuid, |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3235 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3236 | } |
| 3237 | net2task_id['scenario'][sce_net['uuid']][datacenter_id] = task_index |
| 3238 | task_index += 1 |
| 3239 | db_vim_actions.append(db_vim_action) |
| 3240 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3241 | if 'ip_profile' in sce_net: |
| 3242 | db_ip_profile={ |
| 3243 | 'instance_net_id': net_uuid, |
| 3244 | 'ip_version': sce_net['ip_profile']['ip_version'], |
| 3245 | 'subnet_address': sce_net['ip_profile']['subnet_address'], |
| 3246 | 'gateway_address': sce_net['ip_profile']['gateway_address'], |
| 3247 | 'dns_address': sce_net['ip_profile']['dns_address'], |
| 3248 | 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'], |
| 3249 | 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'], |
| 3250 | 'dhcp_count': sce_net['ip_profile']['dhcp_count'], |
| 3251 | } |
| 3252 | db_ip_profiles.append(db_ip_profile) |
| 3253 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3254 | # Create VNFs |
| 3255 | vnf_params = { |
| 3256 | "default_datacenter_id": default_datacenter_id, |
| 3257 | "myvim_threads_id": myvim_threads_id, |
| 3258 | "instance_uuid": instance_uuid, |
| 3259 | "instance_name": instance_name, |
| 3260 | "instance_action_id": instance_action_id, |
| 3261 | "myvims": myvims, |
| 3262 | "cloud_config": cloud_config, |
| 3263 | "RO_pub_key": tenant[0].get('RO_pub_key'), |
| 3264 | } |
| 3265 | vnf_params_out = { |
| 3266 | "task_index": task_index, |
| 3267 | "uuid_list": uuid_list, |
| 3268 | "db_instance_nets": db_instance_nets, |
| 3269 | "db_vim_actions": db_vim_actions, |
| 3270 | "db_ip_profiles": db_ip_profiles, |
| 3271 | "db_instance_vnfs": db_instance_vnfs, |
| 3272 | "db_instance_vms": db_instance_vms, |
| 3273 | "db_instance_interfaces": db_instance_interfaces, |
| 3274 | "net2task_id": net2task_id, |
| 3275 | "sce_net2instance": sce_net2instance, |
| 3276 | } |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 3277 | # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) |
| 3278 | for sce_vnf in scenarioDict['vnfs']: # sce_vnf_list: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3279 | instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList) |
| 3280 | task_index = vnf_params_out["task_index"] |
| 3281 | uuid_list = vnf_params_out["uuid_list"] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3282 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3283 | # Create VNFFGs |
| 3284 | # task_depends_on = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3285 | for vnffg in scenarioDict['vnffgs']: |
| 3286 | for rsp in vnffg['rsps']: |
| 3287 | sfs_created = [] |
| 3288 | for cp in rsp['connection_points']: |
| 3289 | count = mydb.get_rows( |
| 3290 | SELECT=('vms.count'), |
| 3291 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h on interfaces.uuid=h.interface_id", |
| 3292 | WHERE={'h.uuid': cp['uuid']})[0]['count'] |
| 3293 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None) |
| 3294 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3295 | dependencies = [] |
| 3296 | for instance_vm in instance_vms: |
| 3297 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3298 | if action: |
| 3299 | dependencies.append(action['task_index']) |
| 3300 | # TODO: throw exception if count != len(instance_vms) |
| 3301 | # TODO: and action shouldn't ever be None |
| 3302 | sfis_created = [] |
| 3303 | for i in range(count): |
| 3304 | # create sfis |
| 3305 | sfi_uuid = str(uuid4()) |
| 3306 | uuid_list.append(sfi_uuid) |
| 3307 | db_sfi = { |
| 3308 | "uuid": sfi_uuid, |
| 3309 | "instance_scenario_id": instance_uuid, |
| 3310 | 'sce_rsp_hop_id': cp['uuid'], |
| 3311 | 'datacenter_id': datacenter_id, |
| 3312 | 'datacenter_tenant_id': myvim_thread_id, |
| 3313 | "vim_sfi_id": None, # vim thread will populate |
| 3314 | } |
| 3315 | db_instance_sfis.append(db_sfi) |
| 3316 | db_vim_action = { |
| 3317 | "instance_action_id": instance_action_id, |
| 3318 | "task_index": task_index, |
| 3319 | "datacenter_vim_id": myvim_thread_id, |
| 3320 | "action": "CREATE", |
| 3321 | "status": "SCHEDULED", |
| 3322 | "item": "instance_sfis", |
| 3323 | "item_id": sfi_uuid, |
| 3324 | "extra": yaml.safe_dump({"params": "", "depends_on": [dependencies[i]]}, |
| 3325 | default_flow_style=True, width=256) |
| 3326 | } |
| 3327 | sfis_created.append(task_index) |
| 3328 | task_index += 1 |
| 3329 | db_vim_actions.append(db_vim_action) |
| 3330 | # create sfs |
| 3331 | sf_uuid = str(uuid4()) |
| 3332 | uuid_list.append(sf_uuid) |
| 3333 | db_sf = { |
| 3334 | "uuid": sf_uuid, |
| 3335 | "instance_scenario_id": instance_uuid, |
| 3336 | 'sce_rsp_hop_id': cp['uuid'], |
| 3337 | 'datacenter_id': datacenter_id, |
| 3338 | 'datacenter_tenant_id': myvim_thread_id, |
| 3339 | "vim_sf_id": None, # vim thread will populate |
| 3340 | } |
| 3341 | db_instance_sfs.append(db_sf) |
| 3342 | db_vim_action = { |
| 3343 | "instance_action_id": instance_action_id, |
| 3344 | "task_index": task_index, |
| 3345 | "datacenter_vim_id": myvim_thread_id, |
| 3346 | "action": "CREATE", |
| 3347 | "status": "SCHEDULED", |
| 3348 | "item": "instance_sfs", |
| 3349 | "item_id": sf_uuid, |
| 3350 | "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created}, |
| 3351 | default_flow_style=True, width=256) |
| 3352 | } |
| 3353 | sfs_created.append(task_index) |
| 3354 | task_index += 1 |
| 3355 | db_vim_actions.append(db_vim_action) |
| 3356 | classifier = rsp['classifier'] |
| 3357 | |
| 3358 | # TODO the following ~13 lines can be reused for the sfi case |
| 3359 | count = mydb.get_rows( |
| 3360 | SELECT=('vms.count'), |
| 3361 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id", |
| 3362 | WHERE={'c.uuid': classifier['uuid']})[0]['count'] |
| 3363 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None) |
| 3364 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3365 | dependencies = [] |
| 3366 | for instance_vm in instance_vms: |
| 3367 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3368 | if action: |
| 3369 | dependencies.append(action['task_index']) |
| 3370 | # TODO: throw exception if count != len(instance_vms) |
| 3371 | # TODO: and action shouldn't ever be None |
| 3372 | classifications_created = [] |
| 3373 | for i in range(count): |
| 3374 | for match in classifier['matches']: |
| 3375 | # create classifications |
| 3376 | classification_uuid = str(uuid4()) |
| 3377 | uuid_list.append(classification_uuid) |
| 3378 | db_classification = { |
| 3379 | "uuid": classification_uuid, |
| 3380 | "instance_scenario_id": instance_uuid, |
| 3381 | 'sce_classifier_match_id': match['uuid'], |
| 3382 | 'datacenter_id': datacenter_id, |
| 3383 | 'datacenter_tenant_id': myvim_thread_id, |
| 3384 | "vim_classification_id": None, # vim thread will populate |
| 3385 | } |
| 3386 | db_instance_classifications.append(db_classification) |
| 3387 | classification_params = { |
| 3388 | "ip_proto": match["ip_proto"], |
| 3389 | "source_ip": match["source_ip"], |
| 3390 | "destination_ip": match["destination_ip"], |
| 3391 | "source_port": match["source_port"], |
| 3392 | "destination_port": match["destination_port"] |
| 3393 | } |
| 3394 | db_vim_action = { |
| 3395 | "instance_action_id": instance_action_id, |
| 3396 | "task_index": task_index, |
| 3397 | "datacenter_vim_id": myvim_thread_id, |
| 3398 | "action": "CREATE", |
| 3399 | "status": "SCHEDULED", |
| 3400 | "item": "instance_classifications", |
| 3401 | "item_id": classification_uuid, |
| 3402 | "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]}, |
| 3403 | default_flow_style=True, width=256) |
| 3404 | } |
| 3405 | classifications_created.append(task_index) |
| 3406 | task_index += 1 |
| 3407 | db_vim_actions.append(db_vim_action) |
| 3408 | |
| 3409 | # create sfps |
| 3410 | sfp_uuid = str(uuid4()) |
| 3411 | uuid_list.append(sfp_uuid) |
| 3412 | db_sfp = { |
| 3413 | "uuid": sfp_uuid, |
| 3414 | "instance_scenario_id": instance_uuid, |
| 3415 | 'sce_rsp_id': rsp['uuid'], |
| 3416 | 'datacenter_id': datacenter_id, |
| 3417 | 'datacenter_tenant_id': myvim_thread_id, |
| 3418 | "vim_sfp_id": None, # vim thread will populate |
| 3419 | } |
| 3420 | db_instance_sfps.append(db_sfp) |
| 3421 | db_vim_action = { |
| 3422 | "instance_action_id": instance_action_id, |
| 3423 | "task_index": task_index, |
| 3424 | "datacenter_vim_id": myvim_thread_id, |
| 3425 | "action": "CREATE", |
| 3426 | "status": "SCHEDULED", |
| 3427 | "item": "instance_sfps", |
| 3428 | "item_id": sfp_uuid, |
| 3429 | "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created}, |
| 3430 | default_flow_style=True, width=256) |
| 3431 | } |
| 3432 | task_index += 1 |
| 3433 | db_vim_actions.append(db_vim_action) |
| 3434 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3435 | scenarioDict["datacenter2tenant"] = myvim_threads_id |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3436 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3437 | db_instance_action["number_tasks"] = task_index |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3438 | db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id] |
| 3439 | db_instance_scenario['datacenter_id'] = default_datacenter_id |
| 3440 | db_tables=[ |
| 3441 | {"instance_scenarios": db_instance_scenario}, |
| 3442 | {"instance_vnfs": db_instance_vnfs}, |
| 3443 | {"instance_nets": db_instance_nets}, |
| 3444 | {"ip_profiles": db_ip_profiles}, |
| 3445 | {"instance_vms": db_instance_vms}, |
| 3446 | {"instance_interfaces": db_instance_interfaces}, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3447 | {"instance_actions": db_instance_action}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3448 | {"instance_sfis": db_instance_sfis}, |
| 3449 | {"instance_sfs": db_instance_sfs}, |
| 3450 | {"instance_classifications": db_instance_classifications}, |
| 3451 | {"instance_sfps": db_instance_sfps}, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3452 | {"vim_actions": db_vim_actions} |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3453 | ] |
| 3454 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3455 | logger.debug("create_instance done DB tables: %s", |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3456 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 3457 | mydb.new_rows(db_tables, uuid_list) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3458 | for myvim_thread_id in myvim_threads_id.values(): |
| 3459 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3460 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3461 | returned_instance = mydb.get_instance_scenario(instance_uuid) |
| 3462 | returned_instance["action_id"] = instance_action_id |
| 3463 | return returned_instance |
| 3464 | except (NfvoException, vimconn.vimconnException, db_base_Exception) as e: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3465 | message = rollback(mydb, myvims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3466 | if isinstance(e, db_base_Exception): |
| 3467 | error_text = "database Exception" |
| 3468 | elif isinstance(e, vimconn.vimconnException): |
| 3469 | error_text = "VIM Exception" |
| 3470 | else: |
| 3471 | error_text = "Exception" |
| 3472 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3473 | # logger.error("create_instance: %s", error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3474 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3475 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3476 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3477 | def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): |
| 3478 | default_datacenter_id = params["default_datacenter_id"] |
| 3479 | myvim_threads_id = params["myvim_threads_id"] |
| 3480 | instance_uuid = params["instance_uuid"] |
| 3481 | instance_name = params["instance_name"] |
| 3482 | instance_action_id = params["instance_action_id"] |
| 3483 | myvims = params["myvims"] |
| 3484 | cloud_config = params["cloud_config"] |
| 3485 | RO_pub_key = params["RO_pub_key"] |
| 3486 | |
| 3487 | task_index = params_out["task_index"] |
| 3488 | uuid_list = params_out["uuid_list"] |
| 3489 | db_instance_nets = params_out["db_instance_nets"] |
| 3490 | db_vim_actions = params_out["db_vim_actions"] |
| 3491 | db_ip_profiles = params_out["db_ip_profiles"] |
| 3492 | db_instance_vnfs = params_out["db_instance_vnfs"] |
| 3493 | db_instance_vms = params_out["db_instance_vms"] |
| 3494 | db_instance_interfaces = params_out["db_instance_interfaces"] |
| 3495 | net2task_id = params_out["net2task_id"] |
| 3496 | sce_net2instance = params_out["sce_net2instance"] |
| 3497 | |
| 3498 | vnf_net2instance = {} |
| 3499 | |
| 3500 | # 2. Creating new nets (vnf internal nets) in the VIM" |
| 3501 | # For each vnf net, we create it and we add it to instanceNetlist. |
| 3502 | if sce_vnf.get("datacenter"): |
| 3503 | datacenter_id = sce_vnf["datacenter"] |
| 3504 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3505 | else: |
| 3506 | datacenter_id = default_datacenter_id |
| 3507 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3508 | for net in sce_vnf['nets']: |
| 3509 | # TODO revis |
| 3510 | # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {}) |
| 3511 | # net_name = descriptor_net.get("name") |
| 3512 | net_name = None |
| 3513 | if not net_name: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3514 | net_name = "{}-{}".format(instance_name, net["name"]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3515 | net_name = net_name[:255] # limit length |
| 3516 | net_type = net['type'] |
| 3517 | |
| 3518 | if sce_vnf['uuid'] not in vnf_net2instance: |
| 3519 | vnf_net2instance[sce_vnf['uuid']] = {} |
| 3520 | if sce_vnf['uuid'] not in net2task_id: |
| 3521 | net2task_id[sce_vnf['uuid']] = {} |
| 3522 | net2task_id[sce_vnf['uuid']][net['uuid']] = task_index |
| 3523 | |
| 3524 | # fill database content |
| 3525 | net_uuid = str(uuid4()) |
| 3526 | uuid_list.append(net_uuid) |
| 3527 | vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid |
| 3528 | db_net = { |
| 3529 | "uuid": net_uuid, |
| 3530 | 'vim_net_id': None, |
| 3531 | "instance_scenario_id": instance_uuid, |
| 3532 | "net_id": net["uuid"], |
| 3533 | "created": True, |
| 3534 | 'datacenter_id': datacenter_id, |
| 3535 | 'datacenter_tenant_id': myvim_thread_id, |
| 3536 | } |
| 3537 | db_instance_nets.append(db_net) |
| 3538 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3539 | if net.get("vim-network-name"): |
| 3540 | lookfor_filter = {"name": net["vim-network-name"]} |
| 3541 | task_action = "FIND" |
| 3542 | task_extra = {"params": (lookfor_filter,)} |
| 3543 | else: |
| 3544 | task_action = "CREATE" |
| 3545 | task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))} |
| 3546 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3547 | db_vim_action = { |
| 3548 | "instance_action_id": instance_action_id, |
| 3549 | "task_index": task_index, |
| 3550 | "datacenter_vim_id": myvim_thread_id, |
| 3551 | "status": "SCHEDULED", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3552 | "action": task_action, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3553 | "item": "instance_nets", |
| 3554 | "item_id": net_uuid, |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3555 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3556 | } |
| 3557 | task_index += 1 |
| 3558 | db_vim_actions.append(db_vim_action) |
| 3559 | |
| 3560 | if 'ip_profile' in net: |
| 3561 | db_ip_profile = { |
| 3562 | 'instance_net_id': net_uuid, |
| 3563 | 'ip_version': net['ip_profile']['ip_version'], |
| 3564 | 'subnet_address': net['ip_profile']['subnet_address'], |
| 3565 | 'gateway_address': net['ip_profile']['gateway_address'], |
| 3566 | 'dns_address': net['ip_profile']['dns_address'], |
| 3567 | 'dhcp_enabled': net['ip_profile']['dhcp_enabled'], |
| 3568 | 'dhcp_start_address': net['ip_profile']['dhcp_start_address'], |
| 3569 | 'dhcp_count': net['ip_profile']['dhcp_count'], |
| 3570 | } |
| 3571 | db_ip_profiles.append(db_ip_profile) |
| 3572 | |
| 3573 | # print "vnf_net2instance:" |
| 3574 | # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False) |
| 3575 | |
| 3576 | # 3. Creating new vm instances in the VIM |
| 3577 | # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 3578 | ssh_access = None |
| 3579 | if sce_vnf.get('mgmt_access'): |
| 3580 | ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access') |
| 3581 | vnf_availability_zones = [] |
| 3582 | for vm in sce_vnf['vms']: |
| 3583 | vm_av = vm.get('availability_zone') |
| 3584 | if vm_av and vm_av not in vnf_availability_zones: |
| 3585 | vnf_availability_zones.append(vm_av) |
| 3586 | |
| 3587 | # check if there is enough availability zones available at vim level. |
| 3588 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 3589 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| 3590 | raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request) |
| 3591 | |
| 3592 | if sce_vnf.get("datacenter"): |
| 3593 | vim = myvims[sce_vnf["datacenter"]] |
| 3594 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3595 | datacenter_id = sce_vnf["datacenter"] |
| 3596 | else: |
| 3597 | vim = myvims[default_datacenter_id] |
| 3598 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3599 | datacenter_id = default_datacenter_id |
| 3600 | sce_vnf["datacenter_id"] = datacenter_id |
| 3601 | i = 0 |
| 3602 | |
| 3603 | vnf_uuid = str(uuid4()) |
| 3604 | uuid_list.append(vnf_uuid) |
| 3605 | db_instance_vnf = { |
| 3606 | 'uuid': vnf_uuid, |
| 3607 | 'instance_scenario_id': instance_uuid, |
| 3608 | 'vnf_id': sce_vnf['vnf_id'], |
| 3609 | 'sce_vnf_id': sce_vnf['uuid'], |
| 3610 | 'datacenter_id': datacenter_id, |
| 3611 | 'datacenter_tenant_id': myvim_thread_id, |
| 3612 | } |
| 3613 | db_instance_vnfs.append(db_instance_vnf) |
| 3614 | |
| 3615 | for vm in sce_vnf['vms']: |
| 3616 | myVMDict = {} |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 3617 | sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name'] |
| 3618 | myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3619 | myVMDict['description'] = myVMDict['name'][0:99] |
| 3620 | # if not startvms: |
| 3621 | # myVMDict['start'] = "no" |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3622 | if vm.get("instance_parameters") and vm["instance_parameters"].get("name"): |
| 3623 | myVMDict['name'] = vm["instance_parameters"].get("name") |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3624 | myVMDict['name'] = myVMDict['name'][0:255] # limit name length |
| 3625 | # create image at vim in case it not exist |
| 3626 | image_uuid = vm['image_id'] |
| 3627 | if vm.get("image_list"): |
| 3628 | for alternative_image in vm["image_list"]: |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 3629 | if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3630 | image_uuid = alternative_image['image_id'] |
| 3631 | break |
| 3632 | image_dict = mydb.get_table_by_uuid_name("images", image_uuid) |
| 3633 | image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True) |
| 3634 | vm['vim_image_id'] = image_id |
| 3635 | |
| 3636 | # create flavor at vim in case it not exist |
| 3637 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| 3638 | if flavor_dict['extended'] != None: |
| 3639 | flavor_dict['extended'] = yaml.load(flavor_dict['extended']) |
| 3640 | flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True) |
| 3641 | |
| 3642 | # Obtain information for additional disks |
| 3643 | extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), |
| 3644 | WHERE={'vim_id': flavor_id}) |
| 3645 | if not extended_flavor_dict: |
| 3646 | raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3647 | |
| 3648 | # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0]) |
| 3649 | myVMDict['disks'] = None |
| 3650 | extended_info = extended_flavor_dict[0]['extended'] |
| 3651 | if extended_info != None: |
| 3652 | extended_flavor_dict_yaml = yaml.load(extended_info) |
| 3653 | if 'disks' in extended_flavor_dict_yaml: |
| 3654 | myVMDict['disks'] = extended_flavor_dict_yaml['disks'] |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3655 | if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"): |
| 3656 | for disk in myVMDict['disks']: |
| 3657 | if disk.get("name") in vm["instance_parameters"]["devices"]: |
| 3658 | disk.update(vm["instance_parameters"]["devices"][disk.get("name")]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3659 | |
| 3660 | vm['vim_flavor_id'] = flavor_id |
| 3661 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 3662 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 3663 | myVMDict['availability_zone'] = vm.get('availability_zone') |
| 3664 | myVMDict['networks'] = [] |
| 3665 | task_depends_on = [] |
| 3666 | # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true |
| 3667 | db_vm_ifaces = [] |
| 3668 | for iface in vm['interfaces']: |
| 3669 | netDict = {} |
| 3670 | if iface['type'] == "data": |
| 3671 | netDict['type'] = iface['model'] |
| 3672 | elif "model" in iface and iface["model"] != None: |
| 3673 | netDict['model'] = iface['model'] |
| 3674 | # TODO in future, remove this because mac_address will not be set, and the type of PV,VF |
| 3675 | # is obtained from iterface table model |
| 3676 | # discover type of interface looking at flavor |
| 3677 | for numa in flavor_dict.get('extended', {}).get('numas', []): |
| 3678 | for flavor_iface in numa.get('interfaces', []): |
| 3679 | if flavor_iface.get('name') == iface['internal_name']: |
| 3680 | if flavor_iface['dedicated'] == 'yes': |
| 3681 | netDict['type'] = "PF" # passthrough |
| 3682 | elif flavor_iface['dedicated'] == 'no': |
| 3683 | netDict['type'] = "VF" # siov |
| 3684 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 3685 | netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF |
| 3686 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 3687 | break |
| 3688 | netDict["use"] = iface['type'] |
| 3689 | if netDict["use"] == "data" and not netDict.get("type"): |
| 3690 | # print "netDict", netDict |
| 3691 | # print "iface", iface |
| 3692 | e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat( |
| 3693 | sce_vnf['name'], vm['name'], iface['internal_name']) |
| 3694 | if flavor_dict.get('extended') == None: |
| 3695 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 3696 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| 3697 | else: |
| 3698 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| 3699 | if netDict["use"] == "mgmt" or netDict["use"] == "bridge": |
| 3700 | netDict["type"] = "virtual" |
| 3701 | if iface.get("vpci"): |
| 3702 | netDict['vpci'] = iface['vpci'] |
| 3703 | if iface.get("mac"): |
| 3704 | netDict['mac_address'] = iface['mac'] |
| tierno | 6082b7d | 2018-08-31 11:24:08 +0000 | [diff] [blame] | 3705 | if iface.get("mac_address"): |
| 3706 | netDict['mac_address'] = iface['mac_address'] |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3707 | if iface.get("ip_address"): |
| 3708 | netDict['ip_address'] = iface['ip_address'] |
| 3709 | if iface.get("port-security") is not None: |
| 3710 | netDict['port_security'] = iface['port-security'] |
| 3711 | if iface.get("floating-ip") is not None: |
| 3712 | netDict['floating_ip'] = iface['floating-ip'] |
| 3713 | netDict['name'] = iface['internal_name'] |
| 3714 | if iface['net_id'] is None: |
| 3715 | for vnf_iface in sce_vnf["interfaces"]: |
| 3716 | # print iface |
| 3717 | # print vnf_iface |
| 3718 | if vnf_iface['interface_id'] == iface['uuid']: |
| 3719 | netDict['net_id'] = "TASK-{}".format( |
| 3720 | net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3721 | instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id] |
| 3722 | task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3723 | break |
| 3724 | else: |
| 3725 | netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3726 | instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']] |
| 3727 | task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3728 | # skip bridge ifaces not connected to any net |
| 3729 | if 'net_id' not in netDict or netDict['net_id'] == None: |
| 3730 | continue |
| 3731 | myVMDict['networks'].append(netDict) |
| 3732 | db_vm_iface = { |
| 3733 | # "uuid" |
| 3734 | # 'instance_vm_id': instance_vm_uuid, |
| 3735 | "instance_net_id": instance_net_id, |
| 3736 | 'interface_id': iface['uuid'], |
| 3737 | # 'vim_interface_id': , |
| 3738 | 'type': 'external' if iface['external_name'] is not None else 'internal', |
| 3739 | 'ip_address': iface.get('ip_address'), |
| 3740 | 'mac_address': iface.get('mac'), |
| 3741 | 'floating_ip': int(iface.get('floating-ip', False)), |
| 3742 | 'port_security': int(iface.get('port-security', True)) |
| 3743 | } |
| 3744 | db_vm_ifaces.append(db_vm_iface) |
| 3745 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3746 | # print myVMDict['name'] |
| 3747 | # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 3748 | # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 3749 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3750 | |
| 3751 | # We add the RO key to cloud_config if vnf will need ssh access |
| 3752 | cloud_config_vm = cloud_config |
| 3753 | if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'): |
| 3754 | RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]} |
| 3755 | cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key) |
| 3756 | if vm.get("boot_data"): |
| 3757 | cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm) |
| 3758 | |
| 3759 | if myVMDict.get('availability_zone'): |
| 3760 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| 3761 | else: |
| 3762 | av_index = None |
| 3763 | for vm_index in range(0, vm.get('count', 1)): |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 3764 | vm_name = myVMDict['name'] + "-" + str(vm_index+1) |
| 3765 | task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3766 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm, |
| 3767 | myVMDict['disks'], av_index, vnf_availability_zones) |
| 3768 | # put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 3769 | for net in myVMDict['networks']: |
| 3770 | if "vim_id" in net: |
| 3771 | for iface in vm['interfaces']: |
| 3772 | if net["name"] == iface["internal_name"]: |
| 3773 | iface["vim_id"] = net["vim_id"] |
| 3774 | break |
| 3775 | vm_uuid = str(uuid4()) |
| 3776 | uuid_list.append(vm_uuid) |
| 3777 | db_vm = { |
| 3778 | "uuid": vm_uuid, |
| 3779 | 'instance_vnf_id': vnf_uuid, |
| 3780 | # TODO delete "vim_vm_id": vm_id, |
| 3781 | "vm_id": vm["uuid"], |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 3782 | "vim_name": vm_name, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3783 | # "status": |
| 3784 | } |
| 3785 | db_instance_vms.append(db_vm) |
| 3786 | |
| 3787 | iface_index = 0 |
| 3788 | for db_vm_iface in db_vm_ifaces: |
| 3789 | iface_uuid = str(uuid4()) |
| 3790 | uuid_list.append(iface_uuid) |
| 3791 | db_vm_iface_instance = { |
| 3792 | "uuid": iface_uuid, |
| 3793 | "instance_vm_id": vm_uuid |
| 3794 | } |
| 3795 | db_vm_iface_instance.update(db_vm_iface) |
| 3796 | if db_vm_iface_instance.get("ip_address"): # increment ip_address |
| 3797 | ip = db_vm_iface_instance.get("ip_address") |
| 3798 | i = ip.rfind(".") |
| 3799 | if i > 0: |
| 3800 | try: |
| 3801 | i += 1 |
| 3802 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 3803 | db_vm_iface_instance["ip_address"] = ip |
| 3804 | except: |
| 3805 | db_vm_iface_instance["ip_address"] = None |
| 3806 | db_instance_interfaces.append(db_vm_iface_instance) |
| 3807 | myVMDict['networks'][iface_index]["uuid"] = iface_uuid |
| 3808 | iface_index += 1 |
| 3809 | |
| 3810 | db_vim_action = { |
| 3811 | "instance_action_id": instance_action_id, |
| 3812 | "task_index": task_index, |
| 3813 | "datacenter_vim_id": myvim_thread_id, |
| 3814 | "action": "CREATE", |
| 3815 | "status": "SCHEDULED", |
| 3816 | "item": "instance_vms", |
| 3817 | "item_id": vm_uuid, |
| 3818 | "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on}, |
| 3819 | default_flow_style=True, width=256) |
| 3820 | } |
| 3821 | task_index += 1 |
| 3822 | db_vim_actions.append(db_vim_action) |
| 3823 | params_out["task_index"] = task_index |
| 3824 | params_out["uuid_list"] = uuid_list |
| 3825 | |
| 3826 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3827 | def delete_instance(mydb, tenant_id, instance_id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3828 | # print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3829 | instanceDict = mydb.get_instance_scenario(instance_id, tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3830 | # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3831 | tenant_id = instanceDict["tenant_id"] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3832 | # 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] | 3833 | # 1. Delete from Database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3834 | message = mydb.delete_instance_scenario(instance_id, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3835 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3836 | # 2. delete from VIM |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3837 | error_msg = "" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3838 | myvims = {} |
| 3839 | myvim_threads = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3840 | vimthread_affected = {} |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 3841 | net2vm_dependencies = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3842 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3843 | task_index = 0 |
| 3844 | instance_action_id = get_task_id() |
| 3845 | db_vim_actions = [] |
| 3846 | db_instance_action = { |
| 3847 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 3848 | "tenant_id": tenant_id, |
| 3849 | "instance_id": instance_id, |
| 3850 | "description": "DELETE", |
| 3851 | # "number_tasks": 0 # filled bellow |
| 3852 | } |
| 3853 | |
| 3854 | # 2.1 deleting VMs |
| 3855 | # vm_fail_list=[] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3856 | for sce_vnf in instanceDict['vnfs']: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3857 | datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3858 | vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3859 | if datacenter_key not in myvims: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3860 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3861 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3862 | except NfvoException as e: |
| 3863 | logger.error(str(e)) |
| 3864 | myvim_thread = None |
| 3865 | myvim_threads[datacenter_key] = myvim_thread |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3866 | vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"], |
| 3867 | datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 3868 | if len(vims) == 0: |
| 3869 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], |
| 3870 | sce_vnf["datacenter_tenant_id"])) |
| 3871 | myvims[datacenter_key] = None |
| 3872 | else: |
| 3873 | myvims[datacenter_key] = vims.values()[0] |
| 3874 | myvim = myvims[datacenter_key] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3875 | myvim_thread = myvim_threads[datacenter_key] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3876 | for vm in sce_vnf['vms']: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3877 | if not myvim: |
| 3878 | error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"]) |
| 3879 | continue |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 3880 | db_vim_action = { |
| 3881 | "instance_action_id": instance_action_id, |
| 3882 | "task_index": task_index, |
| 3883 | "datacenter_vim_id": sce_vnf["datacenter_tenant_id"], |
| 3884 | "action": "DELETE", |
| 3885 | "status": "SCHEDULED", |
| 3886 | "item": "instance_vms", |
| 3887 | "item_id": vm["uuid"], |
| 3888 | "extra": yaml.safe_dump({"params": vm["interfaces"]}, |
| 3889 | default_flow_style=True, width=256) |
| 3890 | } |
| 3891 | db_vim_actions.append(db_vim_action) |
| 3892 | for interface in vm["interfaces"]: |
| 3893 | if not interface.get("instance_net_id"): |
| 3894 | continue |
| 3895 | if interface["instance_net_id"] not in net2vm_dependencies: |
| 3896 | net2vm_dependencies[interface["instance_net_id"]] = [] |
| 3897 | net2vm_dependencies[interface["instance_net_id"]].append(task_index) |
| 3898 | task_index += 1 |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3899 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3900 | # 2.2 deleting NETS |
| 3901 | # net_fail_list=[] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3902 | for net in instanceDict['nets']: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3903 | vimthread_affected[net["datacenter_tenant_id"]] = None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3904 | datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 3905 | if datacenter_key not in myvims: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3906 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3907 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3908 | except NfvoException as e: |
| 3909 | logger.error(str(e)) |
| 3910 | myvim_thread = None |
| 3911 | myvim_threads[datacenter_key] = myvim_thread |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3912 | vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"], |
| 3913 | datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 3914 | if len(vims) == 0: |
| 3915 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 3916 | myvims[datacenter_key] = None |
| 3917 | else: |
| 3918 | myvims[datacenter_key] = vims.values()[0] |
| 3919 | myvim = myvims[datacenter_key] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3920 | myvim_thread = myvim_threads[datacenter_key] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3921 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3922 | if not myvim: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3923 | error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"]) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3924 | continue |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 3925 | extra = {"params": (net['vim_net_id'], net['sdn_net_id'])} |
| 3926 | if net2vm_dependencies.get(net["uuid"]): |
| 3927 | extra["depends_on"] = net2vm_dependencies[net["uuid"]] |
| 3928 | db_vim_action = { |
| 3929 | "instance_action_id": instance_action_id, |
| 3930 | "task_index": task_index, |
| 3931 | "datacenter_vim_id": net["datacenter_tenant_id"], |
| 3932 | "action": "DELETE", |
| 3933 | "status": "SCHEDULED", |
| 3934 | "item": "instance_nets", |
| 3935 | "item_id": net["uuid"], |
| 3936 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 3937 | } |
| 3938 | task_index += 1 |
| 3939 | db_vim_actions.append(db_vim_action) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3940 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3941 | # 2.3 deleting VNFFGs |
| 3942 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3943 | for sfp in instanceDict.get('sfps', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3944 | vimthread_affected[sfp["datacenter_tenant_id"]] = None |
| 3945 | datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"]) |
| 3946 | if datacenter_key not in myvims: |
| 3947 | try: |
| 3948 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"]) |
| 3949 | except NfvoException as e: |
| 3950 | logger.error(str(e)) |
| 3951 | myvim_thread = None |
| 3952 | myvim_threads[datacenter_key] = myvim_thread |
| 3953 | vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"], |
| 3954 | datacenter_tenant_id=sfp["datacenter_tenant_id"]) |
| 3955 | if len(vims) == 0: |
| 3956 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"])) |
| 3957 | myvims[datacenter_key] = None |
| 3958 | else: |
| 3959 | myvims[datacenter_key] = vims.values()[0] |
| 3960 | myvim = myvims[datacenter_key] |
| 3961 | myvim_thread = myvim_threads[datacenter_key] |
| 3962 | |
| 3963 | if not myvim: |
| 3964 | error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"]) |
| 3965 | continue |
| 3966 | extra = {"params": (sfp['vim_sfp_id'])} |
| 3967 | db_vim_action = { |
| 3968 | "instance_action_id": instance_action_id, |
| 3969 | "task_index": task_index, |
| 3970 | "datacenter_vim_id": sfp["datacenter_tenant_id"], |
| 3971 | "action": "DELETE", |
| 3972 | "status": "SCHEDULED", |
| 3973 | "item": "instance_sfps", |
| 3974 | "item_id": sfp["uuid"], |
| 3975 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 3976 | } |
| 3977 | task_index += 1 |
| 3978 | db_vim_actions.append(db_vim_action) |
| 3979 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3980 | for sf in instanceDict.get('sfs', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3981 | vimthread_affected[sf["datacenter_tenant_id"]] = None |
| 3982 | datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"]) |
| 3983 | if datacenter_key not in myvims: |
| 3984 | try: |
| 3985 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"]) |
| 3986 | except NfvoException as e: |
| 3987 | logger.error(str(e)) |
| 3988 | myvim_thread = None |
| 3989 | myvim_threads[datacenter_key] = myvim_thread |
| 3990 | vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"], |
| 3991 | datacenter_tenant_id=sf["datacenter_tenant_id"]) |
| 3992 | if len(vims) == 0: |
| 3993 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"])) |
| 3994 | myvims[datacenter_key] = None |
| 3995 | else: |
| 3996 | myvims[datacenter_key] = vims.values()[0] |
| 3997 | myvim = myvims[datacenter_key] |
| 3998 | myvim_thread = myvim_threads[datacenter_key] |
| 3999 | |
| 4000 | if not myvim: |
| 4001 | error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"]) |
| 4002 | continue |
| 4003 | extra = {"params": (sf['vim_sf_id'])} |
| 4004 | db_vim_action = { |
| 4005 | "instance_action_id": instance_action_id, |
| 4006 | "task_index": task_index, |
| 4007 | "datacenter_vim_id": sf["datacenter_tenant_id"], |
| 4008 | "action": "DELETE", |
| 4009 | "status": "SCHEDULED", |
| 4010 | "item": "instance_sfs", |
| 4011 | "item_id": sf["uuid"], |
| 4012 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4013 | } |
| 4014 | task_index += 1 |
| 4015 | db_vim_actions.append(db_vim_action) |
| 4016 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 4017 | for sfi in instanceDict.get('sfis', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4018 | vimthread_affected[sfi["datacenter_tenant_id"]] = None |
| 4019 | datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"]) |
| 4020 | if datacenter_key not in myvims: |
| 4021 | try: |
| 4022 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"]) |
| 4023 | except NfvoException as e: |
| 4024 | logger.error(str(e)) |
| 4025 | myvim_thread = None |
| 4026 | myvim_threads[datacenter_key] = myvim_thread |
| 4027 | vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"], |
| 4028 | datacenter_tenant_id=sfi["datacenter_tenant_id"]) |
| 4029 | if len(vims) == 0: |
| 4030 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"])) |
| 4031 | myvims[datacenter_key] = None |
| 4032 | else: |
| 4033 | myvims[datacenter_key] = vims.values()[0] |
| 4034 | myvim = myvims[datacenter_key] |
| 4035 | myvim_thread = myvim_threads[datacenter_key] |
| 4036 | |
| 4037 | if not myvim: |
| 4038 | error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"]) |
| 4039 | continue |
| 4040 | extra = {"params": (sfi['vim_sfi_id'])} |
| 4041 | db_vim_action = { |
| 4042 | "instance_action_id": instance_action_id, |
| 4043 | "task_index": task_index, |
| 4044 | "datacenter_vim_id": sfi["datacenter_tenant_id"], |
| 4045 | "action": "DELETE", |
| 4046 | "status": "SCHEDULED", |
| 4047 | "item": "instance_sfis", |
| 4048 | "item_id": sfi["uuid"], |
| 4049 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4050 | } |
| 4051 | task_index += 1 |
| 4052 | db_vim_actions.append(db_vim_action) |
| 4053 | |
| 4054 | for classification in instanceDict['classifications']: |
| 4055 | vimthread_affected[classification["datacenter_tenant_id"]] = None |
| 4056 | datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 4057 | if datacenter_key not in myvims: |
| 4058 | try: |
| 4059 | _,myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 4060 | except NfvoException as e: |
| 4061 | logger.error(str(e)) |
| 4062 | myvim_thread = None |
| 4063 | myvim_threads[datacenter_key] = myvim_thread |
| 4064 | vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"], |
| 4065 | datacenter_tenant_id=classification["datacenter_tenant_id"]) |
| 4066 | if len(vims) == 0: |
| 4067 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"], classification["datacenter_tenant_id"])) |
| 4068 | myvims[datacenter_key] = None |
| 4069 | else: |
| 4070 | myvims[datacenter_key] = vims.values()[0] |
| 4071 | myvim = myvims[datacenter_key] |
| 4072 | myvim_thread = myvim_threads[datacenter_key] |
| 4073 | |
| 4074 | if not myvim: |
| 4075 | error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'], classification["datacenter_id"]) |
| 4076 | continue |
| 4077 | extra = {"params": (classification['vim_classification_id'])} |
| 4078 | db_vim_action = { |
| 4079 | "instance_action_id": instance_action_id, |
| 4080 | "task_index": task_index, |
| 4081 | "datacenter_vim_id": classification["datacenter_tenant_id"], |
| 4082 | "action": "DELETE", |
| 4083 | "status": "SCHEDULED", |
| 4084 | "item": "instance_classifications", |
| 4085 | "item_id": classification["uuid"], |
| 4086 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4087 | } |
| 4088 | task_index += 1 |
| 4089 | db_vim_actions.append(db_vim_action) |
| 4090 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4091 | db_instance_action["number_tasks"] = task_index |
| 4092 | db_tables = [ |
| 4093 | {"instance_actions": db_instance_action}, |
| 4094 | {"vim_actions": db_vim_actions} |
| 4095 | ] |
| 4096 | |
| 4097 | logger.debug("delete_instance done DB tables: %s", |
| 4098 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4099 | mydb.new_rows(db_tables, ()) |
| 4100 | for myvim_thread_id in vimthread_affected.keys(): |
| 4101 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| 4102 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4103 | if len(error_msg) > 0: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4104 | return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\ |
| 4105 | '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4106 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4107 | return "action_id={} instance {} deleted".format(instance_action_id, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4108 | |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 4109 | def get_instance_id(mydb, tenant_id, instance_id): |
| 4110 | global ovim |
| 4111 | #check valid tenant_id |
| 4112 | check_tenant(mydb, tenant_id) |
| 4113 | #obtain data |
| 4114 | |
| 4115 | instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True) |
| 4116 | for net in instance_dict["nets"]: |
| 4117 | if net.get("sdn_net_id"): |
| 4118 | net_sdn = ovim.show_network(net["sdn_net_id"]) |
| 4119 | net["sdn_info"] = { |
| 4120 | "admin_state_up": net_sdn.get("admin_state_up"), |
| 4121 | "flows": net_sdn.get("flows"), |
| 4122 | "last_error": net_sdn.get("last_error"), |
| 4123 | "ports": net_sdn.get("ports"), |
| 4124 | "type": net_sdn.get("type"), |
| 4125 | "status": net_sdn.get("status"), |
| 4126 | "vlan": net_sdn.get("vlan"), |
| 4127 | } |
| 4128 | return instance_dict |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4129 | |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 4130 | @deprecated("Instance is automatically refreshed by vim_threads") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4131 | def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None): |
| 4132 | '''Refreshes a scenario instance. It modifies instanceDict''' |
| 4133 | '''Returns: |
| 4134 | - 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 |
| 4135 | - error_msg |
| 4136 | ''' |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4137 | # # Assumption: nfvo_tenant and instance_id were checked before entering into this function |
| 4138 | # #print "nfvo.refresh_instance begins" |
| 4139 | # #print json.dumps(instanceDict, indent=4) |
| 4140 | # |
| 4141 | # #print "Getting the VIM URL and the VIM tenant_id" |
| 4142 | # myvims={} |
| 4143 | # |
| 4144 | # # 1. Getting VIM vm and net list |
| 4145 | # vms_updated = [] #List of VM instance uuids in openmano that were updated |
| 4146 | # vms_notupdated=[] |
| 4147 | # vm_list = {} |
| 4148 | # for sce_vnf in instanceDict['vnfs']: |
| 4149 | # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4150 | # if datacenter_key not in vm_list: |
| 4151 | # vm_list[datacenter_key] = [] |
| 4152 | # if datacenter_key not in myvims: |
| 4153 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"], |
| 4154 | # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 4155 | # if len(vims) == 0: |
| 4156 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])) |
| 4157 | # myvims[datacenter_key] = None |
| 4158 | # else: |
| 4159 | # myvims[datacenter_key] = vims.values()[0] |
| 4160 | # for vm in sce_vnf['vms']: |
| 4161 | # vm_list[datacenter_key].append(vm['vim_vm_id']) |
| 4162 | # vms_notupdated.append(vm["uuid"]) |
| 4163 | # |
| 4164 | # nets_updated = [] #List of VM instance uuids in openmano that were updated |
| 4165 | # nets_notupdated=[] |
| 4166 | # net_list = {} |
| 4167 | # for net in instanceDict['nets']: |
| 4168 | # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4169 | # if datacenter_key not in net_list: |
| 4170 | # net_list[datacenter_key] = [] |
| 4171 | # if datacenter_key not in myvims: |
| 4172 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"], |
| 4173 | # datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 4174 | # if len(vims) == 0: |
| 4175 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 4176 | # myvims[datacenter_key] = None |
| 4177 | # else: |
| 4178 | # myvims[datacenter_key] = vims.values()[0] |
| 4179 | # |
| 4180 | # net_list[datacenter_key].append(net['vim_net_id']) |
| 4181 | # nets_notupdated.append(net["uuid"]) |
| 4182 | # |
| 4183 | # # 1. Getting the status of all VMs |
| 4184 | # vm_dict={} |
| 4185 | # for datacenter_key in myvims: |
| 4186 | # if not vm_list.get(datacenter_key): |
| 4187 | # continue |
| 4188 | # failed = True |
| 4189 | # failed_message="" |
| 4190 | # if not myvims[datacenter_key]: |
| 4191 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4192 | # else: |
| 4193 | # try: |
| 4194 | # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) ) |
| 4195 | # failed = False |
| 4196 | # except vimconn.vimconnException as e: |
| 4197 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4198 | # failed_message = str(e) |
| 4199 | # if failed: |
| 4200 | # for vm in vm_list[datacenter_key]: |
| 4201 | # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4202 | # |
| 4203 | # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed |
| 4204 | # for sce_vnf in instanceDict['vnfs']: |
| 4205 | # for vm in sce_vnf['vms']: |
| 4206 | # vm_id = vm['vim_vm_id'] |
| 4207 | # interfaces = vm_dict[vm_id].pop('interfaces', []) |
| 4208 | # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE |
| 4209 | # has_mgmt_iface = False |
| 4210 | # for iface in vm["interfaces"]: |
| 4211 | # if iface["type"]=="mgmt": |
| 4212 | # has_mgmt_iface = True |
| 4213 | # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface: |
| 4214 | # vm_dict[vm_id]['status'] = "ACTIVE" |
| 4215 | # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024: |
| 4216 | # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:] |
| 4217 | # 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'): |
| 4218 | # vm['status'] = vm_dict[vm_id]['status'] |
| 4219 | # vm['error_msg'] = vm_dict[vm_id].get('error_msg') |
| 4220 | # vm['vim_info'] = vm_dict[vm_id].get('vim_info') |
| 4221 | # # 2.1. Update in openmano DB the VMs whose status changed |
| 4222 | # try: |
| 4223 | # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]}) |
| 4224 | # vms_notupdated.remove(vm["uuid"]) |
| 4225 | # if updates>0: |
| 4226 | # vms_updated.append(vm["uuid"]) |
| 4227 | # except db_base_Exception as e: |
| 4228 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4229 | # # 2.2. Update in openmano DB the interface VMs |
| 4230 | # for interface in interfaces: |
| 4231 | # #translate from vim_net_id to instance_net_id |
| 4232 | # network_id_list=[] |
| 4233 | # for net in instanceDict['nets']: |
| 4234 | # if net["vim_net_id"] == interface["vim_net_id"]: |
| 4235 | # network_id_list.append(net["uuid"]) |
| 4236 | # if not network_id_list: |
| 4237 | # continue |
| 4238 | # del interface["vim_net_id"] |
| 4239 | # try: |
| 4240 | # for network_id in network_id_list: |
| 4241 | # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id}) |
| 4242 | # except db_base_Exception as e: |
| 4243 | # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id) |
| 4244 | # |
| 4245 | # # 3. Getting the status of all nets |
| 4246 | # net_dict = {} |
| 4247 | # for datacenter_key in myvims: |
| 4248 | # if not net_list.get(datacenter_key): |
| 4249 | # continue |
| 4250 | # failed = True |
| 4251 | # failed_message = "" |
| 4252 | # if not myvims[datacenter_key]: |
| 4253 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4254 | # else: |
| 4255 | # try: |
| 4256 | # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) ) |
| 4257 | # failed = False |
| 4258 | # except vimconn.vimconnException as e: |
| 4259 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4260 | # failed_message = str(e) |
| 4261 | # if failed: |
| 4262 | # for net in net_list[datacenter_key]: |
| 4263 | # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4264 | # |
| 4265 | # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed |
| 4266 | # # TODO: update nets inside a vnf |
| 4267 | # for net in instanceDict['nets']: |
| 4268 | # net_id = net['vim_net_id'] |
| 4269 | # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024: |
| 4270 | # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:] |
| 4271 | # 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'): |
| 4272 | # net['status'] = net_dict[net_id]['status'] |
| 4273 | # net['error_msg'] = net_dict[net_id].get('error_msg') |
| 4274 | # net['vim_info'] = net_dict[net_id].get('vim_info') |
| 4275 | # # 5.1. Update in openmano DB the nets whose status changed |
| 4276 | # try: |
| 4277 | # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]}) |
| 4278 | # nets_notupdated.remove(net["uuid"]) |
| 4279 | # if updated>0: |
| 4280 | # nets_updated.append(net["uuid"]) |
| 4281 | # except db_base_Exception as e: |
| 4282 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4283 | # |
| 4284 | # # Returns appropriate output |
| 4285 | # #print "nfvo.refresh_instance finishes" |
| 4286 | # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s", |
| 4287 | # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4288 | instance_id = instanceDict['uuid'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4289 | # if len(vms_notupdated)+len(nets_notupdated)>0: |
| 4290 | # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated) |
| 4291 | # 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] | 4292 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4293 | return 0, 'Scenario instance ' + instance_id + ' refreshed.' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4294 | |
| 4295 | def instance_action(mydb,nfvo_tenant,instance_id, action_dict): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4296 | #print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4297 | instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4298 | #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| 4299 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4300 | #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] | 4301 | vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id']) |
| 4302 | if len(vims) == 0: |
| 4303 | raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4304 | myvim = vims.values()[0] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4305 | vm_result = {} |
| 4306 | vm_error = 0 |
| 4307 | vm_ok = 0 |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4308 | |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4309 | myvim_threads_id = {} |
| 4310 | if action_dict.get("vdu-scaling"): |
| 4311 | db_instance_vms = [] |
| 4312 | db_vim_actions = [] |
| 4313 | db_instance_interfaces = [] |
| 4314 | instance_action_id = get_task_id() |
| 4315 | db_instance_action = { |
| 4316 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 4317 | "tenant_id": nfvo_tenant, |
| 4318 | "instance_id": instance_id, |
| 4319 | "description": "SCALE", |
| 4320 | } |
| 4321 | vm_result["instance_action_id"] = instance_action_id |
| 4322 | task_index = 0 |
| 4323 | for vdu in action_dict["vdu-scaling"]: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4324 | vdu_id = vdu.get("vdu-id") |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4325 | osm_vdu_id = vdu.get("osm_vdu_id") |
| 4326 | member_vnf_index = vdu.get("member-vnf-index") |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4327 | vdu_count = vdu.get("count", 1) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4328 | if vdu_id: |
| 4329 | target_vm = mydb.get_rows( |
| 4330 | FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid", |
| 4331 | WHERE={"vms.uuid": vdu_id}, |
| 4332 | ORDER_BY="vms.created_at" |
| 4333 | ) |
| 4334 | if not target_vm: |
| 4335 | raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), HTTP_Not_Found) |
| 4336 | else: |
| 4337 | if not osm_vdu_id and not member_vnf_index: |
| 4338 | raise NfvoException("Invalid imput vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'") |
| 4339 | target_vm = mydb.get_rows( |
| 4340 | # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"), |
| 4341 | FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\ |
| 4342 | " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\ |
| 4343 | " join vms on ivms.vm_id=vms.uuid", |
| 4344 | WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index}, |
| 4345 | ORDER_BY="ivms.created_at" |
| 4346 | ) |
| 4347 | if not target_vm: |
| 4348 | raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), HTTP_Not_Found) |
| 4349 | vdu_id = target_vm[-1]["uuid"] |
| 4350 | vm_result[vdu_id] = {"created": [], "deleted": [], "description": "scheduled"} |
| 4351 | target_vm = target_vm[-1] |
| 4352 | datacenter = target_vm["datacenter_id"] |
| 4353 | myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter) |
| 4354 | if vdu["type"] == "delete": |
| 4355 | # look for nm |
| 4356 | vm_interfaces = None |
| 4357 | for sce_vnf in instanceDict['vnfs']: |
| 4358 | for vm in sce_vnf['vms']: |
| 4359 | if vm["uuid"] == vdu_id: |
| 4360 | vm_interfaces = vm["interfaces"] |
| 4361 | break |
| 4362 | |
| 4363 | db_vim_action = { |
| 4364 | "instance_action_id": instance_action_id, |
| 4365 | "task_index": task_index, |
| 4366 | "datacenter_vim_id": target_vm["datacenter_tenant_id"], |
| 4367 | "action": "DELETE", |
| 4368 | "status": "SCHEDULED", |
| 4369 | "item": "instance_vms", |
| 4370 | "item_id": target_vm["uuid"], |
| 4371 | "extra": yaml.safe_dump({"params": vm_interfaces}, |
| 4372 | default_flow_style=True, width=256) |
| 4373 | } |
| 4374 | task_index += 1 |
| 4375 | db_vim_actions.append(db_vim_action) |
| 4376 | vm_result[vdu_id]["deleted"].append(vdu_id) |
| 4377 | # delete from database |
| 4378 | db_instance_vms.append({"TO-DELETE": vdu_id}) |
| 4379 | |
| 4380 | else: # vdu["type"] == "create": |
| 4381 | iface2iface = {} |
| 4382 | where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"} |
| 4383 | |
| 4384 | vim_action_to_clone = mydb.get_rows(FROM="vim_actions", WHERE=where) |
| 4385 | if not vim_action_to_clone: |
| 4386 | raise NfvoException("Cannot find the vim_action at database with {}".format(where), HTTP_Internal_Server_Error) |
| 4387 | vim_action_to_clone = vim_action_to_clone[0] |
| 4388 | extra = yaml.safe_load(vim_action_to_clone["extra"]) |
| 4389 | |
| 4390 | # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y |
| 4391 | # TODO do the same for flavor and image when available |
| 4392 | task_depends_on = [] |
| 4393 | task_params = extra["params"] |
| 4394 | task_params_networks = deepcopy(task_params[5]) |
| 4395 | for iface in task_params[5]: |
| 4396 | if iface["net_id"].startswith("TASK-"): |
| 4397 | if "." not in iface["net_id"]: |
| 4398 | task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4399 | iface["net_id"][5:])) |
| 4400 | iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4401 | iface["net_id"][5:]) |
| 4402 | else: |
| 4403 | task_depends_on.append(iface["net_id"][5:]) |
| 4404 | if "mac_address" in iface: |
| 4405 | del iface["mac_address"] |
| 4406 | |
| 4407 | vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]}) |
| 4408 | for index in range(0, vdu_count): |
| 4409 | vm_uuid = str(uuid4()) |
| 4410 | vm_name = target_vm.get('vim_name') |
| 4411 | try: |
| 4412 | suffix = vm_name.rfind("-") |
| 4413 | vm_name = vm_name[:suffix+1] + str(1 + int(vm_name[suffix+1:])) |
| 4414 | except Exception: |
| 4415 | pass |
| 4416 | db_instance_vm = { |
| 4417 | "uuid": vm_uuid, |
| 4418 | 'instance_vnf_id': target_vm['instance_vnf_id'], |
| 4419 | 'vm_id': target_vm['vm_id'], |
| 4420 | 'vim_name': vm_name |
| 4421 | } |
| 4422 | db_instance_vms.append(db_instance_vm) |
| 4423 | |
| 4424 | for vm_iface in vm_ifaces_to_clone: |
| 4425 | iface_uuid = str(uuid4()) |
| 4426 | iface2iface[vm_iface["uuid"]] = iface_uuid |
| 4427 | db_vm_iface = { |
| 4428 | "uuid": iface_uuid, |
| 4429 | 'instance_vm_id': vm_uuid, |
| 4430 | "instance_net_id": vm_iface["instance_net_id"], |
| 4431 | 'interface_id': vm_iface['interface_id'], |
| 4432 | 'type': vm_iface['type'], |
| 4433 | 'floating_ip': vm_iface['floating_ip'], |
| 4434 | 'port_security': vm_iface['port_security'] |
| 4435 | } |
| 4436 | db_instance_interfaces.append(db_vm_iface) |
| 4437 | task_params_copy = deepcopy(task_params) |
| 4438 | for iface in task_params_copy[5]: |
| 4439 | iface["uuid"] = iface2iface[iface["uuid"]] |
| 4440 | # increment ip_address |
| 4441 | if "ip_address" in iface: |
| 4442 | ip = iface.get("ip_address") |
| 4443 | i = ip.rfind(".") |
| 4444 | if i > 0: |
| 4445 | try: |
| 4446 | i += 1 |
| 4447 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 4448 | iface["ip_address"] = ip |
| 4449 | except: |
| 4450 | iface["ip_address"] = None |
| 4451 | if vm_name: |
| 4452 | task_params_copy[0] = vm_name |
| 4453 | db_vim_action = { |
| 4454 | "instance_action_id": instance_action_id, |
| 4455 | "task_index": task_index, |
| 4456 | "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"], |
| 4457 | "action": "CREATE", |
| 4458 | "status": "SCHEDULED", |
| 4459 | "item": "instance_vms", |
| 4460 | "item_id": vm_uuid, |
| 4461 | # ALF |
| 4462 | # ALF |
| 4463 | # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd. |
| 4464 | # ALF |
| 4465 | # ALF |
| 4466 | "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256) |
| 4467 | } |
| 4468 | task_index += 1 |
| 4469 | db_vim_actions.append(db_vim_action) |
| 4470 | vm_result[vdu_id]["created"].append(vm_uuid) |
| 4471 | |
| 4472 | db_instance_action["number_tasks"] = task_index |
| 4473 | db_tables = [ |
| 4474 | {"instance_vms": db_instance_vms}, |
| 4475 | {"instance_interfaces": db_instance_interfaces}, |
| 4476 | {"instance_actions": db_instance_action}, |
| 4477 | # TODO revise sfps |
| 4478 | # {"instance_sfis": db_instance_sfis}, |
| 4479 | # {"instance_sfs": db_instance_sfs}, |
| 4480 | # {"instance_classifications": db_instance_classifications}, |
| 4481 | # {"instance_sfps": db_instance_sfps}, |
| 4482 | {"vim_actions": db_vim_actions} |
| 4483 | ] |
| 4484 | logger.debug("create_vdu done DB tables: %s", |
| 4485 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4486 | mydb.new_rows(db_tables, []) |
| 4487 | for myvim_thread in myvim_threads_id.values(): |
| 4488 | vim_threads["running"][myvim_thread].insert_task(db_vim_actions) |
| 4489 | |
| 4490 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4491 | |
| 4492 | input_vnfs = action_dict.pop("vnfs", []) |
| 4493 | input_vms = action_dict.pop("vms", []) |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4494 | 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] | 4495 | for sce_vnf in instanceDict['vnfs']: |
| 4496 | for vm in sce_vnf['vms']: |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4497 | if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \ |
| 4498 | sce_vnf['member_vnf_index'] not in input_vnfs and \ |
| 4499 | vm['uuid'] not in input_vms and vm['name'] not in input_vms: |
| 4500 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4501 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4502 | if "add_public_key" in action_dict: |
| 4503 | mgmt_access = {} |
| 4504 | if sce_vnf.get('mgmt_access'): |
| 4505 | mgmt_access = yaml.load(sce_vnf['mgmt_access']) |
| 4506 | ssh_access = mgmt_access['config-access']['ssh-access'] |
| 4507 | tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4508 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4509 | if ssh_access['required'] and ssh_access['default-user']: |
| 4510 | if 'ip_address' in vm: |
| 4511 | mgmt_ip = vm['ip_address'].split(';') |
| 4512 | password = mgmt_access['config-access'].get('password') |
| 4513 | priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid']) |
| 4514 | myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'], |
| 4515 | action_dict['add_public_key'], |
| 4516 | password=password, ro_key=priv_RO_key) |
| 4517 | else: |
| 4518 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 4519 | HTTP_Internal_Server_Error) |
| 4520 | except KeyError: |
| 4521 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 4522 | HTTP_Internal_Server_Error) |
| 4523 | else: |
| 4524 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 4525 | HTTP_Internal_Server_Error) |
| 4526 | else: |
| 4527 | data = myvim.action_vminstance(vm['vim_vm_id'], action_dict) |
| 4528 | if "console" in action_dict: |
| 4529 | if not global_config["http_console_proxy"]: |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4530 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4531 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4532 | protocol=data["protocol"], |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4533 | ip = data["server"], |
| 4534 | port = data["port"], |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4535 | suffix = data["suffix"]), |
| 4536 | "name":vm['name'] |
| 4537 | } |
| 4538 | vm_ok +=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4539 | elif data["server"]=="127.0.0.1" or data["server"]=="localhost": |
| 4540 | vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized, |
| 4541 | "description": "this console is only reachable by local interface", |
| 4542 | "name":vm['name'] |
| 4543 | } |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4544 | vm_error+=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4545 | else: |
| 4546 | #print "console data", data |
| 4547 | try: |
| 4548 | console_thread = create_or_use_console_proxy_thread(data["server"], data["port"]) |
| 4549 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4550 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4551 | protocol=data["protocol"], |
| 4552 | ip = global_config["http_console_host"], |
| 4553 | port = console_thread.port, |
| 4554 | suffix = data["suffix"]), |
| 4555 | "name":vm['name'] |
| 4556 | } |
| 4557 | vm_ok +=1 |
| 4558 | except NfvoException as e: |
| 4559 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4560 | vm_error+=1 |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4561 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4562 | else: |
| 4563 | vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']} |
| 4564 | vm_ok +=1 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4565 | except vimconn.vimconnException as e: |
| 4566 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4567 | vm_error+=1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4568 | |
| 4569 | if vm_ok==0: #all goes wrong |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4570 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4571 | else: |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4572 | return vm_result |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4573 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4574 | def instance_action_get(mydb, nfvo_tenant, instance_id, action_id): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4575 | filter = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4576 | if nfvo_tenant and nfvo_tenant != "any": |
| 4577 | filter["tenant_id"] = nfvo_tenant |
| 4578 | if instance_id and instance_id != "any": |
| 4579 | filter["instance_id"] = instance_id |
| 4580 | if action_id: |
| 4581 | filter["uuid"] = action_id |
| 4582 | rows = mydb.get_rows(FROM="instance_actions", WHERE=filter) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4583 | if action_id: |
| 4584 | if not rows: |
| 4585 | raise NfvoException("Not found any action with this criteria", HTTP_Not_Found) |
| 4586 | vim_actions = mydb.get_rows(FROM="vim_actions", WHERE={"instance_action_id": action_id}) |
| 4587 | rows[0]["vim_actions"] = vim_actions |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4588 | return {"actions": rows} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4589 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4590 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4591 | def create_or_use_console_proxy_thread(console_server, console_port): |
| 4592 | #look for a non-used port |
| 4593 | console_thread_key = console_server + ":" + str(console_port) |
| 4594 | if console_thread_key in global_config["console_thread"]: |
| 4595 | #global_config["console_thread"][console_thread_key].start_timeout() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4596 | return global_config["console_thread"][console_thread_key] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4597 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4598 | for port in global_config["console_port_iterator"](): |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4599 | #print "create_or_use_console_proxy_thread() port:", port |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4600 | if port in global_config["console_ports"]: |
| 4601 | continue |
| 4602 | try: |
| 4603 | clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port) |
| 4604 | clithread.start() |
| 4605 | global_config["console_thread"][console_thread_key] = clithread |
| 4606 | global_config["console_ports"][port] = console_thread_key |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4607 | return clithread |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4608 | except cli.ConsoleProxyExceptionPortUsed as e: |
| 4609 | #port used, try with onoher |
| 4610 | continue |
| 4611 | except cli.ConsoleProxyException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4612 | raise NfvoException(str(e), HTTP_Bad_Request) |
| 4613 | raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4614 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4615 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4616 | def check_tenant(mydb, tenant_id): |
| 4617 | '''check that tenant exists at database''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4618 | tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id}) |
| 4619 | if not tenant: |
| 4620 | raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found) |
| 4621 | return |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4622 | |
| 4623 | def new_tenant(mydb, tenant_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4624 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4625 | tenant_uuid = str(uuid4()) |
| 4626 | tenant_dict['uuid'] = tenant_uuid |
| 4627 | try: |
| 4628 | pub_key, priv_key = create_RO_keypair(tenant_uuid) |
| 4629 | tenant_dict['RO_pub_key'] = pub_key |
| 4630 | tenant_dict['encrypted_RO_priv_key'] = priv_key |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4631 | mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4632 | except db_base_Exception as e: |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 4633 | 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] | 4634 | return tenant_uuid |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4635 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4636 | def delete_tenant(mydb, tenant): |
| 4637 | #get nfvo_tenant info |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4638 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4639 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant') |
| 4640 | mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid']) |
| 4641 | return tenant_dict['uuid'] + " " + tenant_dict["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4642 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4643 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4644 | def new_datacenter(mydb, datacenter_descriptor): |
| tierno | 1c848c0 | 2018-05-21 16:40:33 +0200 | [diff] [blame] | 4645 | sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4646 | if "config" in datacenter_descriptor: |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4647 | sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None) |
| 4648 | datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True, |
| 4649 | width=256) |
| 4650 | # Check that datacenter-type is correct |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4651 | datacenter_type = datacenter_descriptor.get("type", "openvim"); |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4652 | # module_info = None |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4653 | try: |
| 4654 | module = "vimconn_" + datacenter_type |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4655 | pkg = __import__("osm_ro." + module) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4656 | # vim_conn = getattr(pkg, module) |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4657 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4658 | except (IOError, ImportError): |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4659 | # if module_info and module_info[0]: |
| 4660 | # file.close(module_info[0]) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4661 | raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type, |
| 4662 | module), |
| 4663 | HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4664 | |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4665 | 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] | 4666 | if sdn_port_mapping: |
| 4667 | try: |
| 4668 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping) |
| 4669 | except Exception as e: |
| 4670 | mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback |
| 4671 | raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4672 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4673 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4674 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4675 | def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4676 | # obtain data, check that only one exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4677 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4678 | |
| 4679 | # edit data |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4680 | datacenter_id = datacenter['uuid'] |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4681 | where = {'uuid': datacenter['uuid']} |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4682 | remove_port_mapping = False |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4683 | new_sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4684 | if "config" in datacenter_descriptor: |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4685 | if datacenter_descriptor['config'] != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4686 | try: |
| 4687 | new_config_dict = datacenter_descriptor["config"] |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4688 | if "sdn-port-mapping" in new_config_dict: |
| 4689 | remove_port_mapping = True |
| 4690 | new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping") |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4691 | # delete null fields |
| 4692 | to_delete = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4693 | for k in new_config_dict: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4694 | if new_config_dict[k] is None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4695 | to_delete.append(k) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4696 | if k == 'sdn-controller': |
| 4697 | remove_port_mapping = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4698 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4699 | config_text = datacenter.get("config") |
| 4700 | if not config_text: |
| 4701 | config_text = '{}' |
| 4702 | config_dict = yaml.load(config_text) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4703 | config_dict.update(new_config_dict) |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4704 | # delete null fields |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4705 | for k in to_delete: |
| 4706 | del config_dict[k] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4707 | except Exception as e: |
| 4708 | raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4709 | if config_dict: |
| 4710 | datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256) |
| 4711 | else: |
| 4712 | datacenter_descriptor["config"] = None |
| 4713 | if remove_port_mapping: |
| 4714 | try: |
| 4715 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id) |
| 4716 | except ovimException as e: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4717 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e), HTTP_Conflict) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4718 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4719 | mydb.update_rows('datacenters', datacenter_descriptor, where) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4720 | if new_sdn_port_mapping: |
| 4721 | try: |
| 4722 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping) |
| 4723 | except ovimException as e: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4724 | # Rollback |
| 4725 | mydb.update_rows('datacenters', datacenter, where) |
| 4726 | raise NfvoException("Error adding datacenter-port-mapping " + str(e), HTTP_Conflict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4727 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4728 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4729 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4730 | def delete_datacenter(mydb, datacenter): |
| 4731 | #get nfvo_tenant info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4732 | datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter') |
| 4733 | mydb.delete_row_by_id("datacenters", datacenter_dict['uuid']) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4734 | try: |
| 4735 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid']) |
| 4736 | except ovimException as e: |
| tierno | d72182f | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4737 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4738 | return datacenter_dict['uuid'] + " " + datacenter_dict['name'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4739 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4740 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4741 | def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None, |
| 4742 | vim_username=None, vim_password=None, config=None): |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 4743 | # get datacenter info |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4744 | try: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4745 | if not datacenter_id: |
| 4746 | if not vim_id: |
| 4747 | raise NfvoException("You must provide 'vim_id", http_code=HTTP_Bad_Request) |
| 4748 | datacenter_id = vim_id |
| 4749 | datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4750 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4751 | 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] | 4752 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4753 | # get nfvo_tenant info |
| 4754 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant) |
| 4755 | if vim_tenant_name==None: |
| 4756 | vim_tenant_name=tenant_dict['name'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4757 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4758 | tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id } |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4759 | # #check that this association does not exist before |
| 4760 | # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 4761 | # if len(tenants_datacenters)>0: |
| 4762 | # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4763 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4764 | vim_tenant_id_exist_atdb=False |
| 4765 | if not create_vim_tenant: |
| 4766 | where_={"datacenter_id": datacenter_id} |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4767 | if vim_tenant!=None: |
| 4768 | where_["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4769 | if vim_tenant_name!=None: |
| 4770 | where_["vim_tenant_name"] = vim_tenant_name |
| 4771 | #check if vim_tenant_id is already at database |
| 4772 | datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_) |
| 4773 | if len(datacenter_tenants_dict)>=1: |
| 4774 | datacenter_tenants_dict = datacenter_tenants_dict[0] |
| 4775 | vim_tenant_id_exist_atdb=True |
| 4776 | #TODO check if a field has changed and edit entry at datacenter_tenants at DB |
| 4777 | else: #result=0 |
| 4778 | datacenter_tenants_dict = {} |
| 4779 | #insert at table datacenter_tenants |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4780 | else: #if vim_tenant==None: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4781 | #create tenant at VIM if not provided |
| 4782 | try: |
| 4783 | _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, |
| 4784 | vim_passwd=vim_password) |
| 4785 | datacenter_name = myvim["name"] |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4786 | 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] | 4787 | except vimconn.vimconnException as e: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4788 | raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant, str(e)), HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4789 | datacenter_tenants_dict = {} |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4790 | datacenter_tenants_dict["created"]="true" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4791 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4792 | #fill datacenter_tenants table |
| 4793 | if not vim_tenant_id_exist_atdb: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4794 | datacenter_tenants_dict["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4795 | datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name |
| 4796 | datacenter_tenants_dict["user"] = vim_username |
| 4797 | datacenter_tenants_dict["passwd"] = vim_password |
| 4798 | datacenter_tenants_dict["datacenter_id"] = datacenter_id |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4799 | if name: |
| 4800 | datacenter_tenants_dict["name"] = name |
| 4801 | else: |
| 4802 | datacenter_tenants_dict["name"] = datacenter_name |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4803 | if config: |
| 4804 | datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256) |
| 4805 | id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True) |
| 4806 | datacenter_tenants_dict["uuid"] = id_ |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4807 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4808 | #fill tenants_datacenters table |
| 4809 | datacenter_tenant_id = datacenter_tenants_dict["uuid"] |
| 4810 | tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id |
| 4811 | mydb.new_row('tenants_datacenters', tenants_datacenter_dict) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4812 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4813 | # create thread |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4814 | 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] | 4815 | 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] | 4816 | db=db, db_lock=db_lock, ovim=ovim) |
| 4817 | new_thread.start() |
| 4818 | thread_id = datacenter_tenants_dict["uuid"] |
| 4819 | vim_threads["running"][thread_id] = new_thread |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4820 | return thread_id |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4821 | except vimconn.vimconnException as e: |
| 4822 | raise NfvoException(str(e), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4823 | |
| tierno | 9931490 | 2017-04-26 13:23:09 +0200 | [diff] [blame] | 4824 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4825 | def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None, |
| 4826 | 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] | 4827 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4828 | # get vim_account; check is valid for this tenant |
| 4829 | from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id" |
| 4830 | where_ = {"td.nfvo_tenant_id": nfvo_tenant} |
| 4831 | if datacenter_tenant_id: |
| 4832 | where_["dt.uuid"] = datacenter_tenant_id |
| 4833 | if datacenter_id: |
| 4834 | where_["dt.datacenter_id"] = datacenter_id |
| 4835 | vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_) |
| 4836 | if not vim_accounts: |
| 4837 | raise NfvoException("vim_account not found for this tenant", http_code=HTTP_Not_Found) |
| 4838 | elif len(vim_accounts) > 1: |
| 4839 | raise NfvoException("found more than one vim_account for this tenant", http_code=HTTP_Conflict) |
| 4840 | datacenter_tenant_id = vim_accounts[0]["uuid"] |
| 4841 | original_config = vim_accounts[0]["config"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4842 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4843 | update_ = {} |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4844 | if config: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4845 | original_config_dict = yaml.load(original_config) |
| 4846 | original_config_dict.update(config) |
| 4847 | update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256) |
| 4848 | if name: |
| 4849 | update_['name'] = name |
| 4850 | if vim_tenant: |
| 4851 | update_['vim_tenant_id'] = vim_tenant |
| 4852 | if vim_tenant_name: |
| 4853 | update_['vim_tenant_name'] = vim_tenant_name |
| 4854 | if vim_username: |
| 4855 | update_['user'] = vim_username |
| 4856 | if vim_password: |
| 4857 | update_['passwd'] = vim_password |
| 4858 | if update_: |
| 4859 | 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] | 4860 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4861 | vim_threads["running"][datacenter_tenant_id].insert_task("reload") |
| 4862 | return datacenter_tenant_id |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4863 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4864 | def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4865 | #get nfvo_tenant info |
| 4866 | if not tenant_id or tenant_id=="any": |
| 4867 | tenant_uuid = None |
| 4868 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4869 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4870 | tenant_uuid = tenant_dict['uuid'] |
| 4871 | |
| 4872 | #check that this association exist before |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4873 | tenants_datacenter_dict = {} |
| 4874 | if datacenter: |
| 4875 | datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter) |
| 4876 | tenants_datacenter_dict["datacenter_id"] = datacenter_id |
| 4877 | elif vim_account_id: |
| 4878 | tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4879 | if tenant_uuid: |
| 4880 | tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4881 | tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 4882 | if len(tenant_datacenter_list)==0 and tenant_uuid: |
| 4883 | raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4884 | |
| 4885 | #delete this association |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4886 | mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4887 | |
| 4888 | #get vim_tenant info and deletes |
| 4889 | warning='' |
| 4890 | for tenant_datacenter_item in tenant_datacenter_list: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4891 | vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 4892 | #try to delete vim:tenant |
| 4893 | try: |
| 4894 | mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 4895 | if vim_tenant_dict['created']=='true': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4896 | #delete tenant at VIM if created by NFVO |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4897 | try: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4898 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4899 | myvim.delete_tenant(vim_tenant_dict['vim_tenant_id']) |
| 4900 | except vimconn.vimconnException as e: |
| 4901 | warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e)) |
| 4902 | logger.warn(warning) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4903 | except db_base_Exception as e: |
| 4904 | logger.error("Cannot delete datacenter_tenants " + str(e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4905 | 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] | 4906 | thread_id = tenant_datacenter_item["datacenter_tenant_id"] |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 4907 | thread = vim_threads["running"].get(thread_id) |
| 4908 | if thread: |
| 4909 | thread.insert_task("exit") |
| 4910 | vim_threads["deleting"][thread_id] = thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4911 | return "datacenter {} detached. {}".format(datacenter_id, warning) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4912 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4913 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4914 | def datacenter_action(mydb, tenant_id, datacenter, action_dict): |
| 4915 | #DEPRECATED |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4916 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4917 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4918 | |
| 4919 | if 'net-update' in action_dict: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4920 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4921 | 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] | 4922 | #print content |
| 4923 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4924 | #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e)) |
| 4925 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4926 | #update nets Change from VIM format to NFVO format |
| 4927 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4928 | for net in nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4929 | net_nfvo={'datacenter_id': datacenter_id} |
| 4930 | net_nfvo['name'] = net['name'] |
| 4931 | #net_nfvo['description']= net['name'] |
| 4932 | net_nfvo['vim_net_id'] = net['id'] |
| 4933 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 4934 | net_nfvo['shared'] = net['shared'] |
| 4935 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| 4936 | net_list.append(net_nfvo) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4937 | inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list) |
| 4938 | logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted) |
| 4939 | return inserted |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4940 | elif 'net-edit' in action_dict: |
| 4941 | net = action_dict['net-edit'].pop('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4942 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4943 | result = mydb.update_rows('datacenter_nets', action_dict['net-edit'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4944 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4945 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4946 | elif 'net-delete' in action_dict: |
| 4947 | net = action_dict['net-deelte'].get('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4948 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4949 | result = mydb.delete_row(FROM='datacenter_nets', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4950 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4951 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4952 | |
| 4953 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4954 | raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4955 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4956 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4957 | def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): |
| 4958 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4959 | datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4960 | |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4961 | what = 'uuid' if utils.check_valid_uuid(netmap) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4962 | result = mydb.update_rows('datacenter_nets', action_dict['netmap'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4963 | WHERE={'datacenter_id':datacenter_id, what: netmap}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4964 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4965 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4966 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4967 | def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): |
| 4968 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4969 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4970 | filter_dict={} |
| 4971 | if action_dict: |
| 4972 | action_dict = action_dict["netmap"] |
| 4973 | if 'vim_id' in action_dict: |
| 4974 | filter_dict["id"] = action_dict['vim_id'] |
| 4975 | if 'vim_name' in action_dict: |
| 4976 | filter_dict["name"] = action_dict['vim_name'] |
| 4977 | else: |
| 4978 | filter_dict["shared"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4979 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4980 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4981 | vim_nets = myvim.get_network_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4982 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4983 | #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e)) |
| 4984 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| 4985 | if len(vim_nets)>1 and action_dict: |
| 4986 | raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict) |
| 4987 | elif len(vim_nets)==0: # and action_dict: |
| 4988 | raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4989 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4990 | for net in vim_nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4991 | net_nfvo={'datacenter_id': datacenter_id} |
| 4992 | if action_dict and "name" in action_dict: |
| 4993 | net_nfvo['name'] = action_dict['name'] |
| 4994 | else: |
| 4995 | net_nfvo['name'] = net['name'] |
| 4996 | #net_nfvo['description']= net['name'] |
| 4997 | net_nfvo['vim_net_id'] = net['id'] |
| 4998 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 4999 | net_nfvo['shared'] = net['shared'] |
| 5000 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5001 | try: |
| 5002 | net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5003 | net_nfvo["status"] = "OK" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5004 | net_nfvo["uuid"] = net_id |
| 5005 | except db_base_Exception as e: |
| 5006 | if action_dict: |
| 5007 | raise |
| 5008 | else: |
| 5009 | net_nfvo["status"] = "FAIL: " + str(e) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5010 | net_list.append(net_nfvo) |
| 5011 | return net_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5012 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5013 | def get_sdn_net_id(mydb, tenant_id, datacenter, network_id): |
| 5014 | # obtain all network data |
| 5015 | try: |
| 5016 | if utils.check_valid_uuid(network_id): |
| 5017 | filter_dict = {"id": network_id} |
| 5018 | else: |
| 5019 | filter_dict = {"name": network_id} |
| 5020 | |
| 5021 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 5022 | network = myvim.get_network_list(filter_dict=filter_dict) |
| 5023 | except vimconn.vimconnException as e: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 5024 | 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] | 5025 | |
| 5026 | # ensure the network is defined |
| 5027 | if len(network) == 0: |
| 5028 | raise NfvoException("Network {} is not present in the system".format(network_id), |
| 5029 | HTTP_Bad_Request) |
| 5030 | |
| 5031 | # ensure there is only one network with the provided name |
| 5032 | if len(network) > 1: |
| 5033 | raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request) |
| 5034 | |
| 5035 | # ensure it is a dataplane network |
| 5036 | if network[0]['type'] != 'data': |
| 5037 | return None |
| 5038 | |
| 5039 | # ensure we use the id |
| 5040 | network_id = network[0]['id'] |
| 5041 | |
| 5042 | # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id |
| 5043 | # and with instance_scenario_id==NULL |
| 5044 | #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None} |
| 5045 | search_dict = {'vim_net_id': network_id} |
| 5046 | |
| 5047 | try: |
| 5048 | #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id'] |
| 5049 | result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict) |
| 5050 | except db_base_Exception as e: |
| 5051 | 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] | 5052 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5053 | |
| 5054 | sdn_net_counter = 0 |
| 5055 | for net in result: |
| 5056 | if net['sdn_net_id'] != None: |
| 5057 | sdn_net_counter+=1 |
| 5058 | sdn_net_id = net['sdn_net_id'] |
| 5059 | |
| 5060 | if sdn_net_counter == 0: |
| 5061 | return None |
| 5062 | elif sdn_net_counter == 1: |
| 5063 | return sdn_net_id |
| 5064 | else: |
| 5065 | raise NfvoException("More than one SDN network is associated to vim network {}".format( |
| 5066 | network_id), HTTP_Internal_Server_Error) |
| 5067 | |
| 5068 | def get_sdn_controller_id(mydb, datacenter): |
| 5069 | # Obtain sdn controller id |
| 5070 | config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}') |
| 5071 | if not config: |
| 5072 | return None |
| 5073 | |
| 5074 | return yaml.load(config).get('sdn-controller') |
| 5075 | |
| 5076 | def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor): |
| 5077 | try: |
| 5078 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5079 | if not sdn_network_id: |
| 5080 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error) |
| 5081 | |
| 5082 | #Obtain sdn controller id |
| 5083 | controller_id = get_sdn_controller_id(mydb, datacenter) |
| 5084 | if not controller_id: |
| 5085 | raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error) |
| 5086 | |
| 5087 | #Obtain sdn controller info |
| 5088 | sdn_controller = ovim.show_of_controller(controller_id) |
| 5089 | |
| 5090 | port_data = { |
| 5091 | 'name': 'external_port', |
| 5092 | 'net_id': sdn_network_id, |
| 5093 | 'ofc_id': controller_id, |
| 5094 | 'switch_dpid': sdn_controller['dpid'], |
| 5095 | 'switch_port': descriptor['port'] |
| 5096 | } |
| 5097 | |
| 5098 | if 'vlan' in descriptor: |
| 5099 | port_data['vlan'] = descriptor['vlan'] |
| 5100 | if 'mac' in descriptor: |
| 5101 | port_data['mac'] = descriptor['mac'] |
| 5102 | |
| 5103 | result = ovim.new_port(port_data) |
| 5104 | except ovimException as e: |
| 5105 | raise NfvoException("ovimException attaching SDN network {} to vim network {}".format( |
| 5106 | sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error) |
| 5107 | except db_base_Exception as e: |
| 5108 | raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5109 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5110 | |
| 5111 | return 'Port uuid: '+ result |
| 5112 | |
| 5113 | def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None): |
| 5114 | if port_id: |
| 5115 | filter = {'uuid': port_id} |
| 5116 | else: |
| 5117 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5118 | if not sdn_network_id: |
| 5119 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), |
| 5120 | HTTP_Internal_Server_Error) |
| 5121 | #in case no port_id is specified only ports marked as 'external_port' will be detached |
| 5122 | filter = {'name': 'external_port', 'net_id': sdn_network_id} |
| 5123 | |
| 5124 | try: |
| 5125 | port_list = ovim.get_ports(columns={'uuid'}, filter=filter) |
| 5126 | except ovimException as e: |
| 5127 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| 5128 | HTTP_Internal_Server_Error) |
| 5129 | |
| 5130 | if len(port_list) == 0: |
| 5131 | raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id), |
| 5132 | HTTP_Bad_Request) |
| 5133 | |
| 5134 | port_uuid_list = [] |
| 5135 | for port in port_list: |
| 5136 | try: |
| 5137 | port_uuid_list.append(port['uuid']) |
| 5138 | ovim.delete_port(port['uuid']) |
| 5139 | except ovimException as e: |
| 5140 | raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error) |
| 5141 | |
| 5142 | return 'Detached ports uuid: {}'.format(','.join(port_uuid_list)) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5143 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5144 | def vim_action_get(mydb, tenant_id, datacenter, item, name): |
| 5145 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5146 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5147 | filter_dict={} |
| 5148 | if name: |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5149 | if utils.check_valid_uuid(name): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5150 | filter_dict["id"] = name |
| 5151 | else: |
| 5152 | filter_dict["name"] = name |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5153 | try: |
| 5154 | if item=="networks": |
| 5155 | #filter_dict['tenant_id'] = myvim['tenant_id'] |
| 5156 | content = myvim.get_network_list(filter_dict=filter_dict) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5157 | |
| 5158 | if len(content) == 0: |
| 5159 | raise NfvoException("Network {} is not present in the system. ".format(name), |
| 5160 | HTTP_Bad_Request) |
| 5161 | |
| 5162 | #Update the networks with the attached ports |
| 5163 | for net in content: |
| 5164 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id']) |
| 5165 | if sdn_network_id != None: |
| 5166 | try: |
| 5167 | #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id}) |
| 5168 | port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id}) |
| 5169 | except ovimException as e: |
| 5170 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error) |
| 5171 | #Remove field name and if port name is external_port save it as 'type' |
| 5172 | for port in port_list: |
| 5173 | if port['name'] == 'external_port': |
| 5174 | port['type'] = "External" |
| 5175 | del port['name'] |
| 5176 | net['sdn_network_id'] = sdn_network_id |
| 5177 | net['sdn_attached_ports'] = port_list |
| 5178 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5179 | elif item=="tenants": |
| 5180 | content = myvim.get_tenant_list(filter_dict=filter_dict) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5181 | elif item == "images": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5182 | |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5183 | content = myvim.get_image_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5184 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5185 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 5186 | 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] | 5187 | if name and len(content)==1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5188 | return {item[:-1]: content[0]} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5189 | elif name and len(content)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5190 | 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] | 5191 | datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5192 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5193 | return {item: content} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5194 | except vimconn.vimconnException as e: |
| 5195 | 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] | 5196 | 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] | 5197 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5198 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5199 | def vim_action_delete(mydb, tenant_id, datacenter, item, name): |
| 5200 | #get datacenter info |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5201 | if tenant_id == "any": |
| 5202 | tenant_id=None |
| 5203 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5204 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5205 | #get uuid name |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5206 | content = vim_action_get(mydb, tenant_id, datacenter, item, name) |
| 5207 | logger.debug("vim_action_delete vim response: " + str(content)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5208 | items = content.values()[0] |
| 5209 | if type(items)==list and len(items)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5210 | raise NfvoException("Not found " + item, HTTP_Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5211 | elif type(items)==list and len(items)>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5212 | raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5213 | else: # it is a dict |
| 5214 | item_id = items["id"] |
| 5215 | item_name = str(items.get("name")) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5216 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5217 | try: |
| 5218 | if item=="networks": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5219 | # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it |
| 5220 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id) |
| 5221 | if sdn_network_id != None: |
| 5222 | #Delete any port attachment to this network |
| 5223 | try: |
| 5224 | port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id}) |
| 5225 | except ovimException as e: |
| 5226 | raise NfvoException( |
| 5227 | "ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| 5228 | HTTP_Internal_Server_Error) |
| 5229 | |
| 5230 | # By calling one by one all ports to be detached we ensure that not only the external_ports get detached |
| 5231 | for port in port_list: |
| 5232 | vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid']) |
| 5233 | |
| 5234 | #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id |
| 5235 | try: |
| 5236 | mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id}) |
| 5237 | except db_base_Exception as e: |
| 5238 | raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) + |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5239 | str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5240 | |
| 5241 | #Delete the SDN network |
| 5242 | try: |
| 5243 | ovim.delete_network(sdn_network_id) |
| 5244 | except ovimException as e: |
| 5245 | logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True) |
| 5246 | raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), |
| 5247 | HTTP_Internal_Server_Error) |
| 5248 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5249 | content = myvim.delete_network(item_id) |
| 5250 | elif item=="tenants": |
| 5251 | content = myvim.delete_tenant(item_id) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5252 | elif item == "images": |
| 5253 | content = myvim.delete_image(item_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5254 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5255 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5256 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5257 | #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e))) |
| 5258 | 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] | 5259 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5260 | return "{} {} {} deleted".format(item[:-1], item_id,item_name) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5261 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5262 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5263 | def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): |
| 5264 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5265 | logger.debug("vim_action_create descriptor %s", str(descriptor)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5266 | if tenant_id == "any": |
| 5267 | tenant_id=None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5268 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5269 | try: |
| 5270 | if item=="networks": |
| 5271 | net = descriptor["network"] |
| 5272 | net_name = net.pop("name") |
| 5273 | net_type = net.pop("type", "bridge") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 5274 | net_public = net.pop("shared", False) |
| 5275 | net_ipprofile = net.pop("ip_profile", None) |
| tierno | a7d34d0 | 2017-02-23 14:42:07 +0100 | [diff] [blame] | 5276 | net_vlan = net.pop("vlan", None) |
| 5277 | 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] | 5278 | |
| 5279 | #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network |
| 5280 | 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] | 5281 | #obtain datacenter_tenant_id |
| 5282 | datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',), |
| 5283 | FROM='datacenter_tenants', |
| 5284 | WHERE={'datacenter_id': datacenter})[0]['uuid'] |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5285 | try: |
| 5286 | sdn_network = {} |
| 5287 | sdn_network['vlan'] = net_vlan |
| 5288 | sdn_network['type'] = net_type |
| 5289 | sdn_network['name'] = net_name |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5290 | sdn_network['region'] = datacenter_tenant_id |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5291 | ovim_content = ovim.new_network(sdn_network) |
| 5292 | except ovimException as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5293 | logger.error("ovimException creating SDN network={} ".format( |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5294 | sdn_network) + str(e), exc_info=True) |
| 5295 | raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e), |
| 5296 | HTTP_Internal_Server_Error) |
| 5297 | |
| 5298 | # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id |
| 5299 | # use instance_scenario_id=None to distinguish from real instaces of nets |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5300 | correspondence = {'instance_scenario_id': None, |
| 5301 | 'sdn_net_id': ovim_content, |
| 5302 | 'vim_net_id': content, |
| 5303 | 'datacenter_tenant_id': datacenter_tenant_id |
| 5304 | } |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5305 | try: |
| 5306 | mydb.new_row('instance_nets', correspondence, add_uuid=True) |
| 5307 | except db_base_Exception as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5308 | raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5309 | correspondence, e), e.http_code) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5310 | elif item=="tenants": |
| 5311 | tenant = descriptor["tenant"] |
| 5312 | content = myvim.new_tenant(tenant["name"], tenant.get("description")) |
| 5313 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5314 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5315 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5316 | 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] | 5317 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5318 | return vim_action_get(mydb, tenant_id, datacenter, item, content) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5319 | |
| 5320 | def sdn_controller_create(mydb, tenant_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5321 | data = ovim.new_of_controller(sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5322 | logger.debug('New SDN controller created with uuid {}'.format(data)) |
| 5323 | return data |
| 5324 | |
| 5325 | def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5326 | data = ovim.edit_of_controller(controller_id, sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5327 | msg = 'SDN controller {} updated'.format(data) |
| 5328 | logger.debug(msg) |
| 5329 | return msg |
| 5330 | |
| 5331 | def sdn_controller_list(mydb, tenant_id, controller_id=None): |
| 5332 | if controller_id == None: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5333 | data = ovim.get_of_controllers() |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5334 | else: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5335 | data = ovim.show_of_controller(controller_id) |
| 5336 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5337 | msg = 'SDN controller list:\n {}'.format(data) |
| 5338 | logger.debug(msg) |
| 5339 | return data |
| 5340 | |
| 5341 | def sdn_controller_delete(mydb, tenant_id, controller_id): |
| 5342 | select_ = ('uuid', 'config') |
| 5343 | datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_) |
| 5344 | for datacenter in datacenters: |
| 5345 | if datacenter['config']: |
| 5346 | config = yaml.load(datacenter['config']) |
| 5347 | if 'sdn-controller' in config and config['sdn-controller'] == controller_id: |
| 5348 | raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict) |
| 5349 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5350 | data = ovim.delete_of_controller(controller_id) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5351 | msg = 'SDN controller {} deleted'.format(data) |
| 5352 | logger.debug(msg) |
| 5353 | return msg |
| 5354 | |
| 5355 | def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping): |
| 5356 | controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id}) |
| 5357 | if len(controller) < 1: |
| 5358 | raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found) |
| 5359 | |
| 5360 | try: |
| 5361 | sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"] |
| 5362 | except: |
| 5363 | raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request) |
| 5364 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5365 | sdn_controller = ovim.show_of_controller(sdn_controller_id) |
| 5366 | switch_dpid = sdn_controller["dpid"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5367 | |
| 5368 | maps = list() |
| 5369 | for compute_node in sdn_port_mapping: |
| 5370 | #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid} |
| 5371 | element = dict() |
| 5372 | element["compute_node"] = compute_node["compute_node"] |
| 5373 | for port in compute_node["ports"]: |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5374 | pci = port.get("pci") |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5375 | element["switch_port"] = port.get("switch_port") |
| 5376 | element["switch_mac"] = port.get("switch_mac") |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5377 | if not pci or not (element["switch_port"] or element["switch_mac"]): |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5378 | raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'" |
| 5379 | " or 'switch_mac'", HTTP_Bad_Request) |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5380 | for pci_expanded in utils.expand_brackets(pci): |
| 5381 | element["pci"] = pci_expanded |
| 5382 | maps.append(dict(element)) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5383 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5384 | 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] | 5385 | |
| 5386 | def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5387 | maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id}) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5388 | |
| 5389 | result = { |
| 5390 | "sdn-controller": None, |
| 5391 | "datacenter-id": datacenter_id, |
| 5392 | "dpid": None, |
| 5393 | "ports_mapping": list() |
| 5394 | } |
| 5395 | |
| 5396 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id) |
| 5397 | if datacenter['config']: |
| 5398 | config = yaml.load(datacenter['config']) |
| 5399 | if 'sdn-controller' in config: |
| 5400 | controller_id = config['sdn-controller'] |
| 5401 | sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id) |
| 5402 | result["sdn-controller"] = controller_id |
| 5403 | result["dpid"] = sdn_controller["dpid"] |
| 5404 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5405 | if result["sdn-controller"] == None: |
| 5406 | raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request) |
| 5407 | if result["dpid"] == None: |
| 5408 | raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]), |
| 5409 | HTTP_Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5410 | |
| 5411 | if len(maps) == 0: |
| 5412 | return result |
| 5413 | |
| 5414 | ports_correspondence_dict = dict() |
| 5415 | for link in maps: |
| 5416 | if result["sdn-controller"] != link["ofc_id"]: |
| 5417 | raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 5418 | if result["dpid"] != link["switch_dpid"]: |
| 5419 | raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 5420 | element = dict() |
| 5421 | element["pci"] = link["pci"] |
| 5422 | if link["switch_port"]: |
| 5423 | element["switch_port"] = link["switch_port"] |
| 5424 | if link["switch_mac"]: |
| 5425 | element["switch_mac"] = link["switch_mac"] |
| 5426 | |
| 5427 | if not link["compute_node"] in ports_correspondence_dict: |
| 5428 | content = dict() |
| 5429 | content["compute_node"] = link["compute_node"] |
| 5430 | content["ports"] = list() |
| 5431 | ports_correspondence_dict[link["compute_node"]] = content |
| 5432 | |
| 5433 | ports_correspondence_dict[link["compute_node"]]["ports"].append(element) |
| 5434 | |
| 5435 | for key in sorted(ports_correspondence_dict): |
| 5436 | result["ports_mapping"].append(ports_correspondence_dict[key]) |
| 5437 | |
| 5438 | return result |
| 5439 | |
| 5440 | def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id): |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 5441 | return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id}) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5442 | |
| 5443 | def create_RO_keypair(tenant_id): |
| 5444 | """ |
| 5445 | Creates a public / private keys for a RO tenant and returns their values |
| 5446 | Params: |
| 5447 | tenant_id: ID of the tenant |
| 5448 | Return: |
| 5449 | public_key: Public key for the RO tenant |
| 5450 | private_key: Encrypted private key for RO tenant |
| 5451 | """ |
| 5452 | |
| 5453 | bits = 2048 |
| 5454 | key = RSA.generate(bits) |
| 5455 | try: |
| 5456 | public_key = key.publickey().exportKey('OpenSSH') |
| 5457 | if isinstance(public_key, ValueError): |
| 5458 | raise NfvoException("Unable to create public key: {}".format(public_key), HTTP_Internal_Server_Error) |
| 5459 | private_key = key.exportKey(passphrase=tenant_id, pkcs=8) |
| 5460 | except (ValueError, NameError) as e: |
| 5461 | raise NfvoException("Unable to create private key: {}".format(e), HTTP_Internal_Server_Error) |
| 5462 | return public_key, private_key |
| 5463 | |
| 5464 | def decrypt_key (key, tenant_id): |
| 5465 | """ |
| 5466 | Decrypts an encrypted RSA key |
| 5467 | Params: |
| 5468 | key: Private key to be decrypted |
| 5469 | tenant_id: ID of the tenant |
| 5470 | Return: |
| 5471 | unencrypted_key: Unencrypted private key for RO tenant |
| 5472 | """ |
| 5473 | try: |
| 5474 | key = RSA.importKey(key,tenant_id) |
| 5475 | unencrypted_key = key.exportKey('PEM') |
| 5476 | if isinstance(unencrypted_key, ValueError): |
| 5477 | raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), HTTP_Internal_Server_Error) |
| 5478 | except ValueError as e: |
| 5479 | raise NfvoException("Unable to decrypt the private key: {}".format(e), HTTP_Internal_Server_Error) |
| 5480 | return unencrypted_key |