| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| 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 | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 34 | import vim_thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 35 | 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] | 36 | HTTP_Conflict, HTTP_Method_Not_Allowed |
| 37 | import console_proxy_thread as cli |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 38 | import vimconn |
| 39 | import logging |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 40 | import collections |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 41 | import math |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 42 | from uuid import uuid4 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 43 | from db_base import db_base_Exception |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 44 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 45 | import nfvo_db |
| 46 | from threading import Lock |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 47 | import time as t |
| tierno | 01b3e17 | 2017-04-21 10:52:34 +0200 | [diff] [blame] | 48 | from lib_osm_openvim import ovim as ovim_module |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 49 | from lib_osm_openvim.ovim import ovimException |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 50 | from Crypto.PublicKey import RSA |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 51 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 52 | import osm_im.vnfd as vnfd_catalog |
| 53 | import osm_im.nsd as nsd_catalog |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 54 | from pyangbind.lib.serialise import pybindJSONDecoder |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 55 | from copy import deepcopy |
| 56 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 57 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 58 | global global_config |
| 59 | global vimconn_imported |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 60 | global logger |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 61 | global default_volume_size |
| 62 | default_volume_size = '5' #size in GB |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 63 | global ovim |
| 64 | ovim = None |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 65 | global_config = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 66 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 67 | vimconn_imported = {} # dictionary with VIM type as key, loaded module as value |
| 68 | vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 69 | vim_persistent_info = {} |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 70 | logger = logging.getLogger('openmano.nfvo') |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 71 | task_lock = Lock() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 72 | last_task_id = 0.0 |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 73 | db = None |
| 74 | db_lock = Lock() |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 75 | |
| 76 | class NfvoException(Exception): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 77 | def __init__(self, message, http_code): |
| 78 | self.http_code = http_code |
| 79 | Exception.__init__(self, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 80 | |
| 81 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 82 | def get_task_id(): |
| 83 | global last_task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 84 | task_id = t.time() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 85 | if task_id <= last_task_id: |
| 86 | task_id = last_task_id + 0.000001 |
| 87 | last_task_id = task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 88 | return "ACTION-{:.6f}".format(task_id) |
| 89 | # 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] | 90 | |
| 91 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 92 | def new_task(name, params, depends=None): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 93 | """Deprected!!!""" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 94 | task_id = get_task_id() |
| 95 | task = {"status": "enqueued", "id": task_id, "name": name, "params": params} |
| 96 | if depends: |
| 97 | task["depends"] = depends |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 98 | return task |
| 99 | |
| 100 | |
| 101 | def is_task_id(id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 102 | return True if id[:5] == "TASK-" else False |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 103 | |
| 104 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 105 | def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id): |
| 106 | name = datacenter_name[:16] |
| 107 | if name not in vim_threads["names"]: |
| 108 | vim_threads["names"].append(name) |
| 109 | return name |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 110 | name = datacenter_name[:16] + "." + tenant_name[:16] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 111 | if name not in vim_threads["names"]: |
| 112 | vim_threads["names"].append(name) |
| 113 | return name |
| 114 | name = datacenter_id + "-" + tenant_id |
| 115 | vim_threads["names"].append(name) |
| 116 | return name |
| 117 | |
| 118 | |
| 119 | def start_service(mydb): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 120 | global db, global_config |
| 121 | db = nfvo_db.nfvo_db() |
| 122 | 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] | 123 | global ovim |
| 124 | |
| 125 | # Initialize openvim for SDN control |
| 126 | # TODO: Avoid static configuration by adding new parameters to openmanod.cfg |
| 127 | # TODO: review ovim.py to delete not needed configuration |
| 128 | ovim_configuration = { |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 129 | 'logger_name': 'openmano.ovim', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 130 | 'network_vlan_range_start': 1000, |
| 131 | 'network_vlan_range_end': 4096, |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 132 | 'db_name': global_config["db_ovim_name"], |
| 133 | 'db_host': global_config["db_ovim_host"], |
| 134 | 'db_user': global_config["db_ovim_user"], |
| 135 | 'db_passwd': global_config["db_ovim_passwd"], |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 136 | 'bridge_ifaces': {}, |
| 137 | 'mode': 'normal', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 138 | 'network_type': 'bridge', |
| 139 | #TODO: log_level_of should not be needed. To be modified in ovim |
| 140 | 'log_level_of': 'DEBUG' |
| 141 | } |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 142 | try: |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 143 | # starts ovim library |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 144 | ovim = ovim_module.ovim(ovim_configuration) |
| 145 | ovim.start_service() |
| 146 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 147 | #delete old unneeded vim_actions |
| 148 | clean_db(mydb) |
| 149 | |
| 150 | # starts vim_threads |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 151 | from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\ |
| 152 | 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid' |
| 153 | select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin', |
| 154 | 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id', |
| 155 | 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id', |
| 156 | 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id') |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 157 | vims = mydb.get_rows(FROM=from_, SELECT=select_) |
| 158 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 159 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 160 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 161 | if vim["config"]: |
| 162 | extra.update(yaml.load(vim["config"])) |
| 163 | if vim.get('dt_config'): |
| 164 | extra.update(yaml.load(vim["dt_config"])) |
| 165 | if vim["type"] not in vimconn_imported: |
| 166 | module_info=None |
| 167 | try: |
| 168 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 169 | pkg = __import__("osm_ro." + module) |
| 170 | vim_conn = getattr(pkg, module) |
| 171 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 172 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 173 | vimconn_imported[vim["type"]] = vim_conn |
| 174 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 175 | # if module_info and module_info[0]: |
| 176 | # file.close(module_info[0]) |
| tierno | cdee8cc | 2017-04-25 13:42:06 +0200 | [diff] [blame] | 177 | raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 178 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 179 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 180 | thread_id = vim['datacenter_tenant_id'] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 181 | vim_persistent_info[thread_id] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 182 | try: |
| 183 | #if not tenant: |
| 184 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 185 | myvim = vimconn_imported[ vim["type"] ].vimconnector( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 186 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| 187 | tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'], |
| 188 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| 189 | user=vim['user'], passwd=vim['passwd'], |
| 190 | config=extra, persistent_info=vim_persistent_info[thread_id] |
| 191 | ) |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 192 | except vimconn.vimconnException as e: |
| 193 | myvim = e |
| 194 | logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'], |
| 195 | vim['datacenter_id'], e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 196 | except Exception as e: |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 197 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e), |
| 198 | HTTP_Internal_Server_Error) |
| 199 | thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], |
| 200 | vim['vim_tenant_id']) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 201 | new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'], |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 202 | vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 203 | new_thread.start() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 204 | vim_threads["running"][thread_id] = new_thread |
| 205 | except db_base_Exception as e: |
| 206 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 207 | except ovim_module.ovimException as e: |
| 208 | message = str(e) |
| 209 | if message[:22] == "DATABASE wrong version": |
| 210 | message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\ |
| 211 | "at host {dbhost}".format( |
| 212 | msg=message[22:-3], dbname=global_config["db_ovim_name"], |
| 213 | dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"], |
| 214 | ver=message[-3:-1], dbhost=global_config["db_ovim_host"]) |
| 215 | raise NfvoException(message, HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 216 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 217 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 218 | def stop_service(): |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 219 | global ovim, global_config |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 220 | if ovim: |
| 221 | ovim.stop_service() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 222 | for thread_id,thread in vim_threads["running"].items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 223 | thread.insert_task("exit") |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 224 | vim_threads["deleting"][thread_id] = thread |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 225 | vim_threads["running"] = {} |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 226 | if global_config and global_config.get("console_thread"): |
| 227 | for thread in global_config["console_thread"]: |
| 228 | thread.terminate = True |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 229 | |
| tierno | 6ddeded | 2017-05-16 15:40:26 +0200 | [diff] [blame] | 230 | def get_version(): |
| 231 | return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"], |
| 232 | global_config["version_date"] )) |
| 233 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 234 | def clean_db(mydb): |
| 235 | """ |
| 236 | Clean unused or old entries at database to avoid unlimited growing |
| 237 | :param mydb: database connector |
| 238 | :return: None |
| 239 | """ |
| 240 | # get and delete unused vim_actions: all elements deleted, one week before, instance not present |
| 241 | now = t.time()-3600*24*7 |
| 242 | instance_action_id = None |
| 243 | nb_deleted = 0 |
| 244 | while True: |
| 245 | actions_to_delete = mydb.get_rows( |
| 246 | SELECT=("item", "item_id", "instance_action_id"), |
| 247 | FROM="vim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid " |
| 248 | "left join instance_scenarios as i on ia.instance_id=i.uuid", |
| 249 | WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None, |
| 250 | "va.status": ("DONE", "SUPERSEDED")}, |
| 251 | LIMIT=100 |
| 252 | ) |
| 253 | for to_delete in actions_to_delete: |
| 254 | mydb.delete_row(FROM="vim_actions", WHERE=to_delete) |
| 255 | if instance_action_id != to_delete["instance_action_id"]: |
| 256 | instance_action_id = to_delete["instance_action_id"] |
| 257 | mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id}) |
| 258 | nb_deleted += len(actions_to_delete) |
| 259 | if len(actions_to_delete) < 100: |
| 260 | break |
| 261 | if nb_deleted: |
| 262 | logger.debug("Removed {} unused vim_actions".format(nb_deleted)) |
| 263 | |
| 264 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 265 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 266 | def get_flavorlist(mydb, vnf_id, nfvo_tenant=None): |
| 267 | '''Obtain flavorList |
| 268 | return result, content: |
| 269 | <0, error_text upon error |
| 270 | nb_records, flavor_list on success |
| 271 | ''' |
| 272 | WHERE_dict={} |
| 273 | WHERE_dict['vnf_id'] = vnf_id |
| 274 | if nfvo_tenant is not None: |
| 275 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 276 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 277 | #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict ) |
| 278 | #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] | 279 | flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict ) |
| 280 | #print "get_flavor_list result:", result |
| 281 | #print "get_flavor_list content:", content |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 282 | flavorList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 283 | for flavor in flavors: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 284 | flavorList.append(flavor['flavor_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 285 | return flavorList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 286 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 287 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 288 | def get_imagelist(mydb, vnf_id, nfvo_tenant=None): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 289 | """ |
| 290 | Get used images of all vms belonging to this VNFD |
| 291 | :param mydb: database conector |
| 292 | :param vnf_id: vnfd uuid |
| 293 | :param nfvo_tenant: tenant, not used |
| 294 | :return: The list of image uuid used |
| 295 | """ |
| 296 | image_list = [] |
| 297 | vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id}) |
| 298 | for vm in vms: |
| 299 | if vm["image_id"] not in image_list: |
| 300 | image_list.append(vm["image_id"]) |
| 301 | if vm["image_list"]: |
| 302 | vm_image_list = yaml.load(vm["image_list"]) |
| 303 | for image_dict in vm_image_list: |
| 304 | if image_dict["image_id"] not in image_list: |
| 305 | image_list.append(image_dict["image_id"]) |
| 306 | return image_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 307 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 308 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 309 | 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] | 310 | 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] | 311 | '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 312 | return dictionary with {datacenter_id: vim_class, ... }. vim_class contain: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 313 | '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] | 314 | raise exception upon error |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 315 | ''' |
| 316 | WHERE_dict={} |
| 317 | if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| 318 | if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 319 | 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] | 320 | if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name |
| 321 | 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] | 322 | if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name |
| 323 | 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] | 324 | 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] | 325 | 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] | 326 | '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] | 327 | 'user','passwd', 'dt.config as dt_config') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 328 | else: |
| 329 | from_ = 'datacenters as d' |
| 330 | 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] | 331 | try: |
| 332 | vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict ) |
| 333 | vim_dict={} |
| 334 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 335 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 336 | 'datacenter_id': vim.get('datacenter_id'), |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 337 | '_vim_type_internal': vim.get('type')} |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 338 | if vim["config"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 339 | extra.update(yaml.load(vim["config"])) |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 340 | if vim.get('dt_config'): |
| 341 | extra.update(yaml.load(vim["dt_config"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 342 | if vim["type"] not in vimconn_imported: |
| 343 | module_info=None |
| 344 | try: |
| 345 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 346 | pkg = __import__("osm_ro." + module) |
| 347 | vim_conn = getattr(pkg, module) |
| 348 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 349 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 350 | vimconn_imported[vim["type"]] = vim_conn |
| 351 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 352 | # if module_info and module_info[0]: |
| 353 | # file.close(module_info[0]) |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 354 | if ignore_errors: |
| 355 | logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 356 | vim["type"], module, type(e).__name__, str(e))) |
| 357 | continue |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 358 | raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 359 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 360 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 361 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 362 | if 'datacenter_tenant_id' in vim: |
| 363 | thread_id = vim["datacenter_tenant_id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 364 | if thread_id not in vim_persistent_info: |
| 365 | vim_persistent_info[thread_id] = {} |
| 366 | persistent_info = vim_persistent_info[thread_id] |
| 367 | else: |
| 368 | persistent_info = {} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 369 | #if not tenant: |
| 370 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 371 | vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector( |
| 372 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 373 | tenant_id=vim.get('vim_tenant_id',vim_tenant), |
| 374 | tenant_name=vim.get('vim_tenant_name',vim_tenant_name), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 375 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 376 | user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd), |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 377 | config=extra, persistent_info=persistent_info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 378 | ) |
| 379 | except Exception as e: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 380 | if ignore_errors: |
| 381 | logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e))) |
| 382 | continue |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 383 | http_code = HTTP_Internal_Server_Error |
| 384 | if isinstance(e, vimconn.vimconnException): |
| 385 | http_code = e.http_code |
| 386 | 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] | 387 | return vim_dict |
| 388 | except db_base_Exception as e: |
| 389 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 390 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 391 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 392 | def rollback(mydb, vims, rollback_list): |
| 393 | undeleted_items=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 394 | #delete things by reverse order |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 395 | for i in range(len(rollback_list)-1, -1, -1): |
| 396 | item = rollback_list[i] |
| 397 | if item["where"]=="vim": |
| 398 | if item["vim_id"] not in vims: |
| 399 | continue |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 400 | if is_task_id(item["uuid"]): |
| 401 | continue |
| 402 | vim = vims[item["vim_id"]] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 403 | try: |
| 404 | if item["what"]=="image": |
| 405 | vim.delete_image(item["uuid"]) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 406 | 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] | 407 | elif item["what"]=="flavor": |
| 408 | vim.delete_flavor(item["uuid"]) |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 409 | 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] | 410 | elif item["what"]=="network": |
| 411 | vim.delete_network(item["uuid"]) |
| 412 | elif item["what"]=="vm": |
| 413 | vim.delete_vminstance(item["uuid"]) |
| 414 | except vimconn.vimconnException as e: |
| 415 | logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e)) |
| 416 | undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 417 | except db_base_Exception as e: |
| 418 | 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] | 419 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 420 | else: # where==mano |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 421 | try: |
| 422 | if item["what"]=="image": |
| 423 | mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]}) |
| 424 | elif item["what"]=="flavor": |
| 425 | mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]}) |
| 426 | except db_base_Exception as e: |
| 427 | logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e)) |
| 428 | undeleted_items.append("{} '{}'".format(item['what'], item["uuid"])) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 429 | if len(undeleted_items)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 430 | return True," Rollback successful." |
| 431 | else: |
| 432 | return False," Rollback fails to delete: " + str(undeleted_items) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 433 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 434 | |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 435 | def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 436 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 437 | #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 438 | vnfc_interfaces={} |
| 439 | for vnfc in vnf_descriptor["vnf"]["VNFC"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 440 | name_dict = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 441 | #dataplane interfaces |
| 442 | for numa in vnfc.get("numas",() ): |
| 443 | for interface in numa.get("interfaces",()): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 444 | if interface["name"] in name_dict: |
| 445 | raise NfvoException( |
| 446 | "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format( |
| 447 | vnfc["name"], interface["name"]), |
| 448 | HTTP_Bad_Request) |
| 449 | name_dict[ interface["name"] ] = "underlay" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 450 | #bridge interfaces |
| 451 | for interface in vnfc.get("bridge-ifaces",() ): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 452 | if interface["name"] in name_dict: |
| 453 | raise NfvoException( |
| 454 | "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format( |
| 455 | vnfc["name"], interface["name"]), |
| 456 | HTTP_Bad_Request) |
| 457 | name_dict[ interface["name"] ] = "overlay" |
| 458 | vnfc_interfaces[ vnfc["name"] ] = name_dict |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 459 | # check bood-data info |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 460 | # if "boot-data" in vnfc: |
| 461 | # # check that user-data is incompatible with users and config-files |
| 462 | # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"): |
| 463 | # raise NfvoException( |
| 464 | # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'", |
| 465 | # HTTP_Bad_Request) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 466 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 467 | #check if the info in external_connections matches with the one in the vnfcs |
| 468 | name_list=[] |
| 469 | for external_connection in vnf_descriptor["vnf"].get("external-connections",() ): |
| 470 | if external_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 471 | raise NfvoException( |
| 472 | "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format( |
| 473 | external_connection["name"]), |
| 474 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 475 | name_list.append(external_connection["name"]) |
| 476 | if external_connection["VNFC"] not in vnfc_interfaces: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 477 | raise NfvoException( |
| 478 | "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format( |
| 479 | external_connection["name"], external_connection["VNFC"]), |
| 480 | HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 481 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 482 | if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 483 | raise NfvoException( |
| 484 | "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 485 | external_connection["name"], |
| 486 | external_connection["local_iface_name"]), |
| 487 | HTTP_Bad_Request ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 488 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 489 | #check if the info in internal_connections matches with the one in the vnfcs |
| 490 | name_list=[] |
| 491 | for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ): |
| 492 | if internal_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 493 | raise NfvoException( |
| 494 | "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format( |
| 495 | internal_connection["name"]), |
| 496 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 497 | name_list.append(internal_connection["name"]) |
| 498 | #We should check that internal-connections of type "ptp" have only 2 elements |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 499 | |
| 500 | if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"): |
| 501 | raise NfvoException( |
| 502 | "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format( |
| 503 | internal_connection["name"], |
| 504 | 'ptp' if vnf_descriptor_version==1 else 'e-line', |
| 505 | 'data' if vnf_descriptor_version==1 else "e-lan"), |
| 506 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 507 | for port in internal_connection["elements"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 508 | vnf = port["VNFC"] |
| 509 | iface = port["local_iface_name"] |
| 510 | if vnf not in vnfc_interfaces: |
| 511 | raise NfvoException( |
| 512 | "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format( |
| 513 | internal_connection["name"], vnf), |
| 514 | HTTP_Bad_Request) |
| 515 | if iface not in vnfc_interfaces[ vnf ]: |
| 516 | raise NfvoException( |
| 517 | "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 518 | internal_connection["name"], iface), |
| 519 | HTTP_Bad_Request) |
| 520 | return -HTTP_Bad_Request, |
| 521 | if vnf_descriptor_version==1 and "type" not in internal_connection: |
| 522 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 523 | internal_connection["type"] = "bridge" |
| 524 | else: |
| 525 | internal_connection["type"] = "data" |
| 526 | if vnf_descriptor_version==2 and "implementation" not in internal_connection: |
| 527 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 528 | internal_connection["implementation"] = "overlay" |
| 529 | else: |
| 530 | internal_connection["implementation"] = "underlay" |
| 531 | if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \ |
| 532 | internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay": |
| 533 | raise NfvoException( |
| 534 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 535 | internal_connection["name"], |
| 536 | iface, 'bridge' if vnf_descriptor_version==1 else 'overlay', |
| 537 | 'data' if vnf_descriptor_version==1 else 'underlay'), |
| 538 | HTTP_Bad_Request) |
| 539 | if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \ |
| 540 | vnfc_interfaces[vnf][iface] == "underlay": |
| 541 | raise NfvoException( |
| 542 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 543 | internal_connection["name"], iface, |
| 544 | 'data' if vnf_descriptor_version==1 else 'underlay', |
| 545 | 'bridge' if vnf_descriptor_version==1 else 'overlay'), |
| 546 | HTTP_Bad_Request) |
| 547 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 548 | |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 549 | 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] | 550 | #look if image exist |
| 551 | if only_create_at_vim: |
| 552 | image_mano_id = image_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 553 | if return_on_error == None: |
| 554 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 555 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 556 | if image_dict['location']: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 557 | images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']}) |
| 558 | else: |
| 559 | 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] | 560 | if len(images)>=1: |
| 561 | image_mano_id = images[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 562 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 563 | #create image in MANO DB |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 564 | temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None), |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 565 | 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None), |
| 566 | 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 567 | } |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 568 | #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] | 569 | image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True) |
| 570 | rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 571 | #create image at every vim |
| 572 | for vim_id,vim in vims.iteritems(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 573 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 574 | image_created="false" |
| 575 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 576 | image_db = mydb.get_rows(FROM="datacenters_images", |
| 577 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 578 | #look at VIM if this image exist |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 579 | try: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 580 | if image_dict['location'] is not None: |
| 581 | image_vim_id = vim.get_image_id_from_path(image_dict['location']) |
| 582 | else: |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 583 | filter_dict = {} |
| 584 | filter_dict['name'] = image_dict['universal_name'] |
| 585 | if image_dict.get('checksum') != None: |
| 586 | filter_dict['checksum'] = image_dict['checksum'] |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 587 | #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 588 | vim_images = vim.get_image_list(filter_dict) |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 589 | #logger.debug('>>>>>>>> VIM images: %s', str(vim_images)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 590 | if len(vim_images) > 1: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 591 | 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] | 592 | elif len(vim_images) == 0: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 593 | 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] | 594 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 595 | #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0])) |
| 596 | image_vim_id = vim_images[0]['id'] |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 597 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 598 | except vimconn.vimconnNotFoundException as e: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 599 | #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] | 600 | try: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 601 | #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None |
| 602 | if image_dict['location']: |
| 603 | image_vim_id = vim.new_image(image_dict) |
| 604 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id}) |
| 605 | image_created="true" |
| 606 | else: |
| garciadeblas | b6153a2 | 2017-02-06 15:38:33 +0100 | [diff] [blame] | 607 | #If we reach this point, then the image has image name, and optionally checksum, and could not be found |
| 608 | raise vimconn.vimconnException(str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 609 | except vimconn.vimconnException as e: |
| 610 | if return_on_error: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 611 | logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 612 | raise |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 613 | image_vim_id = None |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 614 | logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 615 | continue |
| 616 | except vimconn.vimconnException as e: |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 617 | if return_on_error: |
| 618 | logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e)) |
| 619 | raise |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 620 | 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] | 621 | image_vim_id = None |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 622 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 623 | #if we reach here, the image has been created or existed |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 624 | if len(image_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 625 | #add new vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 626 | mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id, |
| 627 | 'image_id':image_mano_id, |
| 628 | 'vim_id': image_vim_id, |
| 629 | 'created':image_created}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 630 | elif image_db[0]["vim_id"]!=image_vim_id: |
| 631 | #modify existing vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 632 | 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] | 633 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 634 | return image_vim_id if only_create_at_vim else image_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 635 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 636 | |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 637 | 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] | 638 | temp_flavor_dict= {'disk':flavor_dict.get('disk',0), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 639 | 'ram':flavor_dict.get('ram'), |
| 640 | 'vcpus':flavor_dict.get('vcpus'), |
| 641 | } |
| 642 | if 'extended' in flavor_dict and flavor_dict['extended']==None: |
| 643 | del flavor_dict['extended'] |
| 644 | if 'extended' in flavor_dict: |
| 645 | temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256) |
| 646 | |
| 647 | #look if flavor exist |
| 648 | if only_create_at_vim: |
| 649 | flavor_mano_id = flavor_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 650 | if return_on_error == None: |
| 651 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 652 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 653 | flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 654 | if len(flavors)>=1: |
| 655 | flavor_mano_id = flavors[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 656 | else: |
| 657 | #create flavor |
| 658 | #create one by one the images of aditional disks |
| 659 | dev_image_list=[] #list of images |
| 660 | if 'extended' in flavor_dict and flavor_dict['extended']!=None: |
| 661 | dev_nb=0 |
| 662 | for device in flavor_dict['extended'].get('devices',[]): |
| garciadeblas | 41f18be | 2016-10-04 09:09:58 +0200 | [diff] [blame] | 663 | if "image" not in device and "image name" not in device: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 664 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 665 | image_dict={} |
| 666 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 667 | image_dict['universal_name']=device.get('image name') |
| 668 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 669 | image_dict['location']=device.get('image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 670 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 671 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 672 | image_metadata_dict = device.get('image metadata', None) |
| 673 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 674 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 675 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 676 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 677 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 678 | #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] | 679 | dev_image_list.append(image_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 680 | dev_nb += 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 681 | temp_flavor_dict['name'] = flavor_dict['name'] |
| 682 | temp_flavor_dict['description'] = flavor_dict.get('description',None) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 683 | content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True) |
| 684 | flavor_mano_id= content |
| 685 | rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 686 | #create flavor at every vim |
| 687 | if 'uuid' in flavor_dict: |
| 688 | del flavor_dict['uuid'] |
| 689 | flavor_vim_id=None |
| 690 | for vim_id,vim in vims.items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 691 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 692 | flavor_created="false" |
| 693 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 694 | flavor_db = mydb.get_rows(FROM="datacenters_flavors", |
| 695 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 696 | #look at VIM if this flavor exist SKIPPED |
| 697 | #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location']) |
| 698 | #if res_vim < 0: |
| 699 | # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id |
| 700 | # continue |
| 701 | #elif res_vim==0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 702 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 703 | # Create the flavor in VIM |
| 704 | # Translate images at devices from MANO id to VIM id |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 705 | disk_list = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 706 | 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] | 707 | # make a copy of original devices |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 708 | devices_original=[] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 709 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 710 | for device in flavor_dict["extended"].get("devices",[]): |
| 711 | dev={} |
| 712 | dev.update(device) |
| 713 | devices_original.append(dev) |
| 714 | if 'image' in device: |
| 715 | del device['image'] |
| 716 | if 'image metadata' in device: |
| 717 | del device['image metadata'] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 718 | if 'image checksum' in device: |
| 719 | del device['image checksum'] |
| 720 | dev_nb = 0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 721 | for index in range(0,len(devices_original)) : |
| 722 | device=devices_original[index] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 723 | if "image" not in device and "image name" not in device: |
| 724 | if 'size' in device: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 725 | 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] | 726 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 727 | image_dict={} |
| 728 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 729 | image_dict['universal_name']=device.get('image name') |
| 730 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 731 | image_dict['location']=device.get('image') |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 732 | # image_dict['new_location']=device.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 733 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 734 | image_metadata_dict = device.get('image metadata', None) |
| 735 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 736 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 737 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 738 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 739 | 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] | 740 | image_dict["uuid"]=image_mano_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 741 | 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] | 742 | |
| 743 | #save disk information (image must be based on and size |
| 744 | disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)}) |
| 745 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 746 | flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id |
| 747 | dev_nb += 1 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 748 | if len(flavor_db)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 749 | #check that this vim_id exist in VIM, if not create |
| 750 | flavor_vim_id=flavor_db[0]["vim_id"] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 751 | try: |
| 752 | vim.get_flavor(flavor_vim_id) |
| 753 | continue #flavor exist |
| 754 | except vimconn.vimconnException: |
| 755 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 756 | #create flavor at vim |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 757 | logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"]) |
| 758 | try: |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 759 | flavor_vim_id = None |
| 760 | flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict) |
| 761 | flavor_create="false" |
| 762 | except vimconn.vimconnException as e: |
| 763 | pass |
| 764 | try: |
| 765 | if not flavor_vim_id: |
| 766 | flavor_vim_id = vim.new_flavor(flavor_dict) |
| 767 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id}) |
| 768 | flavor_created="true" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 769 | except vimconn.vimconnException as e: |
| 770 | if return_on_error: |
| 771 | logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 772 | raise |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 773 | logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 774 | flavor_vim_id = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 775 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 776 | #if reach here the flavor has been create or exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 777 | if len(flavor_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 778 | #add new vim_id at datacenters_flavors |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 779 | extended_devices_yaml = None |
| 780 | if len(disk_list) > 0: |
| 781 | extended_devices = dict() |
| 782 | extended_devices['disks'] = disk_list |
| 783 | extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256) |
| 784 | mydb.new_row('datacenters_flavors', |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 785 | {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id, |
| 786 | 'created': flavor_created, 'extended': extended_devices_yaml}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 787 | elif flavor_db[0]["vim_id"]!=flavor_vim_id: |
| 788 | #modify existing vim_id at datacenters_flavors |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 789 | mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, |
| 790 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 791 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 792 | return flavor_vim_id if only_create_at_vim else flavor_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 793 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 794 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 795 | def get_str(obj, field, length): |
| 796 | """ |
| 797 | Obtain the str value, |
| 798 | :param obj: |
| 799 | :param length: |
| 800 | :return: |
| 801 | """ |
| 802 | value = obj.get(field) |
| 803 | if value is not None: |
| 804 | value = str(value)[:length] |
| 805 | return value |
| 806 | |
| 807 | def _lookfor_or_create_image(db_image, mydb, descriptor): |
| 808 | """ |
| 809 | fill image content at db_image dictionary. Check if the image with this image and checksum exist |
| 810 | :param db_image: dictionary to insert data |
| 811 | :param mydb: database connector |
| 812 | :param descriptor: yang descriptor |
| 813 | :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image |
| 814 | """ |
| 815 | |
| 816 | db_image["name"] = get_str(descriptor, "image", 255) |
| 817 | db_image["checksum"] = get_str(descriptor, "image-checksum", 32) |
| 818 | if not db_image["checksum"]: # Ensure that if empty string, None is stored |
| 819 | db_image["checksum"] = None |
| 820 | if db_image["name"].startswith("/"): |
| 821 | db_image["location"] = db_image["name"] |
| 822 | existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]}) |
| 823 | else: |
| 824 | db_image["universal_name"] = db_image["name"] |
| 825 | existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'], |
| 826 | 'checksum': db_image['checksum']}) |
| 827 | if existing_images: |
| 828 | return existing_images[0]["uuid"] |
| 829 | else: |
| 830 | image_uuid = str(uuid4()) |
| 831 | db_image["uuid"] = image_uuid |
| 832 | return None |
| 833 | |
| 834 | def new_vnfd_v3(mydb, tenant_id, vnf_descriptor): |
| 835 | """ |
| 836 | Parses an OSM IM vnfd_catalog and insert at DB |
| 837 | :param mydb: |
| 838 | :param tenant_id: |
| 839 | :param vnf_descriptor: |
| 840 | :return: The list of cretated vnf ids |
| 841 | """ |
| 842 | try: |
| 843 | myvnfd = vnfd_catalog.vnfd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 844 | try: |
| tierno | ad6bdd4 | 2018-01-10 10:43:46 +0100 | [diff] [blame] | 845 | pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True) |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 846 | except Exception as e: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 847 | raise NfvoException("Error. Invalid VNF descriptor format " + str(e), HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 848 | db_vnfs = [] |
| 849 | db_nets = [] |
| 850 | db_vms = [] |
| 851 | db_vms_index = 0 |
| 852 | db_interfaces = [] |
| 853 | db_images = [] |
| 854 | db_flavors = [] |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 855 | db_ip_profiles_index = 0 |
| 856 | db_ip_profiles = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 857 | uuid_list = [] |
| 858 | vnfd_uuid_list = [] |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 859 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog") |
| 860 | if not vnfd_catalog_descriptor: |
| 861 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog") |
| 862 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd") |
| 863 | if not vnfd_descriptor_list: |
| 864 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd") |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 865 | for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues(): |
| 866 | vnfd = vnfd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 867 | |
| 868 | # table vnf |
| 869 | vnf_uuid = str(uuid4()) |
| 870 | uuid_list.append(vnf_uuid) |
| 871 | vnfd_uuid_list.append(vnf_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 872 | vnfd_id = get_str(vnfd, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 873 | db_vnf = { |
| 874 | "uuid": vnf_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 875 | "osm_id": vnfd_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 876 | "name": get_str(vnfd, "name", 255), |
| 877 | "description": get_str(vnfd, "description", 255), |
| 878 | "tenant_id": tenant_id, |
| 879 | "vendor": get_str(vnfd, "vendor", 255), |
| 880 | "short_name": get_str(vnfd, "short-name", 255), |
| 881 | "descriptor": str(vnf_descriptor)[:60000] |
| 882 | } |
| 883 | |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 884 | for vnfd_descriptor in vnfd_descriptor_list: |
| 885 | if vnfd_descriptor["id"] == str(vnfd["id"]): |
| 886 | break |
| 887 | |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 888 | # table ip_profiles (ip-profiles) |
| 889 | ip_profile_name2db_table_index = {} |
| 890 | for ip_profile in vnfd.get("ip-profiles").itervalues(): |
| 891 | db_ip_profile = { |
| 892 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 893 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 894 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 895 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 896 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 897 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 898 | } |
| 899 | dns_list = [] |
| 900 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 901 | dns_list.append(str(dns.get("address"))) |
| 902 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 903 | if ip_profile["ip-profile-params"].get('security-group'): |
| 904 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 905 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 906 | db_ip_profiles_index += 1 |
| 907 | db_ip_profiles.append(db_ip_profile) |
| 908 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 909 | # table nets (internal-vld) |
| 910 | net_id2uuid = {} # for mapping interface with network |
| 911 | for vld in vnfd.get("internal-vld").itervalues(): |
| 912 | net_uuid = str(uuid4()) |
| 913 | uuid_list.append(net_uuid) |
| 914 | db_net = { |
| 915 | "name": get_str(vld, "name", 255), |
| 916 | "vnf_id": vnf_uuid, |
| 917 | "uuid": net_uuid, |
| 918 | "description": get_str(vld, "description", 255), |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 919 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 920 | "type": "bridge", # TODO adjust depending on connection point type |
| 921 | } |
| 922 | net_id2uuid[vld.get("id")] = net_uuid |
| 923 | db_nets.append(db_net) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 924 | # ip-profile, link db_ip_profile with db_sce_net |
| 925 | if vld.get("ip-profile-ref"): |
| 926 | ip_profile_name = vld.get("ip-profile-ref") |
| 927 | if ip_profile_name not in ip_profile_name2db_table_index: |
| 928 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':" |
| 929 | "'{}'. Reference to a non-existing 'ip_profiles'".format( |
| 930 | str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| 931 | HTTP_Bad_Request) |
| 932 | db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid |
| 933 | else: #check no ip-address has been defined |
| tierno | 45140f5 | 2018-03-26 12:11:46 +0200 | [diff] [blame] | 934 | for icp in vld.get("internal-connection-point").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 935 | if icp.get("ip-address"): |
| 936 | raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' " |
| 937 | "contains an ip-address but no ip-profile has been defined at VLD".format( |
| 938 | str(vnfd["id"]), str(vld["id"]), str(icp["id"])), |
| 939 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 940 | |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 941 | # connection points vaiable declaration |
| 942 | cp_name2iface_uuid = {} |
| 943 | cp_name2vm_uuid = {} |
| 944 | cp_name2db_interface = {} |
| 945 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 946 | # table vms (vdus) |
| 947 | vdu_id2uuid = {} |
| 948 | vdu_id2db_table_index = {} |
| 949 | for vdu in vnfd.get("vdu").itervalues(): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 950 | |
| 951 | for vdu_descriptor in vnfd_descriptor["vdu"]: |
| 952 | if vdu_descriptor["id"] == str(vdu["id"]): |
| 953 | break |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 954 | vm_uuid = str(uuid4()) |
| 955 | uuid_list.append(vm_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 956 | vdu_id = get_str(vdu, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 957 | db_vm = { |
| 958 | "uuid": vm_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 959 | "osm_id": vdu_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 960 | "name": get_str(vdu, "name", 255), |
| 961 | "description": get_str(vdu, "description", 255), |
| 962 | "vnf_id": vnf_uuid, |
| 963 | } |
| 964 | vdu_id2uuid[db_vm["osm_id"]] = vm_uuid |
| 965 | vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index |
| 966 | if vdu.get("count"): |
| 967 | db_vm["count"] = int(vdu["count"]) |
| 968 | |
| 969 | # table image |
| 970 | image_present = False |
| 971 | if vdu.get("image"): |
| 972 | image_present = True |
| 973 | db_image = {} |
| 974 | image_uuid = _lookfor_or_create_image(db_image, mydb, vdu) |
| 975 | if not image_uuid: |
| 976 | image_uuid = db_image["uuid"] |
| 977 | db_images.append(db_image) |
| 978 | db_vm["image_id"] = image_uuid |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 979 | if vdu.get("alternative-images"): |
| 980 | vm_alternative_images = [] |
| 981 | for alt_image in vdu.get("alternative-images").itervalues(): |
| 982 | db_image = {} |
| 983 | image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image) |
| 984 | if not image_uuid: |
| 985 | image_uuid = db_image["uuid"] |
| 986 | db_images.append(db_image) |
| 987 | vm_alternative_images.append({ |
| 988 | "image_id": image_uuid, |
| 989 | "vim_type": str(alt_image["vim-type"]), |
| 990 | # "universal_name": str(alt_image["image"]), |
| 991 | # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None |
| 992 | }) |
| 993 | |
| 994 | 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] | 995 | |
| 996 | # volumes |
| 997 | devices = [] |
| 998 | if vdu.get("volumes"): |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 999 | for volume_key in vdu["volumes"]: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1000 | volume = vdu["volumes"][volume_key] |
| 1001 | if not image_present: |
| 1002 | # Convert the first volume to vnfc.image |
| 1003 | image_present = True |
| 1004 | db_image = {} |
| 1005 | image_uuid = _lookfor_or_create_image(db_image, mydb, volume) |
| 1006 | if not image_uuid: |
| 1007 | image_uuid = db_image["uuid"] |
| 1008 | db_images.append(db_image) |
| 1009 | db_vm["image_id"] = image_uuid |
| 1010 | else: |
| 1011 | # Add Openmano devices |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1012 | device = {"name": str(volume.get("name"))} |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1013 | device["type"] = str(volume.get("device-type")) |
| 1014 | if volume.get("size"): |
| 1015 | device["size"] = int(volume["size"]) |
| 1016 | if volume.get("image"): |
| 1017 | device["image name"] = str(volume["image"]) |
| 1018 | if volume.get("image-checksum"): |
| 1019 | device["image checksum"] = str(volume["image-checksum"]) |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 1020 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1021 | devices.append(device) |
| 1022 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1023 | # cloud-init |
| 1024 | boot_data = {} |
| 1025 | if vdu.get("cloud-init"): |
| 1026 | boot_data["user-data"] = str(vdu["cloud-init"]) |
| 1027 | elif vdu.get("cloud-init-file"): |
| 1028 | # TODO Where this file content is present??? |
| 1029 | # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]] |
| 1030 | boot_data["user-data"] = str(vdu["cloud-init-file"]) |
| 1031 | |
| 1032 | if vdu.get("supplemental-boot-data"): |
| 1033 | if vdu["supplemental-boot-data"].get('boot-data-drive'): |
| 1034 | boot_data['boot-data-drive'] = True |
| 1035 | if vdu["supplemental-boot-data"].get('config-file'): |
| 1036 | om_cfgfile_list = list() |
| 1037 | for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues(): |
| 1038 | # TODO Where this file content is present??? |
| 1039 | cfg_source = str(custom_config_file["source"]) |
| 1040 | om_cfgfile_list.append({"dest": custom_config_file["dest"], |
| 1041 | "content": cfg_source}) |
| 1042 | boot_data['config-files'] = om_cfgfile_list |
| 1043 | if boot_data: |
| 1044 | db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256) |
| 1045 | |
| 1046 | db_vms.append(db_vm) |
| 1047 | db_vms_index += 1 |
| 1048 | |
| 1049 | # table interfaces (internal/external interfaces) |
| 1050 | flavor_epa_interfaces = [] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1051 | vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU |
| 1052 | # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()): |
| 1053 | for iface in vdu.get("interface").itervalues(): |
| 1054 | flavor_epa_interface = {} |
| 1055 | iface_uuid = str(uuid4()) |
| 1056 | uuid_list.append(iface_uuid) |
| 1057 | db_interface = { |
| 1058 | "uuid": iface_uuid, |
| 1059 | "internal_name": get_str(iface, "name", 255), |
| 1060 | "vm_id": vm_uuid, |
| 1061 | } |
| 1062 | flavor_epa_interface["name"] = db_interface["internal_name"] |
| 1063 | if iface.get("virtual-interface").get("vpci"): |
| 1064 | db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12) |
| 1065 | flavor_epa_interface["vpci"] = db_interface["vpci"] |
| 1066 | |
| 1067 | if iface.get("virtual-interface").get("bandwidth"): |
| 1068 | bps = int(iface.get("virtual-interface").get("bandwidth")) |
| 1069 | db_interface["bw"] = int(math.ceil(bps/1000000.0)) |
| 1070 | flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"]) |
| 1071 | |
| 1072 | if iface.get("virtual-interface").get("type") == "OM-MGMT": |
| 1073 | db_interface["type"] = "mgmt" |
| 1074 | elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000"): |
| 1075 | db_interface["type"] = "bridge" |
| 1076 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1077 | elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 1078 | db_interface["type"] = "data" |
| 1079 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1080 | flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \ |
| 1081 | else "yes" |
| 1082 | flavor_epa_interfaces.append(flavor_epa_interface) |
| 1083 | else: |
| 1084 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual" |
| 1085 | "-interface':'type':'{}'. Interface type is not supported".format( |
| 1086 | vnfd_id, vdu_id, iface.get("virtual-interface").get("type")), |
| 1087 | HTTP_Bad_Request) |
| 1088 | |
| tierno | e72710b | 2018-07-23 16:16:00 +0200 | [diff] [blame] | 1089 | if iface.get("mgmt-interface"): |
| 1090 | db_interface["type"] = "mgmt" |
| 1091 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1092 | if iface.get("external-connection-point-ref"): |
| 1093 | try: |
| 1094 | cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")] |
| 1095 | db_interface["external_name"] = get_str(cp, "name", 255) |
| 1096 | cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid |
| 1097 | cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid |
| 1098 | cp_name2db_interface[db_interface["external_name"]] = db_interface |
| 1099 | for cp_descriptor in vnfd_descriptor["connection-point"]: |
| 1100 | if cp_descriptor["name"] == db_interface["external_name"]: |
| 1101 | break |
| 1102 | else: |
| 1103 | raise KeyError() |
| 1104 | |
| 1105 | if vdu_id in vdu_id2cp_name: |
| 1106 | vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU |
| 1107 | else: |
| 1108 | vdu_id2cp_name[vdu_id] = db_interface["external_name"] |
| 1109 | |
| 1110 | # port security |
| 1111 | if str(cp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1112 | db_interface["port_security"] = 0 |
| 1113 | elif str(cp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1114 | db_interface["port_security"] = 1 |
| 1115 | except KeyError: |
| 1116 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| 1117 | "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present" |
| 1118 | " at connection-point".format( |
| 1119 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| 1120 | cp=iface.get("vnfd-connection-point-ref")), |
| 1121 | HTTP_Bad_Request) |
| 1122 | elif iface.get("internal-connection-point-ref"): |
| 1123 | try: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1124 | for icp_descriptor in vdu_descriptor["internal-connection-point"]: |
| 1125 | if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")): |
| 1126 | break |
| 1127 | else: |
| 1128 | raise KeyError("does not exist at vdu:internal-connection-point") |
| 1129 | icp = None |
| 1130 | icp_vld = None |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1131 | for vld in vnfd.get("internal-vld").itervalues(): |
| 1132 | for cp in vld.get("internal-connection-point").itervalues(): |
| 1133 | if cp.get("id-ref") == iface.get("internal-connection-point-ref"): |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1134 | if icp: |
| 1135 | raise KeyError("is referenced by more than one 'internal-vld'") |
| 1136 | icp = cp |
| 1137 | icp_vld = vld |
| 1138 | if not icp: |
| 1139 | raise KeyError("is not referenced by any 'internal-vld'") |
| 1140 | |
| 1141 | db_interface["net_id"] = net_id2uuid[icp_vld.get("id")] |
| 1142 | if str(icp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1143 | db_interface["port_security"] = 0 |
| 1144 | elif str(icp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1145 | db_interface["port_security"] = 1 |
| 1146 | if icp.get("ip-address"): |
| 1147 | if not icp_vld.get("ip-profile-ref"): |
| 1148 | raise NfvoException |
| 1149 | db_interface["ip_address"] = str(icp.get("ip-address")) |
| 1150 | except KeyError as e: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1151 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1152 | "'interface[{iface}]':'internal-connection-point-ref':'{cp}'" |
| 1153 | " {msg}".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1154 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1155 | cp=iface.get("internal-connection-point-ref"), msg=str(e)), |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1156 | HTTP_Bad_Request) |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 1157 | if iface.get("position"): |
| 1158 | db_interface["created_at"] = int(iface.get("position")) * 50 |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1159 | if iface.get("mac-address"): |
| 1160 | db_interface["mac"] = str(iface.get("mac-address")) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1161 | db_interfaces.append(db_interface) |
| 1162 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1163 | # table flavors |
| 1164 | db_flavor = { |
| 1165 | "name": get_str(vdu, "name", 250) + "-flv", |
| 1166 | "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)), |
| 1167 | "ram": int(vdu["vm-flavor"].get("memory-mb", 1)), |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1168 | "disk": int(vdu["vm-flavor"].get("storage-gb", 0)), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1169 | } |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1170 | # TODO revise the case of several numa-node-policy node |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1171 | extended = {} |
| 1172 | numa = {} |
| 1173 | if devices: |
| 1174 | extended["devices"] = devices |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1175 | if flavor_epa_interfaces: |
| 1176 | numa["interfaces"] = flavor_epa_interfaces |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1177 | if vdu.get("guest-epa"): # TODO or dedicated_int: |
| 1178 | epa_vcpu_set = False |
| 1179 | if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int: |
| 1180 | numa_node_policy = vdu["guest-epa"].get("numa-node-policy") |
| 1181 | if numa_node_policy.get("node"): |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1182 | numa_node = numa_node_policy["node"].values()[0] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1183 | if numa_node.get("num-cores"): |
| 1184 | numa["cores"] = numa_node["num-cores"] |
| 1185 | epa_vcpu_set = True |
| 1186 | if numa_node.get("paired-threads"): |
| 1187 | if numa_node["paired-threads"].get("num-paired-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1188 | numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1189 | epa_vcpu_set = True |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1190 | if len(numa_node["paired-threads"].get("paired-thread-ids")): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1191 | numa["paired-threads-id"] = [] |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1192 | for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues(): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1193 | numa["paired-threads-id"].append( |
| 1194 | (str(pair["thread-a"]), str(pair["thread-b"])) |
| 1195 | ) |
| 1196 | if numa_node.get("num-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1197 | numa["threads"] = int(numa_node["num-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1198 | epa_vcpu_set = True |
| 1199 | if numa_node.get("memory-mb"): |
| 1200 | numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1) |
| 1201 | if vdu["guest-epa"].get("mempage-size"): |
| 1202 | if vdu["guest-epa"]["mempage-size"] != "SMALL": |
| 1203 | numa["memory"] = max(int(db_flavor["ram"] / 1024), 1) |
| 1204 | if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set: |
| 1205 | if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED": |
| 1206 | if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \ |
| 1207 | vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER": |
| 1208 | numa["cores"] = max(db_flavor["vcpus"], 1) |
| 1209 | else: |
| 1210 | numa["threads"] = max(db_flavor["vcpus"], 1) |
| 1211 | if numa: |
| 1212 | extended["numas"] = [numa] |
| 1213 | if extended: |
| 1214 | extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256) |
| 1215 | db_flavor["extended"] = extended_text |
| 1216 | # look if flavor exist |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1217 | temp_flavor_dict = {'disk': db_flavor.get('disk', 0), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1218 | 'ram': db_flavor.get('ram'), |
| 1219 | 'vcpus': db_flavor.get('vcpus'), |
| 1220 | 'extended': db_flavor.get('extended') |
| 1221 | } |
| 1222 | existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 1223 | if existing_flavors: |
| 1224 | flavor_uuid = existing_flavors[0]["uuid"] |
| 1225 | else: |
| 1226 | flavor_uuid = str(uuid4()) |
| 1227 | uuid_list.append(flavor_uuid) |
| 1228 | db_flavor["uuid"] = flavor_uuid |
| 1229 | db_flavors.append(db_flavor) |
| 1230 | db_vm["flavor_id"] = flavor_uuid |
| 1231 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1232 | # VNF affinity and antiaffinity |
| 1233 | for pg in vnfd.get("placement-groups").itervalues(): |
| 1234 | pg_name = get_str(pg, "name", 255) |
| 1235 | for vdu in pg.get("member-vdus").itervalues(): |
| 1236 | vdu_id = get_str(vdu, "member-vdu-ref", 255) |
| 1237 | if vdu_id not in vdu_id2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1238 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':" |
| 1239 | "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1240 | vnf=vnfd_id, pg=pg_name, vdu=vdu_id), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1241 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1242 | db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name |
| 1243 | # TODO consider the case of isolation and not colocation |
| 1244 | # if pg.get("strategy") == "ISOLATION": |
| 1245 | |
| 1246 | # VNF mgmt configuration |
| 1247 | mgmt_access = {} |
| 1248 | if vnfd["mgmt-interface"].get("vdu-id"): |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1249 | mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255) |
| 1250 | if mgmt_vdu_id not in vdu_id2uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1251 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':" |
| 1252 | "'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1253 | vnf=vnfd_id, vdu=mgmt_vdu_id), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1254 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1255 | mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1256 | # if only one cp is defined by this VDU, mark this interface as of type "mgmt" |
| 1257 | if vdu_id2cp_name.get(mgmt_vdu_id): |
| 1258 | cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt" |
| 1259 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1260 | if vnfd["mgmt-interface"].get("ip-address"): |
| 1261 | mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address")) |
| 1262 | if vnfd["mgmt-interface"].get("cp"): |
| 1263 | if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1264 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. " |
| 1265 | "Reference to a non-existing connection-point".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1266 | vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1267 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1268 | mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]] |
| 1269 | mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]] |
| tierno | e2ff1ce | 2017-11-02 17:01:10 +0100 | [diff] [blame] | 1270 | # mark this interface as of type mgmt |
| 1271 | cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt" |
| 1272 | |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 1273 | 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] | 1274 | "default-user", 64) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1275 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1276 | if default_user: |
| 1277 | mgmt_access["default_user"] = default_user |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1278 | required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}), |
| 1279 | "required", 6) |
| 1280 | if required: |
| 1281 | mgmt_access["required"] = required |
| 1282 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1283 | if mgmt_access: |
| 1284 | db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256) |
| 1285 | |
| 1286 | db_vnfs.append(db_vnf) |
| 1287 | db_tables=[ |
| 1288 | {"vnfs": db_vnfs}, |
| 1289 | {"nets": db_nets}, |
| 1290 | {"images": db_images}, |
| 1291 | {"flavors": db_flavors}, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 1292 | {"ip_profiles": db_ip_profiles}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1293 | {"vms": db_vms}, |
| 1294 | {"interfaces": db_interfaces}, |
| 1295 | ] |
| 1296 | |
| 1297 | logger.debug("create_vnf Deployment done vnfDict: %s", |
| 1298 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 1299 | mydb.new_rows(db_tables, uuid_list) |
| 1300 | return vnfd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1301 | except NfvoException: |
| 1302 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1303 | except Exception as e: |
| 1304 | logger.error("Exception {}".format(e)) |
| 1305 | raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request) |
| 1306 | |
| 1307 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1308 | def new_vnf(mydb, tenant_id, vnf_descriptor): |
| 1309 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1310 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1311 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1312 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1313 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1314 | vims = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1315 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1316 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1317 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1318 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1319 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 1320 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1321 | else: |
| 1322 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1323 | # 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] | 1324 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1325 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1326 | |
| 1327 | # Step 4. Review the descriptor and add missing fields |
| 1328 | #print vnf_descriptor |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1329 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1330 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1331 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1332 | if "physical" in vnf_descriptor['vnf']: |
| 1333 | del vnf_descriptor['vnf']['physical'] |
| 1334 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1335 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1336 | # 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] | 1337 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1338 | 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] | 1339 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1340 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1341 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1342 | 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] | 1343 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1344 | 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] | 1345 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1346 | VNFCitem={} |
| 1347 | VNFCitem["name"] = vnfc['name'] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1348 | VNFCitem["availability_zone"] = vnfc.get('availability_zone') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1349 | 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] | 1350 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1351 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1352 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1353 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1354 | 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] | 1355 | myflavorDict["description"] = VNFCitem["description"] |
| 1356 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1357 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1358 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1359 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1360 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1361 | devices = vnfc.get("devices") |
| 1362 | if devices != None: |
| 1363 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1364 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1365 | # TODO: |
| 1366 | # 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] | 1367 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1368 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1369 | # Previous code has been commented |
| 1370 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1371 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1372 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1373 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1374 | #else: |
| 1375 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1376 | # if result2: |
| 1377 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 1378 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 1379 | # else: |
| 1380 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 1381 | 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] | 1382 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1383 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1384 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1385 | |
| 1386 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1387 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1388 | # Step 6.2 New flavors are created in the VIM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1389 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1390 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1391 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1392 | VNFCitem["flavor_id"] = flavor_id |
| 1393 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1394 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1395 | logger.debug("Creating new images in the VIM for each VNFC") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1396 | # Step 6.3 New images are created in the VIM |
| 1397 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1398 | #This "for" loop might be integrated with the previous one |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1399 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1400 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1401 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1402 | image_dict={} |
| 1403 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1404 | image_dict['universal_name']=vnfc.get('image name') |
| 1405 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1406 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1407 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1408 | image_dict['checksum']=vnfc.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1409 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1410 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1411 | if image_metadata_dict is not None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1412 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1413 | image_dict['metadata']=image_metadata_str |
| 1414 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1415 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1416 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1417 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1418 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1419 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1420 | if vnfc.get("boot-data"): |
| 1421 | 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] | 1422 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1423 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1424 | # Step 7. Storing the VNF descriptor in the repository |
| 1425 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1426 | 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] | 1427 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1428 | # Step 8. Adding the VNF to the NFVO DB |
| 1429 | vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1430 | return vnf_id |
| 1431 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1432 | _, message = rollback(mydb, vims, rollback_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1433 | if isinstance(e, db_base_Exception): |
| 1434 | error_text = "Exception at database" |
| 1435 | elif isinstance(e, KeyError): |
| 1436 | error_text = "KeyError exception " |
| 1437 | e.http_code = HTTP_Internal_Server_Error |
| 1438 | else: |
| 1439 | error_text = "Exception at VIM" |
| 1440 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1441 | #logger.error("start_scenario %s", error_text) |
| 1442 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1443 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1444 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1445 | def new_vnf_v02(mydb, tenant_id, vnf_descriptor): |
| 1446 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1447 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1448 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1449 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1450 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1451 | vims = {} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1452 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1453 | check_tenant(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1454 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1455 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| 1456 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 1457 | HTTP_Unauthorized) |
| 1458 | else: |
| 1459 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1460 | # 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] | 1461 | if global_config["auto_push_VNF_to_VIMs"]: |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1462 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1463 | |
| 1464 | # Step 4. Review the descriptor and add missing fields |
| 1465 | #print vnf_descriptor |
| 1466 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| 1467 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1468 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1469 | if "physical" in vnf_descriptor['vnf']: |
| 1470 | del vnf_descriptor['vnf']['physical'] |
| 1471 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1472 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1473 | # 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] | 1474 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1475 | 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] | 1476 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1477 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1478 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1479 | rollback_list = [] # It will contain the new images created in mano. It is used for rollback |
| 1480 | try: |
| 1481 | logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC") |
| 1482 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1483 | VNFCitem={} |
| 1484 | VNFCitem["name"] = vnfc['name'] |
| 1485 | 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] | 1486 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1487 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1488 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1489 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1490 | 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] | 1491 | myflavorDict["description"] = VNFCitem["description"] |
| 1492 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1493 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| garciadeblas | 79d1a1a | 2017-12-11 16:07:07 +0100 | [diff] [blame] | 1494 | myflavorDict["disk"] = vnfc.get("disk", 0) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1495 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1496 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1497 | devices = vnfc.get("devices") |
| 1498 | if devices != None: |
| 1499 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1500 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1501 | # TODO: |
| 1502 | # 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] | 1503 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1504 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1505 | # Previous code has been commented |
| 1506 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1507 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1508 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1509 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1510 | #else: |
| 1511 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1512 | # if result2: |
| 1513 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 1514 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 1515 | # else: |
| 1516 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 1517 | 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] | 1518 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1519 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1520 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1521 | |
| 1522 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1523 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1524 | # Step 6.2 New flavors are created in the VIM |
| 1525 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| 1526 | |
| 1527 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| 1528 | VNFCitem["flavor_id"] = flavor_id |
| 1529 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1530 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1531 | logger.debug("Creating new images in the VIM for each VNFC") |
| 1532 | # Step 6.3 New images are created in the VIM |
| 1533 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1534 | #This "for" loop might be integrated with the previous one |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1535 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1536 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1537 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1538 | image_dict={} |
| 1539 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1540 | image_dict['universal_name']=vnfc.get('image name') |
| 1541 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1542 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1543 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1544 | image_dict['checksum']=vnfc.get('image checksum') |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1545 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1546 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1547 | if image_metadata_dict is not None: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1548 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1549 | image_dict['metadata']=image_metadata_str |
| 1550 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| 1551 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1552 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| 1553 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1554 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1555 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1556 | if vnfc.get("boot-data"): |
| 1557 | 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] | 1558 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1559 | # Step 7. Storing the VNF descriptor in the repository |
| 1560 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1561 | 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] | 1562 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1563 | # Step 8. Adding the VNF to the NFVO DB |
| 1564 | vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1565 | return vnf_id |
| 1566 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| 1567 | _, message = rollback(mydb, vims, rollback_list) |
| 1568 | if isinstance(e, db_base_Exception): |
| 1569 | error_text = "Exception at database" |
| 1570 | elif isinstance(e, KeyError): |
| 1571 | error_text = "KeyError exception " |
| 1572 | e.http_code = HTTP_Internal_Server_Error |
| 1573 | else: |
| 1574 | error_text = "Exception at VIM" |
| 1575 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1576 | #logger.error("start_scenario %s", error_text) |
| 1577 | raise NfvoException(error_text, e.http_code) |
| 1578 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1579 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1580 | def get_vnf_id(mydb, tenant_id, vnf_id): |
| 1581 | #check valid tenant_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1582 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1583 | #obtain data |
| 1584 | where_or = {} |
| 1585 | if tenant_id != "any": |
| 1586 | where_or["tenant_id"] = tenant_id |
| 1587 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1588 | vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") |
| 1589 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1590 | vnf_id = vnf["uuid"] |
| 1591 | filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1592 | 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] | 1593 | #change_keys_http2db(filtered_content, http2db_vnf, reverse=True) |
| 1594 | data={'vnf' : filtered_content} |
| 1595 | #GET VM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1596 | 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] | 1597 | SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description', |
| 1598 | 'boot_data'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1599 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1600 | if len(content)==0: |
| 1601 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1602 | # change boot_data into boot-data |
| 1603 | for vm in content: |
| 1604 | if vm.get("boot_data"): |
| 1605 | vm["boot-data"] = yaml.safe_load(vm["boot_data"]) |
| 1606 | del vm["boot_data"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1607 | |
| 1608 | data['vnf']['VNFC'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1609 | #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] | 1610 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1611 | #GET NET |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1612 | 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] | 1613 | SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'), |
| 1614 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1615 | data['vnf']['nets'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1616 | |
| 1617 | #GET ip-profile for each net |
| 1618 | for net in data['vnf']['nets']: |
| 1619 | ipprofiles = mydb.get_rows(FROM='ip_profiles', |
| 1620 | SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'), |
| 1621 | WHERE={'net_id': net["uuid"]} ) |
| 1622 | if len(ipprofiles)==1: |
| 1623 | net["ip_profile"] = ipprofiles[0] |
| 1624 | elif len(ipprofiles)>1: |
| 1625 | 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] | 1626 | |
| 1627 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1628 | #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] | 1629 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1630 | #GET External Interfaces |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1631 | 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] | 1632 | SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \ |
| 1633 | '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] | 1634 | WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1635 | #print content |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1636 | data['vnf']['external-connections'] = content |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1637 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1638 | return data |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1639 | |
| 1640 | |
| 1641 | def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None): |
| 1642 | # Check tenant exist |
| 1643 | if tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1644 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1645 | # Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | cbb5205 | 2018-05-31 18:57:30 +0200 | [diff] [blame] | 1646 | vims = get_vim(mydb, tenant_id, ignore_errors=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1647 | else: |
| 1648 | vims={} |
| 1649 | |
| 1650 | # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided" |
| 1651 | where_or = {} |
| 1652 | if tenant_id != "any": |
| 1653 | where_or["tenant_id"] = tenant_id |
| 1654 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1655 | 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] | 1656 | vnf_id = vnf["uuid"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1657 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1658 | # "Getting the list of flavors and tenants of the VNF" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1659 | flavorList = get_flavorlist(mydb, vnf_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1660 | if len(flavorList)==0: |
| 1661 | 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] | 1662 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1663 | imageList = get_imagelist(mydb, vnf_id) |
| 1664 | if len(imageList)==0: |
| 1665 | 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] | 1666 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1667 | deleted = mydb.delete_row_by_id('vnfs', vnf_id) |
| 1668 | if deleted == 0: |
| 1669 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1670 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1671 | undeletedItems = [] |
| 1672 | for flavor in flavorList: |
| 1673 | #check if flavor is used by other vnf |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1674 | try: |
| 1675 | c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} ) |
| 1676 | if len(c) > 0: |
| 1677 | logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor) |
| 1678 | continue |
| 1679 | #flavor not used, must be deleted |
| 1680 | #delelte at VIM |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1681 | c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1682 | for flavor_vim in c: |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1683 | if not flavor_vim['created']: # skip this flavor because not created by openmano |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1684 | continue |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1685 | # look for vim |
| 1686 | myvim = None |
| 1687 | for vim in vims.values(): |
| 1688 | if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]: |
| 1689 | myvim = vim |
| 1690 | break |
| 1691 | if not myvim: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1692 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1693 | try: |
| 1694 | myvim.delete_flavor(flavor_vim["vim_id"]) |
| tierno | 96ebf00 | 2017-12-13 10:55:38 +0100 | [diff] [blame] | 1695 | except vimconn.vimconnNotFoundException: |
| 1696 | logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], |
| 1697 | flavor_vim["datacenter_vim_id"] ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1698 | except vimconn.vimconnException as e: |
| 1699 | 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] | 1700 | flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e)) |
| 1701 | undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], |
| 1702 | flavor_vim["datacenter_vim_id"])) |
| 1703 | # 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] | 1704 | mydb.delete_row_by_id('flavors', flavor) |
| 1705 | except db_base_Exception as e: |
| 1706 | 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] | 1707 | undeletedItems.append("flavor {}".format(flavor)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1708 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1709 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1710 | for image in imageList: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1711 | try: |
| 1712 | #check if image is used by other vnf |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 1713 | 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] | 1714 | if len(c) > 0: |
| 1715 | logger.debug("Image '%s' not deleted because it is being used by another VNF", image) |
| 1716 | continue |
| 1717 | #image not used, must be deleted |
| 1718 | #delelte at VIM |
| 1719 | c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1720 | for image_vim in c: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 1721 | 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] | 1722 | continue |
| 1723 | if image_vim['created']=='false': #skip this image because not created by openmano |
| 1724 | continue |
| 1725 | myvim=vims[ image_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1726 | try: |
| 1727 | myvim.delete_image(image_vim["vim_id"]) |
| 1728 | except vimconn.vimconnNotFoundException as e: |
| 1729 | logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] ) |
| 1730 | except vimconn.vimconnException as e: |
| 1731 | logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s", |
| 1732 | image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1733 | 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] | 1734 | #delete image from Database, using table images and with cascade foreign key also at datacenters_images |
| 1735 | mydb.delete_row_by_id('images', image) |
| 1736 | except db_base_Exception as e: |
| 1737 | 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] | 1738 | undeletedItems.append("image %s" % image) |
| 1739 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1740 | return vnf_id + " " + vnf["name"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1741 | #if undeletedItems: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1742 | # return "delete_vnf. Undeleted: %s" %(undeletedItems) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1743 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1744 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1745 | def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None): |
| 1746 | result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name) |
| 1747 | if result < 0: |
| 1748 | return result, vims |
| 1749 | elif result == 0: |
| 1750 | return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name |
| 1751 | myvim = vims.values()[0] |
| 1752 | result,servers = myvim.get_hosts_info() |
| 1753 | if result < 0: |
| 1754 | return result, servers |
| 1755 | topology = {'name':myvim['name'] , 'servers': servers} |
| 1756 | return result, topology |
| 1757 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1758 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1759 | def get_hosts(mydb, nfvo_tenant_id): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1760 | vims = get_vim(mydb, nfvo_tenant_id) |
| 1761 | if len(vims) == 0: |
| 1762 | raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found) |
| 1763 | elif len(vims)>1: |
| 1764 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 1765 | 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] | 1766 | myvim = vims.values()[0] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1767 | try: |
| 1768 | hosts = myvim.get_hosts() |
| 1769 | 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] | 1770 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1771 | datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] } |
| 1772 | for host in hosts: |
| 1773 | server={'name':host['name'], 'vms':[]} |
| 1774 | for vm in host['instances']: |
| 1775 | #get internal name and model |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1776 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1777 | c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\ |
| 1778 | WHERE={'vim_vm_id':vm['id']} ) |
| 1779 | if len(c) == 0: |
| 1780 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id'])) |
| 1781 | continue |
| 1782 | server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1783 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1784 | except db_base_Exception as e: |
| 1785 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e))) |
| 1786 | datacenter['Datacenters'][0]['servers'].append(server) |
| 1787 | #return -400, "en construccion" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1788 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1789 | #print 'datacenters '+ json.dumps(datacenter, indent=4) |
| 1790 | return datacenter |
| 1791 | except vimconn.vimconnException as e: |
| 1792 | 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] | 1793 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1794 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1795 | def new_scenario(mydb, tenant_id, topo): |
| 1796 | |
| 1797 | # result, vims = get_vim(mydb, tenant_id) |
| 1798 | # if result < 0: |
| 1799 | # return result, vims |
| 1800 | #1: parse input |
| 1801 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1802 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1803 | if "tenant_id" in topo: |
| 1804 | if topo["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1805 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id), |
| 1806 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1807 | else: |
| 1808 | tenant_id=None |
| 1809 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1810 | #1.1: get VNFs and external_networks (other_nets). |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1811 | vnfs={} |
| 1812 | other_nets={} #external_networks, bridge_networks and data_networkds |
| 1813 | nodes = topo['topology']['nodes'] |
| 1814 | for k in nodes.keys(): |
| 1815 | if nodes[k]['type'] == 'VNF': |
| 1816 | vnfs[k] = nodes[k] |
| 1817 | vnfs[k]['ifaces'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1818 | elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1819 | other_nets[k] = nodes[k] |
| 1820 | other_nets[k]['external']=True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1821 | elif nodes[k]['type'] == 'network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1822 | other_nets[k] = nodes[k] |
| 1823 | other_nets[k]['external']=False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1824 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1825 | |
| 1826 | #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces |
| 1827 | for name,vnf in vnfs.items(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1828 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1829 | error_text = "" |
| 1830 | error_pos = "'topology':'nodes':'" + name + "'" |
| 1831 | if 'vnf_id' in vnf: |
| 1832 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1833 | where['uuid'] = vnf['vnf_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1834 | if 'VNF model' in vnf: |
| 1835 | error_text += " 'VNF model' " + vnf['VNF model'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1836 | where['name'] = vnf['VNF model'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1837 | if len(where) == 1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1838 | 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] | 1839 | |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1840 | vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), |
| 1841 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1842 | WHERE=where) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1843 | if len(vnf_db)==0: |
| 1844 | raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| 1845 | elif len(vnf_db)>1: |
| 1846 | 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] | 1847 | vnf['uuid']=vnf_db[0]['uuid'] |
| 1848 | vnf['description']=vnf_db[0]['description'] |
| 1849 | #get external interfaces |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1850 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), |
| 1851 | 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] | 1852 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1853 | for ext_iface in ext_ifaces: |
| 1854 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']} |
| 1855 | |
| 1856 | #1.4 get list of connections |
| 1857 | conections = topo['topology']['connections'] |
| 1858 | conections_list = [] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1859 | conections_list_name = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1860 | for k in conections.keys(): |
| 1861 | if type(conections[k]['nodes'])==dict: #dict with node:iface pairs |
| 1862 | ifaces_list = conections[k]['nodes'].items() |
| 1863 | elif type(conections[k]['nodes'])==list: #list with dictionary |
| 1864 | ifaces_list=[] |
| 1865 | conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] ) |
| 1866 | for k2 in conection_pair_list: |
| 1867 | ifaces_list += k2 |
| 1868 | |
| 1869 | con_type = conections[k].get("type", "link") |
| 1870 | if con_type != "link": |
| 1871 | if k in other_nets: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1872 | 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] | 1873 | other_nets[k] = {'external': False} |
| 1874 | if conections[k].get("graph"): |
| 1875 | other_nets[k]["graph"] = conections[k]["graph"] |
| 1876 | ifaces_list.append( (k, None) ) |
| 1877 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1878 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1879 | if con_type == "external_network": |
| 1880 | other_nets[k]['external'] = True |
| 1881 | if conections[k].get("model"): |
| 1882 | other_nets[k]["model"] = conections[k]["model"] |
| 1883 | else: |
| 1884 | other_nets[k]["model"] = k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1885 | if con_type == "dataplane_net" or con_type == "bridge_net": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1886 | other_nets[k]["model"] = con_type |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1887 | |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1888 | conections_list_name.append(k) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1889 | 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) |
| 1890 | #print set(ifaces_list) |
| 1891 | #check valid VNF and iface names |
| 1892 | for iface in ifaces_list: |
| 1893 | if iface[0] not in vnfs and iface[0] not in other_nets : |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1894 | raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format( |
| 1895 | str(k), iface[0]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1896 | 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] | 1897 | raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format( |
| 1898 | str(k), iface[0], iface[1]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1899 | |
| 1900 | #1.5 unify connections from the pair list to a consolidated list |
| 1901 | index=0 |
| 1902 | while index < len(conections_list): |
| 1903 | index2 = index+1 |
| 1904 | while index2 < len(conections_list): |
| 1905 | if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets |
| 1906 | conections_list[index] |= conections_list[index2] |
| 1907 | del conections_list[index2] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1908 | del conections_list_name[index2] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1909 | else: |
| 1910 | index2 += 1 |
| 1911 | conections_list[index] = list(conections_list[index]) # from set to list again |
| 1912 | index += 1 |
| 1913 | #for k in conections_list: |
| 1914 | # print k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1915 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1916 | |
| 1917 | |
| 1918 | #1.6 Delete non external nets |
| 1919 | # for k in other_nets.keys(): |
| 1920 | # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net': |
| 1921 | # for con in conections_list: |
| 1922 | # delete_indexes=[] |
| 1923 | # for index in range(0,len(con)): |
| 1924 | # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower |
| 1925 | # for index in delete_indexes: |
| 1926 | # del con[index] |
| 1927 | # del other_nets[k] |
| 1928 | #1.7: Check external_ports are present at database table datacenter_nets |
| 1929 | for k,net in other_nets.items(): |
| 1930 | error_pos = "'topology':'nodes':'" + k + "'" |
| 1931 | if net['external']==False: |
| 1932 | if 'name' not in net: |
| 1933 | net['name']=k |
| 1934 | if 'model' not in net: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1935 | raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1936 | if net['model']=='bridge_net': |
| 1937 | net['type']='bridge'; |
| 1938 | elif net['model']=='dataplane_net': |
| 1939 | net['type']='data'; |
| 1940 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1941 | raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1942 | else: #external |
| 1943 | #IF we do not want to check that external network exist at datacenter |
| 1944 | pass |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1945 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1946 | # error_text = "" |
| 1947 | # WHERE_={} |
| 1948 | # if 'net_id' in net: |
| 1949 | # error_text += " 'net_id' " + net['net_id'] |
| 1950 | # WHERE_['uuid'] = net['net_id'] |
| 1951 | # if 'model' in net: |
| 1952 | # error_text += " 'model' " + net['model'] |
| 1953 | # WHERE_['name'] = net['model'] |
| 1954 | # if len(WHERE_) == 0: |
| 1955 | # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos |
| 1956 | # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'), |
| 1957 | # FROM='datacenter_nets', WHERE=WHERE_ ) |
| 1958 | # if r<0: |
| 1959 | # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db |
| 1960 | # elif r==0: |
| 1961 | # print "nfvo.new_scenario Error" +error_text+ " is not present at database" |
| 1962 | # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos |
| 1963 | # elif r>1: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1964 | # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database" |
| 1965 | # 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] | 1966 | # other_nets[k].update(net_db[0]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1967 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1968 | net_list={} |
| 1969 | net_nb=0 #Number of nets |
| 1970 | for con in conections_list: |
| 1971 | #check if this is connected to a external net |
| 1972 | other_net_index=-1 |
| 1973 | #print |
| 1974 | #print "con", con |
| 1975 | for index in range(0,len(con)): |
| 1976 | #check if this is connected to a external net |
| 1977 | for net_key in other_nets.keys(): |
| 1978 | if con[index][0]==net_key: |
| 1979 | if other_net_index>=0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1980 | 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] | 1981 | #print "nfvo.new_scenario " + error_text |
| 1982 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1983 | else: |
| 1984 | other_net_index = index |
| 1985 | net_target = net_key |
| 1986 | break |
| 1987 | #print "other_net_index", other_net_index |
| 1988 | try: |
| 1989 | if other_net_index>=0: |
| 1990 | del con[other_net_index] |
| 1991 | #IF we do not want to check that external network exist at datacenter |
| 1992 | if other_nets[net_target]['external'] : |
| 1993 | if "name" not in other_nets[net_target]: |
| 1994 | other_nets[net_target]['name'] = other_nets[net_target]['model'] |
| 1995 | if other_nets[net_target]["type"] == "external_network": |
| 1996 | if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data": |
| 1997 | other_nets[net_target]["type"] = "data" |
| 1998 | else: |
| 1999 | other_nets[net_target]["type"] = "bridge" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2000 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2001 | # if other_nets[net_target]['external'] : |
| 2002 | # 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 |
| 2003 | # if type_=='data' and other_nets[net_target]['type']=="ptp": |
| 2004 | # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target) |
| 2005 | # print "nfvo.new_scenario " + error_text |
| 2006 | # return -HTTP_Bad_Request, error_text |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2007 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2008 | for iface in con: |
| 2009 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2010 | else: |
| 2011 | #create a net |
| 2012 | net_type_bridge=False |
| 2013 | net_type_data=False |
| 2014 | net_target = "__-__net"+str(net_nb) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2015 | 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] | 2016 | 'description':"net-%s in scenario %s" %(net_nb,topo['name']), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2017 | 'external':False} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2018 | for iface in con: |
| 2019 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 2020 | iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type'] |
| 2021 | if iface_type=='mgmt' or iface_type=='bridge': |
| 2022 | net_type_bridge = True |
| 2023 | else: |
| 2024 | net_type_data = True |
| 2025 | if net_type_bridge and net_type_data: |
| 2026 | 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] | 2027 | #print "nfvo.new_scenario " + error_text |
| 2028 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2029 | elif net_type_bridge: |
| 2030 | type_='bridge' |
| 2031 | else: |
| 2032 | type_='data' if len(con)>2 else 'ptp' |
| 2033 | net_list[net_target]['type'] = type_ |
| 2034 | net_nb+=1 |
| 2035 | except Exception: |
| 2036 | 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] | 2037 | #print "nfvo.new_scenario " + error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2038 | #raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2039 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2040 | |
| 2041 | #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] | 2042 | #1.8.1 obtain management net |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2043 | mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2044 | FROM='datacenter_nets', WHERE={'name':'mgmt'} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2045 | #1.8.2 check all interfaces from all vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2046 | if len(mgmt_net)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2047 | add_mgmt_net = False |
| 2048 | for vnf in vnfs.values(): |
| 2049 | for iface in vnf['ifaces'].values(): |
| 2050 | if iface['type']=='mgmt' and 'net_key' not in iface: |
| 2051 | #iface not connected |
| 2052 | iface['net_key'] = 'mgmt' |
| 2053 | add_mgmt_net = True |
| 2054 | if add_mgmt_net and 'mgmt' not in net_list: |
| 2055 | net_list['mgmt']=mgmt_net[0] |
| 2056 | net_list['mgmt']['external']=True |
| 2057 | net_list['mgmt']['graph']={'visible':False} |
| 2058 | |
| 2059 | net_list.update(other_nets) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2060 | #print |
| 2061 | #print 'net_list', net_list |
| 2062 | #print |
| 2063 | #print 'vnfs', vnfs |
| 2064 | #print |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2065 | |
| 2066 | #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2067 | c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list, |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2068 | 'tenant_id':tenant_id, 'name':topo['name'], |
| 2069 | 'description':topo.get('description',topo['name']), |
| 2070 | 'public': topo.get('public', False) |
| 2071 | }) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2072 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2073 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2074 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2075 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2076 | def new_scenario_v02(mydb, tenant_id, scenario_dict, version): |
| 2077 | """ This creates a new scenario for version 0.2 and 0.3""" |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2078 | scenario = scenario_dict["scenario"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2079 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2080 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2081 | if "tenant_id" in scenario: |
| 2082 | if scenario["tenant_id"] != tenant_id: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2083 | # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2084 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format( |
| 2085 | scenario["tenant_id"], tenant_id), HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2086 | else: |
| 2087 | tenant_id=None |
| 2088 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2089 | # 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] | 2090 | for name,vnf in scenario["vnfs"].iteritems(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2091 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2092 | error_text = "" |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 2093 | error_pos = "'scenario':'vnfs':'" + name + "'" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2094 | if 'vnf_id' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2095 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2096 | where['uuid'] = vnf['vnf_id'] |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2097 | if 'vnf_name' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2098 | error_text += " 'vnf_name' " + vnf['vnf_name'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2099 | where['name'] = vnf['vnf_name'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2100 | if len(where) == 1: |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 2101 | 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] | 2102 | vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'), |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2103 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2104 | WHERE=where) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2105 | if len(vnf_db) == 0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2106 | raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2107 | elif len(vnf_db) > 1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2108 | 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] | 2109 | vnf['uuid'] = vnf_db[0]['uuid'] |
| 2110 | vnf['description'] = vnf_db[0]['description'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2111 | vnf['ifaces'] = {} |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2112 | # get external interfaces |
| 2113 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'), |
| 2114 | 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] | 2115 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2116 | for ext_iface in ext_ifaces: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2117 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']} |
| 2118 | # 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] | 2119 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2120 | # 2: Insert net_key and ip_address at every vnf interface |
| 2121 | for net_name, net in scenario["networks"].items(): |
| 2122 | net_type_bridge = False |
| 2123 | net_type_data = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2124 | for iface_dict in net["interfaces"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2125 | if version == "0.2": |
| 2126 | temp_dict = iface_dict |
| 2127 | ip_address = None |
| 2128 | elif version == "0.3": |
| 2129 | temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]} |
| 2130 | ip_address = iface_dict.get('ip_address', None) |
| 2131 | for vnf, iface in temp_dict.items(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2132 | if vnf not in scenario["vnfs"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2133 | error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format( |
| 2134 | net_name, vnf) |
| 2135 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2136 | raise NfvoException(error_text, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2137 | if iface not in scenario["vnfs"][vnf]['ifaces']: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2138 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\ |
| 2139 | .format(net_name, iface) |
| 2140 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2141 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2142 | if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2143 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\ |
| 2144 | "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key']) |
| 2145 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2146 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2147 | scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2148 | scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2149 | iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type'] |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2150 | if iface_type == 'mgmt' or iface_type == 'bridge': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2151 | net_type_bridge = True |
| 2152 | else: |
| 2153 | net_type_data = True |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2154 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2155 | if net_type_bridge and net_type_data: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2156 | error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\ |
| 2157 | .format(net_name) |
| 2158 | # logger.debug("nfvo.new_scenario " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2159 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2160 | elif net_type_bridge: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2161 | type_ = 'bridge' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2162 | else: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2163 | type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp' |
| 2164 | |
| 2165 | if net.get("implementation"): # for v0.3 |
| 2166 | if type_ == "bridge" and net["implementation"] == "underlay": |
| 2167 | error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\ |
| 2168 | "'network':'{}'".format(net_name) |
| 2169 | # logger.debug(error_text) |
| 2170 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2171 | elif type_ != "bridge" and net["implementation"] == "overlay": |
| 2172 | error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\ |
| 2173 | "'network':'{}'".format(net_name) |
| 2174 | # logger.debug(error_text) |
| 2175 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2176 | net.pop("implementation") |
| 2177 | if "type" in net and version == "0.3": # for v0.3 |
| 2178 | if type_ == "data" and net["type"] == "e-line": |
| 2179 | error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\ |
| 2180 | "'e-line' at 'network':'{}'".format(net_name) |
| 2181 | # logger.debug(error_text) |
| 2182 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2183 | elif type_ == "ptp" and net["type"] == "e-lan": |
| 2184 | type_ = "data" |
| 2185 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2186 | net['type'] = type_ |
| 2187 | net['name'] = net_name |
| 2188 | net['external'] = net.get('external', False) |
| 2189 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2190 | # 3: insert at database |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2191 | scenario["nets"] = scenario["networks"] |
| 2192 | scenario['tenant_id'] = tenant_id |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2193 | scenario_id = mydb.new_scenario(scenario) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2194 | return scenario_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2195 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2196 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2197 | def new_nsd_v3(mydb, tenant_id, nsd_descriptor): |
| 2198 | """ |
| 2199 | Parses an OSM IM nsd_catalog and insert at DB |
| 2200 | :param mydb: |
| 2201 | :param tenant_id: |
| 2202 | :param nsd_descriptor: |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2203 | :return: The list of created NSD ids |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2204 | """ |
| 2205 | try: |
| 2206 | mynsd = nsd_catalog.nsd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 2207 | try: |
| 2208 | pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd) |
| 2209 | except Exception as e: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2210 | raise NfvoException("Error. Invalid NS descriptor format: " + str(e), HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2211 | db_scenarios = [] |
| 2212 | db_sce_nets = [] |
| 2213 | db_sce_vnfs = [] |
| 2214 | db_sce_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2215 | db_sce_vnffgs = [] |
| 2216 | db_sce_rsps = [] |
| 2217 | db_sce_rsp_hops = [] |
| 2218 | db_sce_classifiers = [] |
| 2219 | db_sce_classifier_matches = [] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2220 | db_ip_profiles = [] |
| 2221 | db_ip_profiles_index = 0 |
| 2222 | uuid_list = [] |
| 2223 | nsd_uuid_list = [] |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2224 | for nsd_yang in mynsd.nsd_catalog.nsd.itervalues(): |
| 2225 | nsd = nsd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2226 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2227 | # table scenarios |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2228 | scenario_uuid = str(uuid4()) |
| 2229 | uuid_list.append(scenario_uuid) |
| 2230 | nsd_uuid_list.append(scenario_uuid) |
| 2231 | db_scenario = { |
| 2232 | "uuid": scenario_uuid, |
| 2233 | "osm_id": get_str(nsd, "id", 255), |
| 2234 | "name": get_str(nsd, "name", 255), |
| 2235 | "description": get_str(nsd, "description", 255), |
| 2236 | "tenant_id": tenant_id, |
| 2237 | "vendor": get_str(nsd, "vendor", 255), |
| 2238 | "short_name": get_str(nsd, "short-name", 255), |
| 2239 | "descriptor": str(nsd_descriptor)[:60000], |
| 2240 | } |
| 2241 | db_scenarios.append(db_scenario) |
| 2242 | |
| 2243 | # table sce_vnfs (constituent-vnfd) |
| 2244 | vnf_index2scevnf_uuid = {} |
| 2245 | vnf_index2vnf_uuid = {} |
| 2246 | for vnf in nsd.get("constituent-vnfd").itervalues(): |
| 2247 | existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255], |
| 2248 | 'tenant_id': tenant_id}) |
| 2249 | if not existing_vnf: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2250 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':" |
| 2251 | "'{}'. Reference to a non-existing VNFD in the catalog".format( |
| 2252 | str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]), |
| 2253 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2254 | sce_vnf_uuid = str(uuid4()) |
| 2255 | uuid_list.append(sce_vnf_uuid) |
| 2256 | db_sce_vnf = { |
| 2257 | "uuid": sce_vnf_uuid, |
| 2258 | "scenario_id": scenario_uuid, |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 2259 | # "name": get_str(vnf, "member-vnf-index", 255), |
| 2260 | "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2261 | "vnf_id": existing_vnf[0]["uuid"], |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2262 | "member_vnf_index": str(vnf["member-vnf-index"]), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2263 | # TODO 'start-by-default': True |
| 2264 | } |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2265 | vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid |
| 2266 | vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2267 | db_sce_vnfs.append(db_sce_vnf) |
| 2268 | |
| 2269 | # table ip_profiles (ip-profiles) |
| 2270 | ip_profile_name2db_table_index = {} |
| 2271 | for ip_profile in nsd.get("ip-profiles").itervalues(): |
| 2272 | db_ip_profile = { |
| 2273 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 2274 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 2275 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 2276 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 2277 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 2278 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 2279 | } |
| 2280 | dns_list = [] |
| 2281 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 2282 | dns_list.append(str(dns.get("address"))) |
| 2283 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 2284 | if ip_profile["ip-profile-params"].get('security-group'): |
| 2285 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 2286 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 2287 | db_ip_profiles_index += 1 |
| 2288 | db_ip_profiles.append(db_ip_profile) |
| 2289 | |
| 2290 | # table sce_nets (internal-vld) |
| 2291 | for vld in nsd.get("vld").itervalues(): |
| 2292 | sce_net_uuid = str(uuid4()) |
| 2293 | uuid_list.append(sce_net_uuid) |
| 2294 | db_sce_net = { |
| 2295 | "uuid": sce_net_uuid, |
| 2296 | "name": get_str(vld, "name", 255), |
| 2297 | "scenario_id": scenario_uuid, |
| 2298 | # "type": #TODO |
| 2299 | "multipoint": not vld.get("type") == "ELINE", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 2300 | "osm_id": get_str(vld, "id", 255), |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2301 | # "external": #TODO |
| 2302 | "description": get_str(vld, "description", 255), |
| 2303 | } |
| 2304 | # guess type of network |
| 2305 | if vld.get("mgmt-network"): |
| 2306 | db_sce_net["type"] = "bridge" |
| 2307 | db_sce_net["external"] = True |
| 2308 | elif vld.get("provider-network").get("overlay-type") == "VLAN": |
| 2309 | db_sce_net["type"] = "data" |
| 2310 | else: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2311 | # later on it will be fixed to bridge or data depending on the type of interfaces attached to it |
| 2312 | db_sce_net["type"] = None |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2313 | db_sce_nets.append(db_sce_net) |
| 2314 | |
| 2315 | # ip-profile, link db_ip_profile with db_sce_net |
| 2316 | if vld.get("ip-profile-ref"): |
| 2317 | ip_profile_name = vld.get("ip-profile-ref") |
| 2318 | if ip_profile_name not in ip_profile_name2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2319 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'." |
| 2320 | " Reference to a non-existing 'ip_profiles'".format( |
| 2321 | str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| 2322 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2323 | 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] | 2324 | elif vld.get("vim-network-name"): |
| 2325 | db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2326 | |
| 2327 | # table sce_interfaces (vld:vnfd-connection-point-ref) |
| 2328 | for iface in vld.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2329 | vnf_index = str(iface['member-vnf-index-ref']) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2330 | # check correct parameters |
| 2331 | if vnf_index not in vnf_index2vnf_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2332 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2333 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2334 | "'nsd':'constituent-vnfd'".format( |
| 2335 | str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])), |
| 2336 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2337 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2338 | 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] | 2339 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2340 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2341 | 'external_name': get_str(iface, "vnfd-connection-point-ref", |
| 2342 | 255)}) |
| 2343 | if not existing_ifaces: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2344 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2345 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2346 | "connection-point name at VNFD '{}'".format( |
| 2347 | str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2348 | str(iface.get("vnfd-id-ref"))[:255]), |
| 2349 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2350 | interface_uuid = existing_ifaces[0]["uuid"] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2351 | if existing_ifaces[0]["iface_type"] == "data" and not db_sce_net["type"]: |
| 2352 | db_sce_net["type"] = "data" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2353 | sce_interface_uuid = str(uuid4()) |
| 2354 | uuid_list.append(sce_net_uuid) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2355 | iface_ip_address = None |
| 2356 | if iface.get("ip-address"): |
| 2357 | iface_ip_address = str(iface.get("ip-address")) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2358 | db_sce_interface = { |
| 2359 | "uuid": sce_interface_uuid, |
| 2360 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2361 | "sce_net_id": sce_net_uuid, |
| 2362 | "interface_id": interface_uuid, |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 2363 | "ip_address": iface_ip_address, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2364 | } |
| 2365 | db_sce_interfaces.append(db_sce_interface) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2366 | if not db_sce_net["type"]: |
| 2367 | db_sce_net["type"] = "bridge" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2368 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2369 | # table sce_vnffgs (vnffgd) |
| 2370 | for vnffg in nsd.get("vnffgd").itervalues(): |
| 2371 | sce_vnffg_uuid = str(uuid4()) |
| 2372 | uuid_list.append(sce_vnffg_uuid) |
| 2373 | db_sce_vnffg = { |
| 2374 | "uuid": sce_vnffg_uuid, |
| 2375 | "name": get_str(vnffg, "name", 255), |
| 2376 | "scenario_id": scenario_uuid, |
| 2377 | "vendor": get_str(vnffg, "vendor", 255), |
| 2378 | "description": get_str(vld, "description", 255), |
| 2379 | } |
| 2380 | db_sce_vnffgs.append(db_sce_vnffg) |
| 2381 | |
| 2382 | # deal with rsps |
| 2383 | db_sce_rsps = [] |
| 2384 | for rsp in vnffg.get("rsp").itervalues(): |
| 2385 | sce_rsp_uuid = str(uuid4()) |
| 2386 | uuid_list.append(sce_rsp_uuid) |
| 2387 | db_sce_rsp = { |
| 2388 | "uuid": sce_rsp_uuid, |
| 2389 | "name": get_str(rsp, "name", 255), |
| 2390 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2391 | "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code |
| 2392 | } |
| 2393 | db_sce_rsps.append(db_sce_rsp) |
| 2394 | db_sce_rsp_hops = [] |
| 2395 | for iface in rsp.get("vnfd-connection-point-ref").itervalues(): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2396 | vnf_index = str(iface['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2397 | if_order = int(iface['order']) |
| 2398 | # check correct parameters |
| 2399 | if vnf_index not in vnf_index2vnf_uuid: |
| 2400 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2401 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2402 | "'nsd':'constituent-vnfd'".format( |
| 2403 | str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])), |
| 2404 | HTTP_Bad_Request) |
| 2405 | |
| 2406 | existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2407 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2408 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2409 | 'external_name': get_str(iface, "vnfd-connection-point-ref", |
| 2410 | 255)}) |
| 2411 | if not existing_ifaces: |
| 2412 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2413 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2414 | "connection-point name at VNFD '{}'".format( |
| 2415 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2416 | str(iface.get("vnfd-id-ref"))[:255]), |
| 2417 | HTTP_Bad_Request) |
| 2418 | interface_uuid = existing_ifaces[0]["uuid"] |
| 2419 | sce_rsp_hop_uuid = str(uuid4()) |
| 2420 | uuid_list.append(sce_rsp_hop_uuid) |
| 2421 | db_sce_rsp_hop = { |
| 2422 | "uuid": sce_rsp_hop_uuid, |
| 2423 | "if_order": if_order, |
| 2424 | "interface_id": interface_uuid, |
| 2425 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2426 | "sce_rsp_id": sce_rsp_uuid, |
| 2427 | } |
| 2428 | db_sce_rsp_hops.append(db_sce_rsp_hop) |
| 2429 | |
| 2430 | # deal with classifiers |
| 2431 | db_sce_classifiers = [] |
| 2432 | for classifier in vnffg.get("classifier").itervalues(): |
| 2433 | sce_classifier_uuid = str(uuid4()) |
| 2434 | uuid_list.append(sce_classifier_uuid) |
| 2435 | |
| 2436 | # source VNF |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2437 | vnf_index = str(classifier['member-vnf-index-ref']) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2438 | if vnf_index not in vnf_index2vnf_uuid: |
| 2439 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point" |
| 2440 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2441 | "'nsd':'constituent-vnfd'".format( |
| 2442 | str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])), |
| 2443 | HTTP_Bad_Request) |
| 2444 | existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',), |
| 2445 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2446 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2447 | 'external_name': get_str(classifier, "vnfd-connection-point-ref", |
| 2448 | 255)}) |
| 2449 | if not existing_ifaces: |
| 2450 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point" |
| 2451 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2452 | "connection-point name at VNFD '{}'".format( |
| 2453 | str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2454 | str(iface.get("vnfd-id-ref"))[:255]), |
| 2455 | HTTP_Bad_Request) |
| 2456 | interface_uuid = existing_ifaces[0]["uuid"] |
| 2457 | |
| 2458 | db_sce_classifier = { |
| 2459 | "uuid": sce_classifier_uuid, |
| 2460 | "name": get_str(classifier, "name", 255), |
| 2461 | "sce_vnffg_id": sce_vnffg_uuid, |
| 2462 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2463 | "interface_id": interface_uuid, |
| 2464 | } |
| 2465 | rsp_id = get_str(classifier, "rsp-id-ref", 255) |
| 2466 | rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None) |
| 2467 | db_sce_classifier["sce_rsp_id"] = rsp["uuid"] |
| 2468 | db_sce_classifiers.append(db_sce_classifier) |
| 2469 | |
| 2470 | db_sce_classifier_matches = [] |
| 2471 | for match in classifier.get("match-attributes").itervalues(): |
| 2472 | sce_classifier_match_uuid = str(uuid4()) |
| 2473 | uuid_list.append(sce_classifier_match_uuid) |
| 2474 | db_sce_classifier_match = { |
| 2475 | "uuid": sce_classifier_match_uuid, |
| 2476 | "ip_proto": get_str(match, "ip-proto", 2), |
| 2477 | "source_ip": get_str(match, "source-ip-address", 16), |
| 2478 | "destination_ip": get_str(match, "destination-ip-address", 16), |
| 2479 | "source_port": get_str(match, "source-port", 5), |
| 2480 | "destination_port": get_str(match, "destination-port", 5), |
| 2481 | "sce_classifier_id": sce_classifier_uuid, |
| 2482 | } |
| 2483 | db_sce_classifier_matches.append(db_sce_classifier_match) |
| 2484 | # TODO: vnf/cp keys |
| 2485 | |
| 2486 | # remove unneeded id's in sce_rsps |
| 2487 | for rsp in db_sce_rsps: |
| 2488 | rsp.pop('id') |
| 2489 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2490 | db_tables = [ |
| 2491 | {"scenarios": db_scenarios}, |
| 2492 | {"sce_nets": db_sce_nets}, |
| 2493 | {"ip_profiles": db_ip_profiles}, |
| 2494 | {"sce_vnfs": db_sce_vnfs}, |
| 2495 | {"sce_interfaces": db_sce_interfaces}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2496 | {"sce_vnffgs": db_sce_vnffgs}, |
| 2497 | {"sce_rsps": db_sce_rsps}, |
| 2498 | {"sce_rsp_hops": db_sce_rsp_hops}, |
| 2499 | {"sce_classifiers": db_sce_classifiers}, |
| 2500 | {"sce_classifier_matches": db_sce_classifier_matches}, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2501 | ] |
| 2502 | |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2503 | logger.debug("new_nsd_v3 done: %s", |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2504 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 2505 | mydb.new_rows(db_tables, uuid_list) |
| 2506 | return nsd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2507 | except NfvoException: |
| 2508 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2509 | except Exception as e: |
| 2510 | logger.error("Exception {}".format(e)) |
| 2511 | raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request) |
| 2512 | |
| 2513 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2514 | def edit_scenario(mydb, tenant_id, scenario_id, data): |
| 2515 | data["uuid"] = scenario_id |
| 2516 | data["tenant_id"] = tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2517 | c = mydb.edit_scenario( data ) |
| 2518 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2519 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2520 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2521 | 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] | 2522 | #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] | 2523 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant) |
| 2524 | vims = {datacenter_id: myvim} |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2525 | myvim_tenant = myvim['tenant_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2526 | datacenter_name = myvim['name'] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2527 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2528 | rollbackList=[] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2529 | try: |
| 2530 | #print "Checking that the scenario_id exists and getting the scenario dictionary" |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2531 | scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2532 | scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] } |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2533 | scenarioDict['datacenter_id'] = datacenter_id |
| 2534 | #print '================scenarioDict=======================' |
| 2535 | #print json.dumps(scenarioDict, indent=4) |
| 2536 | #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] | 2537 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2538 | logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs'])) |
| 2539 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2540 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2541 | auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id |
| 2542 | auxNetDict['scenario'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2543 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2544 | logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM") |
| 2545 | for sce_net in scenarioDict['nets']: |
| 2546 | #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2547 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2548 | myNetName = "%s.%s" % (instance_scenario_name, sce_net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2549 | myNetName = myNetName[0:255] #limit length |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2550 | myNetType = sce_net['type'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2551 | myNetDict = {} |
| 2552 | myNetDict["name"] = myNetName |
| 2553 | myNetDict["type"] = myNetType |
| 2554 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2555 | myNetIPProfile = sce_net.get('ip_profile', None) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2556 | #TODO: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2557 | #We should use the dictionary as input parameter for new_network |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2558 | #print myNetDict |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2559 | if not sce_net["external"]: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2560 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2561 | #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id) |
| 2562 | sce_net['vim_id'] = network_id |
| 2563 | auxNetDict['scenario'][sce_net['uuid']] = network_id |
| 2564 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2565 | sce_net["created"] = True |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2566 | else: |
| 2567 | if sce_net['vim_id'] == None: |
| 2568 | error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name']) |
| 2569 | _, message = rollback(mydb, vims, rollbackList) |
| 2570 | logger.error("nfvo.start_scenario: %s", error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2571 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2572 | logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id']) |
| 2573 | auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2574 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2575 | logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM") |
| 2576 | #For each vnf net, we create it and we add it to instanceNetlist. |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2577 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2578 | for sce_vnf in scenarioDict['vnfs']: |
| 2579 | for net in sce_vnf['nets']: |
| 2580 | #print "Net name: %s. Description: %s" % (net["name"], net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2581 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2582 | myNetName = "%s.%s" % (instance_scenario_name,net['name']) |
| 2583 | myNetName = myNetName[0:255] #limit length |
| 2584 | myNetType = net['type'] |
| 2585 | myNetDict = {} |
| 2586 | myNetDict["name"] = myNetName |
| 2587 | myNetDict["type"] = myNetType |
| 2588 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2589 | myNetIPProfile = net.get('ip_profile', None) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2590 | #print myNetDict |
| 2591 | #TODO: |
| 2592 | #We should use the dictionary as input parameter for new_network |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2593 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2594 | #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id) |
| 2595 | net['vim_id'] = network_id |
| 2596 | if sce_vnf['uuid'] not in auxNetDict: |
| 2597 | auxNetDict[sce_vnf['uuid']] = {} |
| 2598 | auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id |
| 2599 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2600 | net["created"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2601 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2602 | #print "auxNetDict:" |
| 2603 | #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2604 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2605 | logger.debug("start_scenario 3. Creating new vm instances in the VIM") |
| 2606 | #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 2607 | i = 0 |
| 2608 | for sce_vnf in scenarioDict['vnfs']: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2609 | vnf_availability_zones = [] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2610 | for vm in sce_vnf['vms']: |
| 2611 | vm_av = vm.get('availability_zone') |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2612 | if vm_av and vm_av not in vnf_availability_zones: |
| 2613 | vnf_availability_zones.append(vm_av) |
| 2614 | |
| 2615 | # check if there is enough availability zones available at vim level. |
| 2616 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 2617 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| 2618 | raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request) |
| 2619 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2620 | for vm in sce_vnf['vms']: |
| 2621 | i += 1 |
| 2622 | myVMDict = {} |
| 2623 | #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name']) |
| tierno | ae65a48 | 2016-11-24 16:20:05 +0100 | [diff] [blame] | 2624 | myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2625 | #myVMDict['description'] = vm['description'] |
| 2626 | myVMDict['description'] = myVMDict['name'][0:99] |
| 2627 | if not startvms: |
| 2628 | myVMDict['start'] = "no" |
| 2629 | myVMDict['name'] = myVMDict['name'][0:255] #limit name length |
| 2630 | #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2631 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2632 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2633 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2634 | image_id = create_or_use_image(mydb, vims, image_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2635 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2636 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2637 | #create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2638 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2639 | if flavor_dict['extended']!=None: |
| 2640 | flavor_dict['extended']= yaml.load(flavor_dict['extended']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2641 | flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2642 | vm['vim_flavor_id'] = flavor_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2643 | |
| 2644 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2645 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 2646 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 2647 | myVMDict['networks'] = [] |
| 2648 | for iface in vm['interfaces']: |
| 2649 | netDict = {} |
| 2650 | if iface['type']=="data": |
| 2651 | netDict['type'] = iface['model'] |
| 2652 | elif "model" in iface and iface["model"]!=None: |
| 2653 | netDict['model']=iface['model'] |
| 2654 | #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model |
| 2655 | #discover type of interface looking at flavor |
| 2656 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 2657 | for flavor_iface in numa.get('interfaces',[]): |
| 2658 | if flavor_iface.get('name') == iface['internal_name']: |
| 2659 | if flavor_iface['dedicated'] == 'yes': |
| 2660 | netDict['type']="PF" #passthrough |
| 2661 | elif flavor_iface['dedicated'] == 'no': |
| 2662 | netDict['type']="VF" #siov |
| 2663 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 2664 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 2665 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 2666 | break; |
| 2667 | netDict["use"]=iface['type'] |
| 2668 | if netDict["use"]=="data" and not netDict.get("type"): |
| 2669 | #print "netDict", netDict |
| 2670 | #print "iface", iface |
| 2671 | 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']) |
| 2672 | if flavor_dict.get('extended')==None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2673 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 2674 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2675 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2676 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2677 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 2678 | netDict["type"]="virtual" |
| 2679 | if "vpci" in iface and iface["vpci"] is not None: |
| 2680 | netDict['vpci'] = iface['vpci'] |
| 2681 | if "mac" in iface and iface["mac"] is not None: |
| 2682 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 2683 | if "port-security" in iface and iface["port-security"] is not None: |
| 2684 | netDict['port_security'] = iface['port-security'] |
| 2685 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 2686 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2687 | netDict['name'] = iface['internal_name'] |
| 2688 | if iface['net_id'] is None: |
| 2689 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2690 | #print iface |
| 2691 | #print vnf_iface |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2692 | if vnf_iface['interface_id']==iface['uuid']: |
| 2693 | netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ] |
| 2694 | break |
| 2695 | else: |
| 2696 | netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| 2697 | #skip bridge ifaces not connected to any net |
| 2698 | #if 'net_id' not in netDict or netDict['net_id']==None: |
| 2699 | # continue |
| 2700 | myVMDict['networks'].append(netDict) |
| 2701 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 2702 | #print myVMDict['name'] |
| 2703 | #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 2704 | #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 2705 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2706 | |
| 2707 | if 'availability_zone' in myVMDict: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2708 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2709 | else: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2710 | av_index = None |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2711 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2712 | vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None), |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2713 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2714 | availability_zone_index=av_index, |
| 2715 | availability_zone_list=vnf_availability_zones) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2716 | #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id) |
| 2717 | vm['vim_id'] = vm_id |
| 2718 | rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id}) |
| 2719 | #put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 2720 | for net in myVMDict['networks']: |
| 2721 | if "vim_id" in net: |
| 2722 | for iface in vm['interfaces']: |
| 2723 | if net["name"]==iface["internal_name"]: |
| 2724 | iface["vim_id"]=net["vim_id"] |
| 2725 | break |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2726 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2727 | logger.debug("start scenario Deployment done") |
| 2728 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| 2729 | #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] | 2730 | instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict) |
| 2731 | return mydb.get_instance_scenario(instance_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2732 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2733 | except (db_base_Exception, vimconn.vimconnException) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2734 | _, message = rollback(mydb, vims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2735 | if isinstance(e, db_base_Exception): |
| 2736 | error_text = "Exception at database" |
| 2737 | else: |
| 2738 | error_text = "Exception at VIM" |
| 2739 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 2740 | #logger.error("start_scenario %s", error_text) |
| 2741 | raise NfvoException(error_text, e.http_code) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2742 | |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2743 | def unify_cloud_config(cloud_config_preserve, cloud_config): |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2744 | """ join the cloud config information into cloud_config_preserve. |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2745 | In case of conflict cloud_config_preserve preserves |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2746 | None is allowed |
| 2747 | """ |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2748 | if not cloud_config_preserve and not cloud_config: |
| 2749 | return None |
| 2750 | |
| 2751 | new_cloud_config = {"key-pairs":[], "users":[]} |
| 2752 | # key-pairs |
| 2753 | if cloud_config_preserve: |
| 2754 | for key in cloud_config_preserve.get("key-pairs", () ): |
| 2755 | if key not in new_cloud_config["key-pairs"]: |
| 2756 | new_cloud_config["key-pairs"].append(key) |
| 2757 | if cloud_config: |
| 2758 | for key in cloud_config.get("key-pairs", () ): |
| 2759 | if key not in new_cloud_config["key-pairs"]: |
| 2760 | new_cloud_config["key-pairs"].append(key) |
| 2761 | if not new_cloud_config["key-pairs"]: |
| 2762 | del new_cloud_config["key-pairs"] |
| 2763 | |
| 2764 | # users |
| 2765 | if cloud_config: |
| 2766 | new_cloud_config["users"] += cloud_config.get("users", () ) |
| 2767 | if cloud_config_preserve: |
| 2768 | new_cloud_config["users"] += cloud_config_preserve.get("users", () ) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2769 | index_to_delete = [] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2770 | users = new_cloud_config.get("users", []) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2771 | for index0 in range(0,len(users)): |
| 2772 | if index0 in index_to_delete: |
| 2773 | continue |
| 2774 | for index1 in range(index0+1,len(users)): |
| 2775 | if index1 in index_to_delete: |
| 2776 | continue |
| 2777 | if users[index0]["name"] == users[index1]["name"]: |
| 2778 | index_to_delete.append(index1) |
| 2779 | for key in users[index1].get("key-pairs",()): |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2780 | if "key-pairs" not in users[index0]: |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2781 | users[index0]["key-pairs"] = [key] |
| 2782 | elif key not in users[index0]["key-pairs"]: |
| 2783 | users[index0]["key-pairs"].append(key) |
| 2784 | index_to_delete.sort(reverse=True) |
| 2785 | for index in index_to_delete: |
| 2786 | del users[index] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2787 | if not new_cloud_config["users"]: |
| 2788 | del new_cloud_config["users"] |
| 2789 | |
| 2790 | #boot-data-drive |
| 2791 | if cloud_config and cloud_config.get("boot-data-drive") != None: |
| 2792 | new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"] |
| 2793 | if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None: |
| 2794 | new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"] |
| 2795 | |
| 2796 | # user-data |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2797 | new_cloud_config["user-data"] = [] |
| 2798 | if cloud_config and cloud_config.get("user-data"): |
| 2799 | if isinstance(cloud_config["user-data"], list): |
| 2800 | new_cloud_config["user-data"] += cloud_config["user-data"] |
| 2801 | else: |
| 2802 | new_cloud_config["user-data"].append(cloud_config["user-data"]) |
| 2803 | if cloud_config_preserve and cloud_config_preserve.get("user-data"): |
| 2804 | if isinstance(cloud_config_preserve["user-data"], list): |
| 2805 | new_cloud_config["user-data"] += cloud_config_preserve["user-data"] |
| 2806 | else: |
| 2807 | new_cloud_config["user-data"].append(cloud_config_preserve["user-data"]) |
| 2808 | if not new_cloud_config["user-data"]: |
| 2809 | del new_cloud_config["user-data"] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2810 | |
| 2811 | # config files |
| 2812 | new_cloud_config["config-files"] = [] |
| 2813 | if cloud_config and cloud_config.get("config-files") != None: |
| 2814 | new_cloud_config["config-files"] += cloud_config["config-files"] |
| 2815 | if cloud_config_preserve: |
| 2816 | for file in cloud_config_preserve.get("config-files", ()): |
| 2817 | for index in range(0, len(new_cloud_config["config-files"])): |
| 2818 | if new_cloud_config["config-files"][index]["dest"] == file["dest"]: |
| 2819 | new_cloud_config["config-files"][index] = file |
| 2820 | break |
| 2821 | else: |
| 2822 | new_cloud_config["config-files"].append(file) |
| 2823 | if not new_cloud_config["config-files"]: |
| 2824 | del new_cloud_config["config-files"] |
| 2825 | return new_cloud_config |
| 2826 | |
| 2827 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2828 | 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] | 2829 | datacenter_id = None |
| 2830 | datacenter_name = None |
| 2831 | thread = None |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2832 | try: |
| 2833 | if datacenter_tenant_id: |
| 2834 | thread_id = datacenter_tenant_id |
| 2835 | thread = vim_threads["running"].get(datacenter_tenant_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2836 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2837 | where_={"td.nfvo_tenant_id": tenant_id} |
| 2838 | if datacenter_id_name: |
| 2839 | if utils.check_valid_uuid(datacenter_id_name): |
| 2840 | datacenter_id = datacenter_id_name |
| 2841 | where_["dt.datacenter_id"] = datacenter_id |
| 2842 | else: |
| 2843 | datacenter_name = datacenter_id_name |
| 2844 | where_["d.name"] = datacenter_name |
| 2845 | if datacenter_tenant_id: |
| 2846 | where_["dt.uuid"] = datacenter_tenant_id |
| 2847 | datacenters = mydb.get_rows( |
| 2848 | SELECT=("dt.uuid as datacenter_tenant_id",), |
| 2849 | FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id " |
| 2850 | "join datacenters as d on d.uuid=dt.datacenter_id", |
| 2851 | WHERE=where_) |
| 2852 | if len(datacenters) > 1: |
| 2853 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 2854 | elif datacenters: |
| 2855 | thread_id = datacenters[0]["datacenter_tenant_id"] |
| 2856 | thread = vim_threads["running"].get(thread_id) |
| 2857 | if not thread: |
| 2858 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2859 | return thread_id, thread |
| 2860 | except db_base_Exception as e: |
| 2861 | raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2862 | |
| tierno | f575596 | 2017-07-13 15:44:34 +0200 | [diff] [blame] | 2863 | |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2864 | def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name): |
| 2865 | WHERE_dict={} |
| 2866 | if utils.check_valid_uuid(datacenter_id_name): |
| 2867 | WHERE_dict['d.uuid'] = datacenter_id_name |
| 2868 | else: |
| 2869 | WHERE_dict['d.name'] = datacenter_id_name |
| 2870 | |
| 2871 | if tenant_id: |
| 2872 | WHERE_dict['nfvo_tenant_id'] = tenant_id |
| 2873 | from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \ |
| 2874 | " dt on td.datacenter_tenant_id=dt.uuid" |
| 2875 | else: |
| 2876 | from_ = 'datacenters as d' |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 2877 | 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] | 2878 | if len(vimaccounts) == 0: |
| 2879 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2880 | elif len(vimaccounts)>1: |
| 2881 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 2882 | 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] | 2883 | return vimaccounts[0]["uuid"], vimaccounts[0]["name"] |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2884 | |
| 2885 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2886 | 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] | 2887 | datacenter_id = None |
| 2888 | datacenter_name = None |
| 2889 | if datacenter_id_name: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2890 | if utils.check_valid_uuid(datacenter_id_name): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2891 | datacenter_id = datacenter_id_name |
| 2892 | else: |
| 2893 | datacenter_name = datacenter_id_name |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2894 | vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2895 | if len(vims) == 0: |
| 2896 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2897 | elif len(vims)>1: |
| 2898 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 2899 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 2900 | return vims.keys()[0], vims.values()[0] |
| 2901 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2902 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2903 | def update(d, u): |
| 2904 | '''Takes dict d and updates it with the values in dict u.''' |
| 2905 | '''It merges all depth levels''' |
| 2906 | for k, v in u.iteritems(): |
| 2907 | if isinstance(v, collections.Mapping): |
| 2908 | r = update(d.get(k, {}), v) |
| 2909 | d[k] = r |
| 2910 | else: |
| 2911 | d[k] = u[k] |
| 2912 | return d |
| 2913 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2914 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2915 | def create_instance(mydb, tenant_id, instance_dict): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2916 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| 2917 | # logger.debug("Creating instance...") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2918 | scenario = instance_dict["scenario"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2919 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2920 | # find main datacenter |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2921 | myvims = {} |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2922 | myvim_threads_id = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2923 | datacenter = instance_dict.get("datacenter") |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2924 | default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 2925 | myvims[default_datacenter_id] = vim |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2926 | 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] | 2927 | tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2928 | # myvim_tenant = myvim['tenant_id'] |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 2929 | rollbackList = [] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2930 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2931 | # print "Checking that the scenario exists and getting the scenario dictionary" |
| 2932 | scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id], |
| 2933 | datacenter_id=default_datacenter_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2934 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2935 | # logger.debug(">>>>>> Dictionaries before merging") |
| 2936 | # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256))) |
| 2937 | # 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] | 2938 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2939 | db_instance_vnfs = [] |
| 2940 | db_instance_vms = [] |
| 2941 | db_instance_interfaces = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 2942 | db_instance_sfis = [] |
| 2943 | db_instance_sfs = [] |
| 2944 | db_instance_classifications = [] |
| 2945 | db_instance_sfps = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2946 | db_ip_profiles = [] |
| 2947 | db_vim_actions = [] |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2948 | uuid_list = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2949 | task_index = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2950 | instance_name = instance_dict["name"] |
| 2951 | instance_uuid = str(uuid4()) |
| 2952 | uuid_list.append(instance_uuid) |
| 2953 | db_instance_scenario = { |
| 2954 | "uuid": instance_uuid, |
| 2955 | "name": instance_name, |
| 2956 | "tenant_id": tenant_id, |
| 2957 | "scenario_id": scenarioDict['uuid'], |
| 2958 | "datacenter_id": default_datacenter_id, |
| 2959 | # filled bellow 'datacenter_tenant_id' |
| 2960 | "description": instance_dict.get("description"), |
| 2961 | } |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2962 | if scenarioDict.get("cloud-config"): |
| 2963 | db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], |
| 2964 | default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2965 | instance_action_id = get_task_id() |
| 2966 | db_instance_action = { |
| 2967 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 2968 | "tenant_id": tenant_id, |
| 2969 | "instance_id": instance_uuid, |
| 2970 | "description": "CREATE", |
| 2971 | } |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2972 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2973 | # Auxiliary dictionaries from x to y |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2974 | sce_net2instance = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2975 | net2task_id = {'scenario': {}} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2976 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 2977 | def ip_profile_IM2RO(ip_profile_im): |
| 2978 | # translate from input format to database format |
| 2979 | ip_profile_ro = {} |
| 2980 | if 'subnet-address' in ip_profile_im: |
| 2981 | ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address'] |
| 2982 | if 'ip-version' in ip_profile_im: |
| 2983 | ip_profile_ro['ip_version'] = ip_profile_im['ip-version'] |
| 2984 | if 'gateway-address' in ip_profile_im: |
| 2985 | ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address'] |
| 2986 | if 'dns-address' in ip_profile_im: |
| 2987 | ip_profile_ro['dns_address'] = ip_profile_im['dns-address'] |
| 2988 | if isinstance(ip_profile_ro['dns_address'], (list, tuple)): |
| 2989 | ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address']) |
| 2990 | if 'dhcp' in ip_profile_im: |
| 2991 | ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address') |
| 2992 | ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True) |
| 2993 | ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count') |
| 2994 | return ip_profile_ro |
| 2995 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2996 | # logger.debug("Creating instance from scenario-dict:\n%s", |
| 2997 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2998 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2999 | # 0 check correct parameters |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3000 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3001 | for scenario_net in scenarioDict['nets']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3002 | 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] | 3003 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3004 | else: |
| 3005 | 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] | 3006 | HTTP_Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3007 | if "sites" not in net_instance_desc: |
| 3008 | net_instance_desc["sites"] = [ {} ] |
| 3009 | site_without_datacenter_field = False |
| 3010 | for site in net_instance_desc["sites"]: |
| 3011 | if site.get("datacenter"): |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3012 | site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"]) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3013 | if site["datacenter"] not in myvims: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3014 | # Add this datacenter to myvims |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3015 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"]) |
| 3016 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3017 | myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"]) |
| 3018 | site["datacenter"] = d # change name to id |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3019 | else: |
| 3020 | if site_without_datacenter_field: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3021 | raise NfvoException("Found more than one entries without datacenter field at " |
| 3022 | "instance:networks:{}:sites".format(net_name), HTTP_Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3023 | site_without_datacenter_field = True |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3024 | site["datacenter"] = default_datacenter_id # change name to id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3025 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3026 | for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3027 | for scenario_vnf in scenarioDict['vnfs']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3028 | 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] | 3029 | break |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3030 | else: |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 3031 | 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] | 3032 | if "datacenter" in vnf_instance_desc: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3033 | # Add this datacenter to myvims |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 3034 | 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] | 3035 | if vnf_instance_desc["datacenter"] not in myvims: |
| 3036 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| 3037 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3038 | 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] | 3039 | scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"] |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 3040 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3041 | for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems(): |
| 3042 | for scenario_net in scenario_vnf['nets']: |
| 3043 | if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]: |
| 3044 | break |
| 3045 | else: |
| 3046 | raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), HTTP_Bad_Request) |
| 3047 | if net_instance_desc.get("vim-network-name"): |
| 3048 | scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"] |
| 3049 | if net_instance_desc.get("name"): |
| 3050 | scenario_net["name"] = net_instance_desc["name"] |
| 3051 | if 'ip-profile' in net_instance_desc: |
| 3052 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| 3053 | if 'ip_profile' not in scenario_net: |
| 3054 | scenario_net['ip_profile'] = ipprofile_db |
| 3055 | else: |
| 3056 | update(scenario_net['ip_profile'], ipprofile_db) |
| 3057 | |
| 3058 | for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems(): |
| 3059 | for scenario_vm in scenario_vnf['vms']: |
| 3060 | if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]: |
| 3061 | break |
| 3062 | else: |
| 3063 | raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), HTTP_Bad_Request) |
| 3064 | scenario_vm["instance_parameters"] = vdu_instance_desc |
| 3065 | for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems(): |
| 3066 | for scenario_interface in scenario_vm['interfaces']: |
| 3067 | if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]: |
| 3068 | scenario_interface.update(iface_instance_desc) |
| 3069 | break |
| 3070 | else: |
| 3071 | raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), HTTP_Bad_Request) |
| 3072 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3073 | # 0.1 parse cloud-config parameters |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 3074 | 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] | 3075 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3076 | # 0.2 merge instance information into scenario |
| 3077 | # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict) |
| 3078 | # However, this is not possible yet. |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3079 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3080 | for scenario_net in scenarioDict['nets']: |
| 3081 | if net_name == scenario_net["name"]: |
| 3082 | if 'ip-profile' in net_instance_desc: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3083 | ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile']) |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3084 | if 'ip_profile' not in scenario_net: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3085 | scenario_net['ip_profile'] = ipprofile_db |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 3086 | else: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 3087 | update(scenario_net['ip_profile'], ipprofile_db) |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3088 | for interface in net_instance_desc.get('interfaces', ()): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3089 | if 'ip_address' in interface: |
| 3090 | for vnf in scenarioDict['vnfs']: |
| 3091 | if interface['vnf'] == vnf['name']: |
| 3092 | for vnf_interface in vnf['interfaces']: |
| 3093 | if interface['vnf_interface'] == vnf_interface['external_name']: |
| tierno | 41a6981 | 2018-02-16 14:34:33 +0100 | [diff] [blame] | 3094 | vnf_interface['ip_address'] = interface['ip_address'] |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3095 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3096 | # logger.debug(">>>>>>>> Merged dictionary") |
| 3097 | # logger.debug("Creating instance scenario-dict MERGED:\n%s", |
| 3098 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3099 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3100 | # 1. Creating new nets (sce_nets) in the VIM" |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3101 | number_mgmt_networks = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3102 | db_instance_nets = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3103 | for sce_net in scenarioDict['nets']: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3104 | # get involved datacenters where this network need to be created |
| 3105 | involved_datacenters = [] |
| 3106 | for sce_vnf in scenarioDict.get("vnfs"): |
| 3107 | vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id) |
| 3108 | if vnf_datacenter in involved_datacenters: |
| 3109 | continue |
| 3110 | if sce_vnf.get("interfaces"): |
| 3111 | for sce_vnf_ifaces in sce_vnf["interfaces"]: |
| 3112 | if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]: |
| 3113 | involved_datacenters.append(vnf_datacenter) |
| 3114 | break |
| 3115 | |
| 3116 | descriptor_net = {} |
| 3117 | if instance_dict.get("networks") and instance_dict["networks"].get(sce_net["name"]): |
| 3118 | descriptor_net = instance_dict["networks"][sce_net["name"]] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3119 | net_name = descriptor_net.get("vim-network-name") |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3120 | sce_net2instance[sce_net['uuid']] = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3121 | net2task_id['scenario'][sce_net['uuid']] = {} |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3122 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3123 | if sce_net["external"]: |
| 3124 | number_mgmt_networks += 1 |
| 3125 | |
| 3126 | for datacenter_id in involved_datacenters: |
| 3127 | netmap_use = None |
| 3128 | netmap_create = None |
| 3129 | if descriptor_net.get("sites"): |
| 3130 | for site in descriptor_net["sites"]: |
| 3131 | if site.get("datacenter") == datacenter_id: |
| 3132 | netmap_use = site.get("netmap-use") |
| 3133 | netmap_create = site.get("netmap-create") |
| 3134 | break |
| 3135 | |
| 3136 | vim = myvims[datacenter_id] |
| 3137 | myvim_thread_id = myvim_threads_id[datacenter_id] |
| 3138 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3139 | net_type = sce_net['type'] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3140 | lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3141 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3142 | if not net_name: |
| 3143 | if sce_net["external"]: |
| 3144 | net_name = sce_net["name"] |
| 3145 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3146 | net_name = "{}-{}".format(instance_name, sce_net["name"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3147 | net_name = net_name[:255] # limit length |
| 3148 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3149 | if netmap_use or netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3150 | create_network = False |
| 3151 | lookfor_network = False |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3152 | if netmap_use: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3153 | lookfor_network = True |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3154 | if utils.check_valid_uuid(netmap_use): |
| 3155 | lookfor_filter["id"] = netmap_use |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3156 | else: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3157 | lookfor_filter["name"] = netmap_use |
| 3158 | if netmap_create: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3159 | create_network = True |
| 3160 | net_vim_name = net_name |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3161 | if isinstance(netmap_create, str): |
| 3162 | net_vim_name = netmap_create |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3163 | elif sce_net.get("vim_network_name"): |
| 3164 | create_network = False |
| 3165 | lookfor_network = True |
| 3166 | lookfor_filter["name"] = sce_net.get("vim_network_name") |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 3167 | elif sce_net["external"]: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3168 | if sce_net['vim_id'] is not None: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3169 | # there is a netmap at datacenter_nets database # TODO REVISE!!!! |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3170 | create_network = False |
| 3171 | lookfor_network = True |
| 3172 | lookfor_filter["id"] = sce_net['vim_id'] |
| tierno | 8f79ea1 | 2018-05-03 17:37:40 +0200 | [diff] [blame] | 3173 | elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"): |
| 3174 | if number_mgmt_networks > 1: |
| 3175 | raise NfvoException("Found several VLD of type mgmt. " |
| 3176 | "You must concrete what vim-network must be use for each one", |
| 3177 | HTTP_Bad_Request) |
| 3178 | create_network = False |
| 3179 | lookfor_network = True |
| 3180 | if vim["config"].get("management_network_id"): |
| 3181 | lookfor_filter["id"] = vim["config"]["management_network_id"] |
| 3182 | else: |
| 3183 | lookfor_filter["name"] = vim["config"]["management_network_name"] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3184 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3185 | # 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] | 3186 | create_network = True |
| 3187 | lookfor_network = True |
| 3188 | lookfor_filter["name"] = sce_net["name"] |
| 3189 | net_vim_name = sce_net["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3190 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3191 | net_vim_name = net_name |
| 3192 | create_network = True |
| 3193 | lookfor_network = False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3194 | |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3195 | task_extra = {} |
| 3196 | if create_network: |
| 3197 | task_action = "CREATE" |
| 3198 | task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None)) |
| 3199 | if lookfor_network: |
| 3200 | task_extra["find"] = (lookfor_filter,) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3201 | elif lookfor_network: |
| 3202 | task_action = "FIND" |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3203 | task_extra["params"] = (lookfor_filter,) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3204 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3205 | # fill database content |
| 3206 | net_uuid = str(uuid4()) |
| 3207 | uuid_list.append(net_uuid) |
| 3208 | sce_net2instance[sce_net['uuid']][datacenter_id] = net_uuid |
| 3209 | db_net = { |
| 3210 | "uuid": net_uuid, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3211 | 'vim_net_id': None, |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3212 | "instance_scenario_id": instance_uuid, |
| 3213 | "sce_net_id": sce_net["uuid"], |
| 3214 | "created": create_network, |
| 3215 | 'datacenter_id': datacenter_id, |
| 3216 | 'datacenter_tenant_id': myvim_thread_id, |
| tierno | d2836fc | 2018-05-30 15:03:27 +0200 | [diff] [blame] | 3217 | 'status': 'BUILD' # if create_network else "ACTIVE" |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3218 | } |
| 3219 | db_instance_nets.append(db_net) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3220 | db_vim_action = { |
| 3221 | "instance_action_id": instance_action_id, |
| 3222 | "status": "SCHEDULED", |
| 3223 | "task_index": task_index, |
| 3224 | "datacenter_vim_id": myvim_thread_id, |
| 3225 | "action": task_action, |
| 3226 | "item": "instance_nets", |
| 3227 | "item_id": net_uuid, |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 3228 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3229 | } |
| 3230 | net2task_id['scenario'][sce_net['uuid']][datacenter_id] = task_index |
| 3231 | task_index += 1 |
| 3232 | db_vim_actions.append(db_vim_action) |
| 3233 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3234 | if 'ip_profile' in sce_net: |
| 3235 | db_ip_profile={ |
| 3236 | 'instance_net_id': net_uuid, |
| 3237 | 'ip_version': sce_net['ip_profile']['ip_version'], |
| 3238 | 'subnet_address': sce_net['ip_profile']['subnet_address'], |
| 3239 | 'gateway_address': sce_net['ip_profile']['gateway_address'], |
| 3240 | 'dns_address': sce_net['ip_profile']['dns_address'], |
| 3241 | 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'], |
| 3242 | 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'], |
| 3243 | 'dhcp_count': sce_net['ip_profile']['dhcp_count'], |
| 3244 | } |
| 3245 | db_ip_profiles.append(db_ip_profile) |
| 3246 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3247 | # Create VNFs |
| 3248 | vnf_params = { |
| 3249 | "default_datacenter_id": default_datacenter_id, |
| 3250 | "myvim_threads_id": myvim_threads_id, |
| 3251 | "instance_uuid": instance_uuid, |
| 3252 | "instance_name": instance_name, |
| 3253 | "instance_action_id": instance_action_id, |
| 3254 | "myvims": myvims, |
| 3255 | "cloud_config": cloud_config, |
| 3256 | "RO_pub_key": tenant[0].get('RO_pub_key'), |
| 3257 | } |
| 3258 | vnf_params_out = { |
| 3259 | "task_index": task_index, |
| 3260 | "uuid_list": uuid_list, |
| 3261 | "db_instance_nets": db_instance_nets, |
| 3262 | "db_vim_actions": db_vim_actions, |
| 3263 | "db_ip_profiles": db_ip_profiles, |
| 3264 | "db_instance_vnfs": db_instance_vnfs, |
| 3265 | "db_instance_vms": db_instance_vms, |
| 3266 | "db_instance_interfaces": db_instance_interfaces, |
| 3267 | "net2task_id": net2task_id, |
| 3268 | "sce_net2instance": sce_net2instance, |
| 3269 | } |
| tierno | 55d234c | 2018-07-04 18:29:21 +0200 | [diff] [blame] | 3270 | # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) |
| 3271 | for sce_vnf in scenarioDict['vnfs']: # sce_vnf_list: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3272 | instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList) |
| 3273 | task_index = vnf_params_out["task_index"] |
| 3274 | uuid_list = vnf_params_out["uuid_list"] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3275 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3276 | # Create VNFFGs |
| 3277 | # task_depends_on = [] |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3278 | for vnffg in scenarioDict['vnffgs']: |
| 3279 | for rsp in vnffg['rsps']: |
| 3280 | sfs_created = [] |
| 3281 | for cp in rsp['connection_points']: |
| 3282 | count = mydb.get_rows( |
| 3283 | SELECT=('vms.count'), |
| 3284 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h on interfaces.uuid=h.interface_id", |
| 3285 | WHERE={'h.uuid': cp['uuid']})[0]['count'] |
| 3286 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None) |
| 3287 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3288 | dependencies = [] |
| 3289 | for instance_vm in instance_vms: |
| 3290 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3291 | if action: |
| 3292 | dependencies.append(action['task_index']) |
| 3293 | # TODO: throw exception if count != len(instance_vms) |
| 3294 | # TODO: and action shouldn't ever be None |
| 3295 | sfis_created = [] |
| 3296 | for i in range(count): |
| 3297 | # create sfis |
| 3298 | sfi_uuid = str(uuid4()) |
| 3299 | uuid_list.append(sfi_uuid) |
| 3300 | db_sfi = { |
| 3301 | "uuid": sfi_uuid, |
| 3302 | "instance_scenario_id": instance_uuid, |
| 3303 | 'sce_rsp_hop_id': cp['uuid'], |
| 3304 | 'datacenter_id': datacenter_id, |
| 3305 | 'datacenter_tenant_id': myvim_thread_id, |
| 3306 | "vim_sfi_id": None, # vim thread will populate |
| 3307 | } |
| 3308 | db_instance_sfis.append(db_sfi) |
| 3309 | db_vim_action = { |
| 3310 | "instance_action_id": instance_action_id, |
| 3311 | "task_index": task_index, |
| 3312 | "datacenter_vim_id": myvim_thread_id, |
| 3313 | "action": "CREATE", |
| 3314 | "status": "SCHEDULED", |
| 3315 | "item": "instance_sfis", |
| 3316 | "item_id": sfi_uuid, |
| 3317 | "extra": yaml.safe_dump({"params": "", "depends_on": [dependencies[i]]}, |
| 3318 | default_flow_style=True, width=256) |
| 3319 | } |
| 3320 | sfis_created.append(task_index) |
| 3321 | task_index += 1 |
| 3322 | db_vim_actions.append(db_vim_action) |
| 3323 | # create sfs |
| 3324 | sf_uuid = str(uuid4()) |
| 3325 | uuid_list.append(sf_uuid) |
| 3326 | db_sf = { |
| 3327 | "uuid": sf_uuid, |
| 3328 | "instance_scenario_id": instance_uuid, |
| 3329 | 'sce_rsp_hop_id': cp['uuid'], |
| 3330 | 'datacenter_id': datacenter_id, |
| 3331 | 'datacenter_tenant_id': myvim_thread_id, |
| 3332 | "vim_sf_id": None, # vim thread will populate |
| 3333 | } |
| 3334 | db_instance_sfs.append(db_sf) |
| 3335 | db_vim_action = { |
| 3336 | "instance_action_id": instance_action_id, |
| 3337 | "task_index": task_index, |
| 3338 | "datacenter_vim_id": myvim_thread_id, |
| 3339 | "action": "CREATE", |
| 3340 | "status": "SCHEDULED", |
| 3341 | "item": "instance_sfs", |
| 3342 | "item_id": sf_uuid, |
| 3343 | "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created}, |
| 3344 | default_flow_style=True, width=256) |
| 3345 | } |
| 3346 | sfs_created.append(task_index) |
| 3347 | task_index += 1 |
| 3348 | db_vim_actions.append(db_vim_action) |
| 3349 | classifier = rsp['classifier'] |
| 3350 | |
| 3351 | # TODO the following ~13 lines can be reused for the sfi case |
| 3352 | count = mydb.get_rows( |
| 3353 | SELECT=('vms.count'), |
| 3354 | FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id", |
| 3355 | WHERE={'c.uuid': classifier['uuid']})[0]['count'] |
| 3356 | instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None) |
| 3357 | instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']] |
| 3358 | dependencies = [] |
| 3359 | for instance_vm in instance_vms: |
| 3360 | action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None) |
| 3361 | if action: |
| 3362 | dependencies.append(action['task_index']) |
| 3363 | # TODO: throw exception if count != len(instance_vms) |
| 3364 | # TODO: and action shouldn't ever be None |
| 3365 | classifications_created = [] |
| 3366 | for i in range(count): |
| 3367 | for match in classifier['matches']: |
| 3368 | # create classifications |
| 3369 | classification_uuid = str(uuid4()) |
| 3370 | uuid_list.append(classification_uuid) |
| 3371 | db_classification = { |
| 3372 | "uuid": classification_uuid, |
| 3373 | "instance_scenario_id": instance_uuid, |
| 3374 | 'sce_classifier_match_id': match['uuid'], |
| 3375 | 'datacenter_id': datacenter_id, |
| 3376 | 'datacenter_tenant_id': myvim_thread_id, |
| 3377 | "vim_classification_id": None, # vim thread will populate |
| 3378 | } |
| 3379 | db_instance_classifications.append(db_classification) |
| 3380 | classification_params = { |
| 3381 | "ip_proto": match["ip_proto"], |
| 3382 | "source_ip": match["source_ip"], |
| 3383 | "destination_ip": match["destination_ip"], |
| 3384 | "source_port": match["source_port"], |
| 3385 | "destination_port": match["destination_port"] |
| 3386 | } |
| 3387 | db_vim_action = { |
| 3388 | "instance_action_id": instance_action_id, |
| 3389 | "task_index": task_index, |
| 3390 | "datacenter_vim_id": myvim_thread_id, |
| 3391 | "action": "CREATE", |
| 3392 | "status": "SCHEDULED", |
| 3393 | "item": "instance_classifications", |
| 3394 | "item_id": classification_uuid, |
| 3395 | "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]}, |
| 3396 | default_flow_style=True, width=256) |
| 3397 | } |
| 3398 | classifications_created.append(task_index) |
| 3399 | task_index += 1 |
| 3400 | db_vim_actions.append(db_vim_action) |
| 3401 | |
| 3402 | # create sfps |
| 3403 | sfp_uuid = str(uuid4()) |
| 3404 | uuid_list.append(sfp_uuid) |
| 3405 | db_sfp = { |
| 3406 | "uuid": sfp_uuid, |
| 3407 | "instance_scenario_id": instance_uuid, |
| 3408 | 'sce_rsp_id': rsp['uuid'], |
| 3409 | 'datacenter_id': datacenter_id, |
| 3410 | 'datacenter_tenant_id': myvim_thread_id, |
| 3411 | "vim_sfp_id": None, # vim thread will populate |
| 3412 | } |
| 3413 | db_instance_sfps.append(db_sfp) |
| 3414 | db_vim_action = { |
| 3415 | "instance_action_id": instance_action_id, |
| 3416 | "task_index": task_index, |
| 3417 | "datacenter_vim_id": myvim_thread_id, |
| 3418 | "action": "CREATE", |
| 3419 | "status": "SCHEDULED", |
| 3420 | "item": "instance_sfps", |
| 3421 | "item_id": sfp_uuid, |
| 3422 | "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created}, |
| 3423 | default_flow_style=True, width=256) |
| 3424 | } |
| 3425 | task_index += 1 |
| 3426 | db_vim_actions.append(db_vim_action) |
| 3427 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3428 | scenarioDict["datacenter2tenant"] = myvim_threads_id |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3429 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3430 | db_instance_action["number_tasks"] = task_index |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3431 | db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id] |
| 3432 | db_instance_scenario['datacenter_id'] = default_datacenter_id |
| 3433 | db_tables=[ |
| 3434 | {"instance_scenarios": db_instance_scenario}, |
| 3435 | {"instance_vnfs": db_instance_vnfs}, |
| 3436 | {"instance_nets": db_instance_nets}, |
| 3437 | {"ip_profiles": db_ip_profiles}, |
| 3438 | {"instance_vms": db_instance_vms}, |
| 3439 | {"instance_interfaces": db_instance_interfaces}, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3440 | {"instance_actions": db_instance_action}, |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3441 | {"instance_sfis": db_instance_sfis}, |
| 3442 | {"instance_sfs": db_instance_sfs}, |
| 3443 | {"instance_classifications": db_instance_classifications}, |
| 3444 | {"instance_sfps": db_instance_sfps}, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3445 | {"vim_actions": db_vim_actions} |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3446 | ] |
| 3447 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3448 | logger.debug("create_instance done DB tables: %s", |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3449 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 3450 | mydb.new_rows(db_tables, uuid_list) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3451 | for myvim_thread_id in myvim_threads_id.values(): |
| 3452 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3453 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3454 | returned_instance = mydb.get_instance_scenario(instance_uuid) |
| 3455 | returned_instance["action_id"] = instance_action_id |
| 3456 | return returned_instance |
| 3457 | except (NfvoException, vimconn.vimconnException, db_base_Exception) as e: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3458 | message = rollback(mydb, myvims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3459 | if isinstance(e, db_base_Exception): |
| 3460 | error_text = "database Exception" |
| 3461 | elif isinstance(e, vimconn.vimconnException): |
| 3462 | error_text = "VIM Exception" |
| 3463 | else: |
| 3464 | error_text = "Exception" |
| 3465 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3466 | # logger.error("create_instance: %s", error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3467 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3468 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3469 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3470 | def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): |
| 3471 | default_datacenter_id = params["default_datacenter_id"] |
| 3472 | myvim_threads_id = params["myvim_threads_id"] |
| 3473 | instance_uuid = params["instance_uuid"] |
| 3474 | instance_name = params["instance_name"] |
| 3475 | instance_action_id = params["instance_action_id"] |
| 3476 | myvims = params["myvims"] |
| 3477 | cloud_config = params["cloud_config"] |
| 3478 | RO_pub_key = params["RO_pub_key"] |
| 3479 | |
| 3480 | task_index = params_out["task_index"] |
| 3481 | uuid_list = params_out["uuid_list"] |
| 3482 | db_instance_nets = params_out["db_instance_nets"] |
| 3483 | db_vim_actions = params_out["db_vim_actions"] |
| 3484 | db_ip_profiles = params_out["db_ip_profiles"] |
| 3485 | db_instance_vnfs = params_out["db_instance_vnfs"] |
| 3486 | db_instance_vms = params_out["db_instance_vms"] |
| 3487 | db_instance_interfaces = params_out["db_instance_interfaces"] |
| 3488 | net2task_id = params_out["net2task_id"] |
| 3489 | sce_net2instance = params_out["sce_net2instance"] |
| 3490 | |
| 3491 | vnf_net2instance = {} |
| 3492 | |
| 3493 | # 2. Creating new nets (vnf internal nets) in the VIM" |
| 3494 | # For each vnf net, we create it and we add it to instanceNetlist. |
| 3495 | if sce_vnf.get("datacenter"): |
| 3496 | datacenter_id = sce_vnf["datacenter"] |
| 3497 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3498 | else: |
| 3499 | datacenter_id = default_datacenter_id |
| 3500 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3501 | for net in sce_vnf['nets']: |
| 3502 | # TODO revis |
| 3503 | # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {}) |
| 3504 | # net_name = descriptor_net.get("name") |
| 3505 | net_name = None |
| 3506 | if not net_name: |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3507 | net_name = "{}-{}".format(instance_name, net["name"]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3508 | net_name = net_name[:255] # limit length |
| 3509 | net_type = net['type'] |
| 3510 | |
| 3511 | if sce_vnf['uuid'] not in vnf_net2instance: |
| 3512 | vnf_net2instance[sce_vnf['uuid']] = {} |
| 3513 | if sce_vnf['uuid'] not in net2task_id: |
| 3514 | net2task_id[sce_vnf['uuid']] = {} |
| 3515 | net2task_id[sce_vnf['uuid']][net['uuid']] = task_index |
| 3516 | |
| 3517 | # fill database content |
| 3518 | net_uuid = str(uuid4()) |
| 3519 | uuid_list.append(net_uuid) |
| 3520 | vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid |
| 3521 | db_net = { |
| 3522 | "uuid": net_uuid, |
| 3523 | 'vim_net_id': None, |
| 3524 | "instance_scenario_id": instance_uuid, |
| 3525 | "net_id": net["uuid"], |
| 3526 | "created": True, |
| 3527 | 'datacenter_id': datacenter_id, |
| 3528 | 'datacenter_tenant_id': myvim_thread_id, |
| 3529 | } |
| 3530 | db_instance_nets.append(db_net) |
| 3531 | |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3532 | if net.get("vim-network-name"): |
| 3533 | lookfor_filter = {"name": net["vim-network-name"]} |
| 3534 | task_action = "FIND" |
| 3535 | task_extra = {"params": (lookfor_filter,)} |
| 3536 | else: |
| 3537 | task_action = "CREATE" |
| 3538 | task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))} |
| 3539 | |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3540 | db_vim_action = { |
| 3541 | "instance_action_id": instance_action_id, |
| 3542 | "task_index": task_index, |
| 3543 | "datacenter_vim_id": myvim_thread_id, |
| 3544 | "status": "SCHEDULED", |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3545 | "action": task_action, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3546 | "item": "instance_nets", |
| 3547 | "item_id": net_uuid, |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3548 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3549 | } |
| 3550 | task_index += 1 |
| 3551 | db_vim_actions.append(db_vim_action) |
| 3552 | |
| 3553 | if 'ip_profile' in net: |
| 3554 | db_ip_profile = { |
| 3555 | 'instance_net_id': net_uuid, |
| 3556 | 'ip_version': net['ip_profile']['ip_version'], |
| 3557 | 'subnet_address': net['ip_profile']['subnet_address'], |
| 3558 | 'gateway_address': net['ip_profile']['gateway_address'], |
| 3559 | 'dns_address': net['ip_profile']['dns_address'], |
| 3560 | 'dhcp_enabled': net['ip_profile']['dhcp_enabled'], |
| 3561 | 'dhcp_start_address': net['ip_profile']['dhcp_start_address'], |
| 3562 | 'dhcp_count': net['ip_profile']['dhcp_count'], |
| 3563 | } |
| 3564 | db_ip_profiles.append(db_ip_profile) |
| 3565 | |
| 3566 | # print "vnf_net2instance:" |
| 3567 | # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False) |
| 3568 | |
| 3569 | # 3. Creating new vm instances in the VIM |
| 3570 | # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 3571 | ssh_access = None |
| 3572 | if sce_vnf.get('mgmt_access'): |
| 3573 | ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access') |
| 3574 | vnf_availability_zones = [] |
| 3575 | for vm in sce_vnf['vms']: |
| 3576 | vm_av = vm.get('availability_zone') |
| 3577 | if vm_av and vm_av not in vnf_availability_zones: |
| 3578 | vnf_availability_zones.append(vm_av) |
| 3579 | |
| 3580 | # check if there is enough availability zones available at vim level. |
| 3581 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 3582 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| 3583 | raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request) |
| 3584 | |
| 3585 | if sce_vnf.get("datacenter"): |
| 3586 | vim = myvims[sce_vnf["datacenter"]] |
| 3587 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| 3588 | datacenter_id = sce_vnf["datacenter"] |
| 3589 | else: |
| 3590 | vim = myvims[default_datacenter_id] |
| 3591 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| 3592 | datacenter_id = default_datacenter_id |
| 3593 | sce_vnf["datacenter_id"] = datacenter_id |
| 3594 | i = 0 |
| 3595 | |
| 3596 | vnf_uuid = str(uuid4()) |
| 3597 | uuid_list.append(vnf_uuid) |
| 3598 | db_instance_vnf = { |
| 3599 | 'uuid': vnf_uuid, |
| 3600 | 'instance_scenario_id': instance_uuid, |
| 3601 | 'vnf_id': sce_vnf['vnf_id'], |
| 3602 | 'sce_vnf_id': sce_vnf['uuid'], |
| 3603 | 'datacenter_id': datacenter_id, |
| 3604 | 'datacenter_tenant_id': myvim_thread_id, |
| 3605 | } |
| 3606 | db_instance_vnfs.append(db_instance_vnf) |
| 3607 | |
| 3608 | for vm in sce_vnf['vms']: |
| 3609 | myVMDict = {} |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 3610 | sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name'] |
| 3611 | myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3612 | myVMDict['description'] = myVMDict['name'][0:99] |
| 3613 | # if not startvms: |
| 3614 | # myVMDict['start'] = "no" |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3615 | if vm.get("instance_parameters") and vm["instance_parameters"].get("name"): |
| 3616 | myVMDict['name'] = vm["instance_parameters"].get("name") |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3617 | myVMDict['name'] = myVMDict['name'][0:255] # limit name length |
| 3618 | # create image at vim in case it not exist |
| 3619 | image_uuid = vm['image_id'] |
| 3620 | if vm.get("image_list"): |
| 3621 | for alternative_image in vm["image_list"]: |
| tierno | b643421 | 2018-04-26 16:27:47 +0200 | [diff] [blame] | 3622 | if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]: |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3623 | image_uuid = alternative_image['image_id'] |
| 3624 | break |
| 3625 | image_dict = mydb.get_table_by_uuid_name("images", image_uuid) |
| 3626 | image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True) |
| 3627 | vm['vim_image_id'] = image_id |
| 3628 | |
| 3629 | # create flavor at vim in case it not exist |
| 3630 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| 3631 | if flavor_dict['extended'] != None: |
| 3632 | flavor_dict['extended'] = yaml.load(flavor_dict['extended']) |
| 3633 | flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True) |
| 3634 | |
| 3635 | # Obtain information for additional disks |
| 3636 | extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), |
| 3637 | WHERE={'vim_id': flavor_id}) |
| 3638 | if not extended_flavor_dict: |
| 3639 | raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3640 | |
| 3641 | # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0]) |
| 3642 | myVMDict['disks'] = None |
| 3643 | extended_info = extended_flavor_dict[0]['extended'] |
| 3644 | if extended_info != None: |
| 3645 | extended_flavor_dict_yaml = yaml.load(extended_info) |
| 3646 | if 'disks' in extended_flavor_dict_yaml: |
| 3647 | myVMDict['disks'] = extended_flavor_dict_yaml['disks'] |
| tierno | 1df468d | 2018-07-06 14:25:16 +0200 | [diff] [blame] | 3648 | if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"): |
| 3649 | for disk in myVMDict['disks']: |
| 3650 | if disk.get("name") in vm["instance_parameters"]["devices"]: |
| 3651 | disk.update(vm["instance_parameters"]["devices"][disk.get("name")]) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3652 | |
| 3653 | vm['vim_flavor_id'] = flavor_id |
| 3654 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 3655 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 3656 | myVMDict['availability_zone'] = vm.get('availability_zone') |
| 3657 | myVMDict['networks'] = [] |
| 3658 | task_depends_on = [] |
| 3659 | # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true |
| 3660 | db_vm_ifaces = [] |
| 3661 | for iface in vm['interfaces']: |
| 3662 | netDict = {} |
| 3663 | if iface['type'] == "data": |
| 3664 | netDict['type'] = iface['model'] |
| 3665 | elif "model" in iface and iface["model"] != None: |
| 3666 | netDict['model'] = iface['model'] |
| 3667 | # TODO in future, remove this because mac_address will not be set, and the type of PV,VF |
| 3668 | # is obtained from iterface table model |
| 3669 | # discover type of interface looking at flavor |
| 3670 | for numa in flavor_dict.get('extended', {}).get('numas', []): |
| 3671 | for flavor_iface in numa.get('interfaces', []): |
| 3672 | if flavor_iface.get('name') == iface['internal_name']: |
| 3673 | if flavor_iface['dedicated'] == 'yes': |
| 3674 | netDict['type'] = "PF" # passthrough |
| 3675 | elif flavor_iface['dedicated'] == 'no': |
| 3676 | netDict['type'] = "VF" # siov |
| 3677 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 3678 | netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF |
| 3679 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 3680 | break |
| 3681 | netDict["use"] = iface['type'] |
| 3682 | if netDict["use"] == "data" and not netDict.get("type"): |
| 3683 | # print "netDict", netDict |
| 3684 | # print "iface", iface |
| 3685 | e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat( |
| 3686 | sce_vnf['name'], vm['name'], iface['internal_name']) |
| 3687 | if flavor_dict.get('extended') == None: |
| 3688 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 3689 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| 3690 | else: |
| 3691 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| 3692 | if netDict["use"] == "mgmt" or netDict["use"] == "bridge": |
| 3693 | netDict["type"] = "virtual" |
| 3694 | if iface.get("vpci"): |
| 3695 | netDict['vpci'] = iface['vpci'] |
| 3696 | if iface.get("mac"): |
| 3697 | netDict['mac_address'] = iface['mac'] |
| 3698 | if iface.get("ip_address"): |
| 3699 | netDict['ip_address'] = iface['ip_address'] |
| 3700 | if iface.get("port-security") is not None: |
| 3701 | netDict['port_security'] = iface['port-security'] |
| 3702 | if iface.get("floating-ip") is not None: |
| 3703 | netDict['floating_ip'] = iface['floating-ip'] |
| 3704 | netDict['name'] = iface['internal_name'] |
| 3705 | if iface['net_id'] is None: |
| 3706 | for vnf_iface in sce_vnf["interfaces"]: |
| 3707 | # print iface |
| 3708 | # print vnf_iface |
| 3709 | if vnf_iface['interface_id'] == iface['uuid']: |
| 3710 | netDict['net_id'] = "TASK-{}".format( |
| 3711 | net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3712 | instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id] |
| 3713 | task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id]) |
| 3714 | break |
| 3715 | else: |
| 3716 | netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3717 | instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']] |
| 3718 | task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']]) |
| 3719 | # skip bridge ifaces not connected to any net |
| 3720 | if 'net_id' not in netDict or netDict['net_id'] == None: |
| 3721 | continue |
| 3722 | myVMDict['networks'].append(netDict) |
| 3723 | db_vm_iface = { |
| 3724 | # "uuid" |
| 3725 | # 'instance_vm_id': instance_vm_uuid, |
| 3726 | "instance_net_id": instance_net_id, |
| 3727 | 'interface_id': iface['uuid'], |
| 3728 | # 'vim_interface_id': , |
| 3729 | 'type': 'external' if iface['external_name'] is not None else 'internal', |
| 3730 | 'ip_address': iface.get('ip_address'), |
| 3731 | 'mac_address': iface.get('mac'), |
| 3732 | 'floating_ip': int(iface.get('floating-ip', False)), |
| 3733 | 'port_security': int(iface.get('port-security', True)) |
| 3734 | } |
| 3735 | db_vm_ifaces.append(db_vm_iface) |
| 3736 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3737 | # print myVMDict['name'] |
| 3738 | # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 3739 | # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 3740 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3741 | |
| 3742 | # We add the RO key to cloud_config if vnf will need ssh access |
| 3743 | cloud_config_vm = cloud_config |
| 3744 | if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'): |
| 3745 | RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]} |
| 3746 | cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key) |
| 3747 | if vm.get("boot_data"): |
| 3748 | cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm) |
| 3749 | |
| 3750 | if myVMDict.get('availability_zone'): |
| 3751 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| 3752 | else: |
| 3753 | av_index = None |
| 3754 | for vm_index in range(0, vm.get('count', 1)): |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 3755 | vm_name = myVMDict['name'] + "-" + str(vm_index+1) |
| 3756 | task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None), |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3757 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm, |
| 3758 | myVMDict['disks'], av_index, vnf_availability_zones) |
| 3759 | # put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 3760 | for net in myVMDict['networks']: |
| 3761 | if "vim_id" in net: |
| 3762 | for iface in vm['interfaces']: |
| 3763 | if net["name"] == iface["internal_name"]: |
| 3764 | iface["vim_id"] = net["vim_id"] |
| 3765 | break |
| 3766 | vm_uuid = str(uuid4()) |
| 3767 | uuid_list.append(vm_uuid) |
| 3768 | db_vm = { |
| 3769 | "uuid": vm_uuid, |
| 3770 | 'instance_vnf_id': vnf_uuid, |
| 3771 | # TODO delete "vim_vm_id": vm_id, |
| 3772 | "vm_id": vm["uuid"], |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 3773 | "vim_name": vm_name, |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 3774 | # "status": |
| 3775 | } |
| 3776 | db_instance_vms.append(db_vm) |
| 3777 | |
| 3778 | iface_index = 0 |
| 3779 | for db_vm_iface in db_vm_ifaces: |
| 3780 | iface_uuid = str(uuid4()) |
| 3781 | uuid_list.append(iface_uuid) |
| 3782 | db_vm_iface_instance = { |
| 3783 | "uuid": iface_uuid, |
| 3784 | "instance_vm_id": vm_uuid |
| 3785 | } |
| 3786 | db_vm_iface_instance.update(db_vm_iface) |
| 3787 | if db_vm_iface_instance.get("ip_address"): # increment ip_address |
| 3788 | ip = db_vm_iface_instance.get("ip_address") |
| 3789 | i = ip.rfind(".") |
| 3790 | if i > 0: |
| 3791 | try: |
| 3792 | i += 1 |
| 3793 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 3794 | db_vm_iface_instance["ip_address"] = ip |
| 3795 | except: |
| 3796 | db_vm_iface_instance["ip_address"] = None |
| 3797 | db_instance_interfaces.append(db_vm_iface_instance) |
| 3798 | myVMDict['networks'][iface_index]["uuid"] = iface_uuid |
| 3799 | iface_index += 1 |
| 3800 | |
| 3801 | db_vim_action = { |
| 3802 | "instance_action_id": instance_action_id, |
| 3803 | "task_index": task_index, |
| 3804 | "datacenter_vim_id": myvim_thread_id, |
| 3805 | "action": "CREATE", |
| 3806 | "status": "SCHEDULED", |
| 3807 | "item": "instance_vms", |
| 3808 | "item_id": vm_uuid, |
| 3809 | "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on}, |
| 3810 | default_flow_style=True, width=256) |
| 3811 | } |
| 3812 | task_index += 1 |
| 3813 | db_vim_actions.append(db_vim_action) |
| 3814 | params_out["task_index"] = task_index |
| 3815 | params_out["uuid_list"] = uuid_list |
| 3816 | |
| 3817 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3818 | def delete_instance(mydb, tenant_id, instance_id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3819 | # print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3820 | instanceDict = mydb.get_instance_scenario(instance_id, tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3821 | # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3822 | tenant_id = instanceDict["tenant_id"] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3823 | # 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] | 3824 | # 1. Delete from Database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3825 | message = mydb.delete_instance_scenario(instance_id, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3826 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3827 | # 2. delete from VIM |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3828 | error_msg = "" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3829 | myvims = {} |
| 3830 | myvim_threads = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3831 | vimthread_affected = {} |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 3832 | net2vm_dependencies = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3833 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3834 | task_index = 0 |
| 3835 | instance_action_id = get_task_id() |
| 3836 | db_vim_actions = [] |
| 3837 | db_instance_action = { |
| 3838 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 3839 | "tenant_id": tenant_id, |
| 3840 | "instance_id": instance_id, |
| 3841 | "description": "DELETE", |
| 3842 | # "number_tasks": 0 # filled bellow |
| 3843 | } |
| 3844 | |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3845 | # 2.1 deleting VNFFGs |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3846 | for sfp in instanceDict.get('sfps', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3847 | vimthread_affected[sfp["datacenter_tenant_id"]] = None |
| 3848 | datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"]) |
| 3849 | if datacenter_key not in myvims: |
| 3850 | try: |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3851 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3852 | except NfvoException as e: |
| 3853 | logger.error(str(e)) |
| 3854 | myvim_thread = None |
| 3855 | myvim_threads[datacenter_key] = myvim_thread |
| 3856 | vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"], |
| 3857 | datacenter_tenant_id=sfp["datacenter_tenant_id"]) |
| 3858 | if len(vims) == 0: |
| 3859 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"])) |
| 3860 | myvims[datacenter_key] = None |
| 3861 | else: |
| 3862 | myvims[datacenter_key] = vims.values()[0] |
| 3863 | myvim = myvims[datacenter_key] |
| 3864 | myvim_thread = myvim_threads[datacenter_key] |
| 3865 | |
| 3866 | if not myvim: |
| 3867 | error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"]) |
| 3868 | continue |
| 3869 | extra = {"params": (sfp['vim_sfp_id'])} |
| 3870 | db_vim_action = { |
| 3871 | "instance_action_id": instance_action_id, |
| 3872 | "task_index": task_index, |
| 3873 | "datacenter_vim_id": sfp["datacenter_tenant_id"], |
| 3874 | "action": "DELETE", |
| 3875 | "status": "SCHEDULED", |
| 3876 | "item": "instance_sfps", |
| 3877 | "item_id": sfp["uuid"], |
| 3878 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 3879 | } |
| 3880 | task_index += 1 |
| 3881 | db_vim_actions.append(db_vim_action) |
| 3882 | |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3883 | for classification in instanceDict['classifications']: |
| 3884 | vimthread_affected[classification["datacenter_tenant_id"]] = None |
| 3885 | datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 3886 | if datacenter_key not in myvims: |
| 3887 | try: |
| 3888 | _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"]) |
| 3889 | except NfvoException as e: |
| 3890 | logger.error(str(e)) |
| 3891 | myvim_thread = None |
| 3892 | myvim_threads[datacenter_key] = myvim_thread |
| 3893 | vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"], |
| 3894 | datacenter_tenant_id=classification["datacenter_tenant_id"]) |
| 3895 | if len(vims) == 0: |
| 3896 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"], |
| 3897 | classification["datacenter_tenant_id"])) |
| 3898 | myvims[datacenter_key] = None |
| 3899 | else: |
| 3900 | myvims[datacenter_key] = vims.values()[0] |
| 3901 | myvim = myvims[datacenter_key] |
| 3902 | myvim_thread = myvim_threads[datacenter_key] |
| 3903 | |
| 3904 | if not myvim: |
| 3905 | error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'], |
| 3906 | classification["datacenter_id"]) |
| 3907 | continue |
| 3908 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"] |
| 3909 | extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on} |
| 3910 | db_vim_action = { |
| 3911 | "instance_action_id": instance_action_id, |
| 3912 | "task_index": task_index, |
| 3913 | "datacenter_vim_id": classification["datacenter_tenant_id"], |
| 3914 | "action": "DELETE", |
| 3915 | "status": "SCHEDULED", |
| 3916 | "item": "instance_classifications", |
| 3917 | "item_id": classification["uuid"], |
| 3918 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 3919 | } |
| 3920 | task_index += 1 |
| 3921 | db_vim_actions.append(db_vim_action) |
| 3922 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3923 | for sf in instanceDict.get('sfs', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3924 | vimthread_affected[sf["datacenter_tenant_id"]] = None |
| 3925 | datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"]) |
| 3926 | if datacenter_key not in myvims: |
| 3927 | try: |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3928 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3929 | except NfvoException as e: |
| 3930 | logger.error(str(e)) |
| 3931 | myvim_thread = None |
| 3932 | myvim_threads[datacenter_key] = myvim_thread |
| 3933 | vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"], |
| 3934 | datacenter_tenant_id=sf["datacenter_tenant_id"]) |
| 3935 | if len(vims) == 0: |
| 3936 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"])) |
| 3937 | myvims[datacenter_key] = None |
| 3938 | else: |
| 3939 | myvims[datacenter_key] = vims.values()[0] |
| 3940 | myvim = myvims[datacenter_key] |
| 3941 | myvim_thread = myvim_threads[datacenter_key] |
| 3942 | |
| 3943 | if not myvim: |
| 3944 | error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"]) |
| 3945 | continue |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3946 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"] |
| 3947 | extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3948 | db_vim_action = { |
| 3949 | "instance_action_id": instance_action_id, |
| 3950 | "task_index": task_index, |
| 3951 | "datacenter_vim_id": sf["datacenter_tenant_id"], |
| 3952 | "action": "DELETE", |
| 3953 | "status": "SCHEDULED", |
| 3954 | "item": "instance_sfs", |
| 3955 | "item_id": sf["uuid"], |
| 3956 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 3957 | } |
| 3958 | task_index += 1 |
| 3959 | db_vim_actions.append(db_vim_action) |
| 3960 | |
| tierno | 69b590e | 2018-03-13 18:52:23 +0100 | [diff] [blame] | 3961 | for sfi in instanceDict.get('sfis', ()): |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3962 | vimthread_affected[sfi["datacenter_tenant_id"]] = None |
| 3963 | datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"]) |
| 3964 | if datacenter_key not in myvims: |
| 3965 | try: |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3966 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3967 | except NfvoException as e: |
| 3968 | logger.error(str(e)) |
| 3969 | myvim_thread = None |
| 3970 | myvim_threads[datacenter_key] = myvim_thread |
| 3971 | vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"], |
| 3972 | datacenter_tenant_id=sfi["datacenter_tenant_id"]) |
| 3973 | if len(vims) == 0: |
| 3974 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"])) |
| 3975 | myvims[datacenter_key] = None |
| 3976 | else: |
| 3977 | myvims[datacenter_key] = vims.values()[0] |
| 3978 | myvim = myvims[datacenter_key] |
| 3979 | myvim_thread = myvim_threads[datacenter_key] |
| 3980 | |
| 3981 | if not myvim: |
| 3982 | error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"]) |
| 3983 | continue |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3984 | depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"] |
| 3985 | extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on} |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 3986 | db_vim_action = { |
| 3987 | "instance_action_id": instance_action_id, |
| 3988 | "task_index": task_index, |
| 3989 | "datacenter_vim_id": sfi["datacenter_tenant_id"], |
| 3990 | "action": "DELETE", |
| 3991 | "status": "SCHEDULED", |
| 3992 | "item": "instance_sfis", |
| 3993 | "item_id": sfi["uuid"], |
| 3994 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 3995 | } |
| 3996 | task_index += 1 |
| 3997 | db_vim_actions.append(db_vim_action) |
| 3998 | |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 3999 | # 2.2 deleting VMs |
| 4000 | # vm_fail_list=[] |
| 4001 | for sce_vnf in instanceDict['vnfs']: |
| 4002 | datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4003 | vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4004 | if datacenter_key not in myvims: |
| 4005 | try: |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4006 | _, myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4007 | except NfvoException as e: |
| 4008 | logger.error(str(e)) |
| 4009 | myvim_thread = None |
| 4010 | myvim_threads[datacenter_key] = myvim_thread |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4011 | vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"], |
| 4012 | datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4013 | if len(vims) == 0: |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4014 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], |
| 4015 | sce_vnf["datacenter_tenant_id"])) |
| 4016 | myvims[datacenter_key] = None |
| 4017 | else: |
| 4018 | myvims[datacenter_key] = vims.values()[0] |
| 4019 | myvim = myvims[datacenter_key] |
| 4020 | myvim_thread = myvim_threads[datacenter_key] |
| 4021 | |
| 4022 | for vm in sce_vnf['vms']: |
| 4023 | if not myvim: |
| 4024 | error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"]) |
| 4025 | continue |
| 4026 | sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"] |
| 4027 | db_vim_action = { |
| 4028 | "instance_action_id": instance_action_id, |
| 4029 | "task_index": task_index, |
| 4030 | "datacenter_vim_id": sce_vnf["datacenter_tenant_id"], |
| 4031 | "action": "DELETE", |
| 4032 | "status": "SCHEDULED", |
| 4033 | "item": "instance_vms", |
| 4034 | "item_id": vm["uuid"], |
| 4035 | "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies}, |
| 4036 | default_flow_style=True, width=256) |
| 4037 | } |
| 4038 | db_vim_actions.append(db_vim_action) |
| 4039 | for interface in vm["interfaces"]: |
| 4040 | if not interface.get("instance_net_id"): |
| 4041 | continue |
| 4042 | if interface["instance_net_id"] not in net2vm_dependencies: |
| 4043 | net2vm_dependencies[interface["instance_net_id"]] = [] |
| 4044 | net2vm_dependencies[interface["instance_net_id"]].append(task_index) |
| 4045 | task_index += 1 |
| 4046 | |
| 4047 | # 2.3 deleting NETS |
| 4048 | # net_fail_list=[] |
| 4049 | for net in instanceDict['nets']: |
| 4050 | vimthread_affected[net["datacenter_tenant_id"]] = None |
| 4051 | datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4052 | if datacenter_key not in myvims: |
| 4053 | try: |
| 4054 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4055 | except NfvoException as e: |
| 4056 | logger.error(str(e)) |
| 4057 | myvim_thread = None |
| 4058 | myvim_threads[datacenter_key] = myvim_thread |
| 4059 | vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"], |
| 4060 | datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 4061 | if len(vims) == 0: |
| 4062 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4063 | myvims[datacenter_key] = None |
| 4064 | else: |
| 4065 | myvims[datacenter_key] = vims.values()[0] |
| 4066 | myvim = myvims[datacenter_key] |
| 4067 | myvim_thread = myvim_threads[datacenter_key] |
| 4068 | |
| 4069 | if not myvim: |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4070 | error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"]) |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4071 | continue |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4072 | extra = {"params": (net['vim_net_id'], net['sdn_net_id'])} |
| 4073 | if net2vm_dependencies.get(net["uuid"]): |
| 4074 | extra["depends_on"] = net2vm_dependencies[net["uuid"]] |
| 4075 | sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"] |
| 4076 | if len(sfi_dependencies) > 0: |
| 4077 | if "depends_on" in extra: |
| 4078 | extra["depends_on"] += sfi_dependencies |
| 4079 | else: |
| 4080 | extra["depends_on"] = sfi_dependencies |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4081 | db_vim_action = { |
| 4082 | "instance_action_id": instance_action_id, |
| 4083 | "task_index": task_index, |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4084 | "datacenter_vim_id": net["datacenter_tenant_id"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4085 | "action": "DELETE", |
| 4086 | "status": "SCHEDULED", |
| Eduardo Sousa | bfd1fba | 2018-10-17 17:10:04 +0100 | [diff] [blame] | 4087 | "item": "instance_nets", |
| 4088 | "item_id": net["uuid"], |
| Igor D.C | caadc44 | 2017-11-06 12:48:48 +0000 | [diff] [blame] | 4089 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 4090 | } |
| 4091 | task_index += 1 |
| 4092 | db_vim_actions.append(db_vim_action) |
| 4093 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4094 | db_instance_action["number_tasks"] = task_index |
| 4095 | db_tables = [ |
| 4096 | {"instance_actions": db_instance_action}, |
| 4097 | {"vim_actions": db_vim_actions} |
| 4098 | ] |
| 4099 | |
| 4100 | logger.debug("delete_instance done DB tables: %s", |
| 4101 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4102 | mydb.new_rows(db_tables, ()) |
| 4103 | for myvim_thread_id in vimthread_affected.keys(): |
| 4104 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| 4105 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4106 | if len(error_msg) > 0: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4107 | return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\ |
| 4108 | '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4109 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4110 | return "action_id={} instance {} deleted".format(instance_action_id, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4111 | |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 4112 | def get_instance_id(mydb, tenant_id, instance_id): |
| 4113 | global ovim |
| 4114 | #check valid tenant_id |
| 4115 | check_tenant(mydb, tenant_id) |
| 4116 | #obtain data |
| 4117 | |
| 4118 | instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True) |
| 4119 | for net in instance_dict["nets"]: |
| 4120 | if net.get("sdn_net_id"): |
| 4121 | net_sdn = ovim.show_network(net["sdn_net_id"]) |
| 4122 | net["sdn_info"] = { |
| 4123 | "admin_state_up": net_sdn.get("admin_state_up"), |
| 4124 | "flows": net_sdn.get("flows"), |
| 4125 | "last_error": net_sdn.get("last_error"), |
| 4126 | "ports": net_sdn.get("ports"), |
| 4127 | "type": net_sdn.get("type"), |
| 4128 | "status": net_sdn.get("status"), |
| 4129 | "vlan": net_sdn.get("vlan"), |
| 4130 | } |
| 4131 | return instance_dict |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4132 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4133 | def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None): |
| 4134 | '''Refreshes a scenario instance. It modifies instanceDict''' |
| 4135 | '''Returns: |
| 4136 | - 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 |
| 4137 | - error_msg |
| 4138 | ''' |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4139 | # # Assumption: nfvo_tenant and instance_id were checked before entering into this function |
| 4140 | # #print "nfvo.refresh_instance begins" |
| 4141 | # #print json.dumps(instanceDict, indent=4) |
| 4142 | # |
| 4143 | # #print "Getting the VIM URL and the VIM tenant_id" |
| 4144 | # myvims={} |
| 4145 | # |
| 4146 | # # 1. Getting VIM vm and net list |
| 4147 | # vms_updated = [] #List of VM instance uuids in openmano that were updated |
| 4148 | # vms_notupdated=[] |
| 4149 | # vm_list = {} |
| 4150 | # for sce_vnf in instanceDict['vnfs']: |
| 4151 | # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 4152 | # if datacenter_key not in vm_list: |
| 4153 | # vm_list[datacenter_key] = [] |
| 4154 | # if datacenter_key not in myvims: |
| 4155 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"], |
| 4156 | # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 4157 | # if len(vims) == 0: |
| 4158 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])) |
| 4159 | # myvims[datacenter_key] = None |
| 4160 | # else: |
| 4161 | # myvims[datacenter_key] = vims.values()[0] |
| 4162 | # for vm in sce_vnf['vms']: |
| 4163 | # vm_list[datacenter_key].append(vm['vim_vm_id']) |
| 4164 | # vms_notupdated.append(vm["uuid"]) |
| 4165 | # |
| 4166 | # nets_updated = [] #List of VM instance uuids in openmano that were updated |
| 4167 | # nets_notupdated=[] |
| 4168 | # net_list = {} |
| 4169 | # for net in instanceDict['nets']: |
| 4170 | # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4171 | # if datacenter_key not in net_list: |
| 4172 | # net_list[datacenter_key] = [] |
| 4173 | # if datacenter_key not in myvims: |
| 4174 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"], |
| 4175 | # datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 4176 | # if len(vims) == 0: |
| 4177 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 4178 | # myvims[datacenter_key] = None |
| 4179 | # else: |
| 4180 | # myvims[datacenter_key] = vims.values()[0] |
| 4181 | # |
| 4182 | # net_list[datacenter_key].append(net['vim_net_id']) |
| 4183 | # nets_notupdated.append(net["uuid"]) |
| 4184 | # |
| 4185 | # # 1. Getting the status of all VMs |
| 4186 | # vm_dict={} |
| 4187 | # for datacenter_key in myvims: |
| 4188 | # if not vm_list.get(datacenter_key): |
| 4189 | # continue |
| 4190 | # failed = True |
| 4191 | # failed_message="" |
| 4192 | # if not myvims[datacenter_key]: |
| 4193 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4194 | # else: |
| 4195 | # try: |
| 4196 | # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) ) |
| 4197 | # failed = False |
| 4198 | # except vimconn.vimconnException as e: |
| 4199 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4200 | # failed_message = str(e) |
| 4201 | # if failed: |
| 4202 | # for vm in vm_list[datacenter_key]: |
| 4203 | # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4204 | # |
| 4205 | # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed |
| 4206 | # for sce_vnf in instanceDict['vnfs']: |
| 4207 | # for vm in sce_vnf['vms']: |
| 4208 | # vm_id = vm['vim_vm_id'] |
| 4209 | # interfaces = vm_dict[vm_id].pop('interfaces', []) |
| 4210 | # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE |
| 4211 | # has_mgmt_iface = False |
| 4212 | # for iface in vm["interfaces"]: |
| 4213 | # if iface["type"]=="mgmt": |
| 4214 | # has_mgmt_iface = True |
| 4215 | # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface: |
| 4216 | # vm_dict[vm_id]['status'] = "ACTIVE" |
| 4217 | # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024: |
| 4218 | # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:] |
| 4219 | # 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'): |
| 4220 | # vm['status'] = vm_dict[vm_id]['status'] |
| 4221 | # vm['error_msg'] = vm_dict[vm_id].get('error_msg') |
| 4222 | # vm['vim_info'] = vm_dict[vm_id].get('vim_info') |
| 4223 | # # 2.1. Update in openmano DB the VMs whose status changed |
| 4224 | # try: |
| 4225 | # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]}) |
| 4226 | # vms_notupdated.remove(vm["uuid"]) |
| 4227 | # if updates>0: |
| 4228 | # vms_updated.append(vm["uuid"]) |
| 4229 | # except db_base_Exception as e: |
| 4230 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4231 | # # 2.2. Update in openmano DB the interface VMs |
| 4232 | # for interface in interfaces: |
| 4233 | # #translate from vim_net_id to instance_net_id |
| 4234 | # network_id_list=[] |
| 4235 | # for net in instanceDict['nets']: |
| 4236 | # if net["vim_net_id"] == interface["vim_net_id"]: |
| 4237 | # network_id_list.append(net["uuid"]) |
| 4238 | # if not network_id_list: |
| 4239 | # continue |
| 4240 | # del interface["vim_net_id"] |
| 4241 | # try: |
| 4242 | # for network_id in network_id_list: |
| 4243 | # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id}) |
| 4244 | # except db_base_Exception as e: |
| 4245 | # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id) |
| 4246 | # |
| 4247 | # # 3. Getting the status of all nets |
| 4248 | # net_dict = {} |
| 4249 | # for datacenter_key in myvims: |
| 4250 | # if not net_list.get(datacenter_key): |
| 4251 | # continue |
| 4252 | # failed = True |
| 4253 | # failed_message = "" |
| 4254 | # if not myvims[datacenter_key]: |
| 4255 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 4256 | # else: |
| 4257 | # try: |
| 4258 | # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) ) |
| 4259 | # failed = False |
| 4260 | # except vimconn.vimconnException as e: |
| 4261 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 4262 | # failed_message = str(e) |
| 4263 | # if failed: |
| 4264 | # for net in net_list[datacenter_key]: |
| 4265 | # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 4266 | # |
| 4267 | # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed |
| 4268 | # # TODO: update nets inside a vnf |
| 4269 | # for net in instanceDict['nets']: |
| 4270 | # net_id = net['vim_net_id'] |
| 4271 | # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024: |
| 4272 | # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:] |
| 4273 | # 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'): |
| 4274 | # net['status'] = net_dict[net_id]['status'] |
| 4275 | # net['error_msg'] = net_dict[net_id].get('error_msg') |
| 4276 | # net['vim_info'] = net_dict[net_id].get('vim_info') |
| 4277 | # # 5.1. Update in openmano DB the nets whose status changed |
| 4278 | # try: |
| 4279 | # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]}) |
| 4280 | # nets_notupdated.remove(net["uuid"]) |
| 4281 | # if updated>0: |
| 4282 | # nets_updated.append(net["uuid"]) |
| 4283 | # except db_base_Exception as e: |
| 4284 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 4285 | # |
| 4286 | # # Returns appropriate output |
| 4287 | # #print "nfvo.refresh_instance finishes" |
| 4288 | # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s", |
| 4289 | # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4290 | instance_id = instanceDict['uuid'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 4291 | # if len(vms_notupdated)+len(nets_notupdated)>0: |
| 4292 | # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated) |
| 4293 | # 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] | 4294 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4295 | return 0, 'Scenario instance ' + instance_id + ' refreshed.' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4296 | |
| 4297 | def instance_action(mydb,nfvo_tenant,instance_id, action_dict): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4298 | #print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4299 | instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4300 | #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| 4301 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4302 | #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] | 4303 | vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id']) |
| 4304 | if len(vims) == 0: |
| 4305 | raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4306 | myvim = vims.values()[0] |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4307 | vm_result = {} |
| 4308 | vm_error = 0 |
| 4309 | vm_ok = 0 |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4310 | |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4311 | myvim_threads_id = {} |
| 4312 | if action_dict.get("vdu-scaling"): |
| 4313 | db_instance_vms = [] |
| 4314 | db_vim_actions = [] |
| 4315 | db_instance_interfaces = [] |
| 4316 | instance_action_id = get_task_id() |
| 4317 | db_instance_action = { |
| 4318 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 4319 | "tenant_id": nfvo_tenant, |
| 4320 | "instance_id": instance_id, |
| 4321 | "description": "SCALE", |
| 4322 | } |
| 4323 | vm_result["instance_action_id"] = instance_action_id |
| 4324 | task_index = 0 |
| 4325 | for vdu in action_dict["vdu-scaling"]: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4326 | vdu_id = vdu.get("vdu-id") |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4327 | osm_vdu_id = vdu.get("osm_vdu_id") |
| 4328 | member_vnf_index = vdu.get("member-vnf-index") |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4329 | vdu_count = vdu.get("count", 1) |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4330 | if vdu_id: |
| 4331 | target_vm = mydb.get_rows( |
| 4332 | FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid", |
| 4333 | WHERE={"vms.uuid": vdu_id}, |
| 4334 | ORDER_BY="vms.created_at" |
| 4335 | ) |
| 4336 | if not target_vm: |
| 4337 | raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), HTTP_Not_Found) |
| 4338 | else: |
| 4339 | if not osm_vdu_id and not member_vnf_index: |
| 4340 | raise NfvoException("Invalid imput vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'") |
| 4341 | target_vm = mydb.get_rows( |
| 4342 | # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"), |
| 4343 | FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\ |
| 4344 | " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\ |
| 4345 | " join vms on ivms.vm_id=vms.uuid", |
| 4346 | WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index}, |
| 4347 | ORDER_BY="ivms.created_at" |
| 4348 | ) |
| 4349 | if not target_vm: |
| 4350 | raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), HTTP_Not_Found) |
| 4351 | vdu_id = target_vm[-1]["uuid"] |
| 4352 | vm_result[vdu_id] = {"created": [], "deleted": [], "description": "scheduled"} |
| 4353 | target_vm = target_vm[-1] |
| 4354 | datacenter = target_vm["datacenter_id"] |
| 4355 | myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter) |
| 4356 | if vdu["type"] == "delete": |
| 4357 | # look for nm |
| 4358 | vm_interfaces = None |
| 4359 | for sce_vnf in instanceDict['vnfs']: |
| 4360 | for vm in sce_vnf['vms']: |
| 4361 | if vm["uuid"] == vdu_id: |
| 4362 | vm_interfaces = vm["interfaces"] |
| 4363 | break |
| 4364 | |
| 4365 | db_vim_action = { |
| 4366 | "instance_action_id": instance_action_id, |
| 4367 | "task_index": task_index, |
| 4368 | "datacenter_vim_id": target_vm["datacenter_tenant_id"], |
| 4369 | "action": "DELETE", |
| 4370 | "status": "SCHEDULED", |
| 4371 | "item": "instance_vms", |
| 4372 | "item_id": target_vm["uuid"], |
| 4373 | "extra": yaml.safe_dump({"params": vm_interfaces}, |
| 4374 | default_flow_style=True, width=256) |
| 4375 | } |
| 4376 | task_index += 1 |
| 4377 | db_vim_actions.append(db_vim_action) |
| 4378 | vm_result[vdu_id]["deleted"].append(vdu_id) |
| 4379 | # delete from database |
| 4380 | db_instance_vms.append({"TO-DELETE": vdu_id}) |
| 4381 | |
| 4382 | else: # vdu["type"] == "create": |
| 4383 | iface2iface = {} |
| 4384 | where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"} |
| 4385 | |
| 4386 | vim_action_to_clone = mydb.get_rows(FROM="vim_actions", WHERE=where) |
| 4387 | if not vim_action_to_clone: |
| 4388 | raise NfvoException("Cannot find the vim_action at database with {}".format(where), HTTP_Internal_Server_Error) |
| 4389 | vim_action_to_clone = vim_action_to_clone[0] |
| 4390 | extra = yaml.safe_load(vim_action_to_clone["extra"]) |
| 4391 | |
| 4392 | # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y |
| 4393 | # TODO do the same for flavor and image when available |
| 4394 | task_depends_on = [] |
| 4395 | task_params = extra["params"] |
| 4396 | task_params_networks = deepcopy(task_params[5]) |
| 4397 | for iface in task_params[5]: |
| 4398 | if iface["net_id"].startswith("TASK-"): |
| 4399 | if "." not in iface["net_id"]: |
| 4400 | task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4401 | iface["net_id"][5:])) |
| 4402 | iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"], |
| 4403 | iface["net_id"][5:]) |
| 4404 | else: |
| 4405 | task_depends_on.append(iface["net_id"][5:]) |
| 4406 | if "mac_address" in iface: |
| 4407 | del iface["mac_address"] |
| 4408 | |
| 4409 | vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]}) |
| 4410 | for index in range(0, vdu_count): |
| 4411 | vm_uuid = str(uuid4()) |
| 4412 | vm_name = target_vm.get('vim_name') |
| 4413 | try: |
| 4414 | suffix = vm_name.rfind("-") |
| 4415 | vm_name = vm_name[:suffix+1] + str(1 + int(vm_name[suffix+1:])) |
| 4416 | except Exception: |
| 4417 | pass |
| 4418 | db_instance_vm = { |
| 4419 | "uuid": vm_uuid, |
| 4420 | 'instance_vnf_id': target_vm['instance_vnf_id'], |
| 4421 | 'vm_id': target_vm['vm_id'], |
| 4422 | 'vim_name': vm_name |
| 4423 | } |
| 4424 | db_instance_vms.append(db_instance_vm) |
| 4425 | |
| 4426 | for vm_iface in vm_ifaces_to_clone: |
| 4427 | iface_uuid = str(uuid4()) |
| 4428 | iface2iface[vm_iface["uuid"]] = iface_uuid |
| 4429 | db_vm_iface = { |
| 4430 | "uuid": iface_uuid, |
| 4431 | 'instance_vm_id': vm_uuid, |
| 4432 | "instance_net_id": vm_iface["instance_net_id"], |
| 4433 | 'interface_id': vm_iface['interface_id'], |
| 4434 | 'type': vm_iface['type'], |
| 4435 | 'floating_ip': vm_iface['floating_ip'], |
| 4436 | 'port_security': vm_iface['port_security'] |
| 4437 | } |
| 4438 | db_instance_interfaces.append(db_vm_iface) |
| 4439 | task_params_copy = deepcopy(task_params) |
| 4440 | for iface in task_params_copy[5]: |
| 4441 | iface["uuid"] = iface2iface[iface["uuid"]] |
| 4442 | # increment ip_address |
| 4443 | if "ip_address" in iface: |
| 4444 | ip = iface.get("ip_address") |
| 4445 | i = ip.rfind(".") |
| 4446 | if i > 0: |
| 4447 | try: |
| 4448 | i += 1 |
| 4449 | ip = ip[i:] + str(int(ip[:i]) + 1) |
| 4450 | iface["ip_address"] = ip |
| 4451 | except: |
| 4452 | iface["ip_address"] = None |
| 4453 | if vm_name: |
| 4454 | task_params_copy[0] = vm_name |
| 4455 | db_vim_action = { |
| 4456 | "instance_action_id": instance_action_id, |
| 4457 | "task_index": task_index, |
| 4458 | "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"], |
| 4459 | "action": "CREATE", |
| 4460 | "status": "SCHEDULED", |
| 4461 | "item": "instance_vms", |
| 4462 | "item_id": vm_uuid, |
| 4463 | # ALF |
| 4464 | # ALF |
| 4465 | # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd. |
| 4466 | # ALF |
| 4467 | # ALF |
| 4468 | "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256) |
| 4469 | } |
| 4470 | task_index += 1 |
| 4471 | db_vim_actions.append(db_vim_action) |
| 4472 | vm_result[vdu_id]["created"].append(vm_uuid) |
| 4473 | |
| 4474 | db_instance_action["number_tasks"] = task_index |
| 4475 | db_tables = [ |
| 4476 | {"instance_vms": db_instance_vms}, |
| 4477 | {"instance_interfaces": db_instance_interfaces}, |
| 4478 | {"instance_actions": db_instance_action}, |
| 4479 | # TODO revise sfps |
| 4480 | # {"instance_sfis": db_instance_sfis}, |
| 4481 | # {"instance_sfs": db_instance_sfs}, |
| 4482 | # {"instance_classifications": db_instance_classifications}, |
| 4483 | # {"instance_sfps": db_instance_sfps}, |
| 4484 | {"vim_actions": db_vim_actions} |
| 4485 | ] |
| 4486 | logger.debug("create_vdu done DB tables: %s", |
| 4487 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 4488 | mydb.new_rows(db_tables, []) |
| 4489 | for myvim_thread in myvim_threads_id.values(): |
| 4490 | vim_threads["running"][myvim_thread].insert_task(db_vim_actions) |
| 4491 | |
| 4492 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4493 | |
| 4494 | input_vnfs = action_dict.pop("vnfs", []) |
| 4495 | input_vms = action_dict.pop("vms", []) |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4496 | 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] | 4497 | for sce_vnf in instanceDict['vnfs']: |
| 4498 | for vm in sce_vnf['vms']: |
| tierno | 92c36fd | 2018-05-04 12:21:10 +0200 | [diff] [blame] | 4499 | if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \ |
| 4500 | sce_vnf['member_vnf_index'] not in input_vnfs and \ |
| 4501 | vm['uuid'] not in input_vms and vm['name'] not in input_vms: |
| 4502 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4503 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4504 | if "add_public_key" in action_dict: |
| 4505 | mgmt_access = {} |
| 4506 | if sce_vnf.get('mgmt_access'): |
| 4507 | mgmt_access = yaml.load(sce_vnf['mgmt_access']) |
| 4508 | ssh_access = mgmt_access['config-access']['ssh-access'] |
| 4509 | tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4510 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4511 | if ssh_access['required'] and ssh_access['default-user']: |
| 4512 | if 'ip_address' in vm: |
| 4513 | mgmt_ip = vm['ip_address'].split(';') |
| 4514 | password = mgmt_access['config-access'].get('password') |
| 4515 | priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid']) |
| 4516 | myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'], |
| 4517 | action_dict['add_public_key'], |
| 4518 | password=password, ro_key=priv_RO_key) |
| 4519 | else: |
| 4520 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 4521 | HTTP_Internal_Server_Error) |
| 4522 | except KeyError: |
| 4523 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 4524 | HTTP_Internal_Server_Error) |
| 4525 | else: |
| 4526 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 4527 | HTTP_Internal_Server_Error) |
| 4528 | else: |
| 4529 | data = myvim.action_vminstance(vm['vim_vm_id'], action_dict) |
| 4530 | if "console" in action_dict: |
| 4531 | if not global_config["http_console_proxy"]: |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4532 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4533 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4534 | protocol=data["protocol"], |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4535 | ip = data["server"], |
| 4536 | port = data["port"], |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4537 | suffix = data["suffix"]), |
| 4538 | "name":vm['name'] |
| 4539 | } |
| 4540 | vm_ok +=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4541 | elif data["server"]=="127.0.0.1" or data["server"]=="localhost": |
| 4542 | vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized, |
| 4543 | "description": "this console is only reachable by local interface", |
| 4544 | "name":vm['name'] |
| 4545 | } |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4546 | vm_error+=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4547 | else: |
| 4548 | #print "console data", data |
| 4549 | try: |
| 4550 | console_thread = create_or_use_console_proxy_thread(data["server"], data["port"]) |
| 4551 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 4552 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 4553 | protocol=data["protocol"], |
| 4554 | ip = global_config["http_console_host"], |
| 4555 | port = console_thread.port, |
| 4556 | suffix = data["suffix"]), |
| 4557 | "name":vm['name'] |
| 4558 | } |
| 4559 | vm_ok +=1 |
| 4560 | except NfvoException as e: |
| 4561 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4562 | vm_error+=1 |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4563 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4564 | else: |
| 4565 | vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']} |
| 4566 | vm_ok +=1 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4567 | except vimconn.vimconnException as e: |
| 4568 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 4569 | vm_error+=1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4570 | |
| 4571 | if vm_ok==0: #all goes wrong |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4572 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4573 | else: |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 4574 | return vm_result |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4575 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4576 | def instance_action_get(mydb, nfvo_tenant, instance_id, action_id): |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4577 | filter = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4578 | if nfvo_tenant and nfvo_tenant != "any": |
| 4579 | filter["tenant_id"] = nfvo_tenant |
| 4580 | if instance_id and instance_id != "any": |
| 4581 | filter["instance_id"] = instance_id |
| 4582 | if action_id: |
| 4583 | filter["uuid"] = action_id |
| 4584 | rows = mydb.get_rows(FROM="instance_actions", WHERE=filter) |
| tierno | 16e3dd4 | 2018-04-24 12:52:40 +0200 | [diff] [blame] | 4585 | if action_id: |
| 4586 | if not rows: |
| 4587 | raise NfvoException("Not found any action with this criteria", HTTP_Not_Found) |
| 4588 | vim_actions = mydb.get_rows(FROM="vim_actions", WHERE={"instance_action_id": action_id}) |
| 4589 | rows[0]["vim_actions"] = vim_actions |
| tierno | fc5f80b | 2018-05-29 16:00:43 +0200 | [diff] [blame] | 4590 | return {"actions": rows} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 4591 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4592 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4593 | def create_or_use_console_proxy_thread(console_server, console_port): |
| 4594 | #look for a non-used port |
| 4595 | console_thread_key = console_server + ":" + str(console_port) |
| 4596 | if console_thread_key in global_config["console_thread"]: |
| 4597 | #global_config["console_thread"][console_thread_key].start_timeout() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4598 | return global_config["console_thread"][console_thread_key] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4599 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4600 | for port in global_config["console_port_iterator"](): |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 4601 | #print "create_or_use_console_proxy_thread() port:", port |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4602 | if port in global_config["console_ports"]: |
| 4603 | continue |
| 4604 | try: |
| 4605 | clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port) |
| 4606 | clithread.start() |
| 4607 | global_config["console_thread"][console_thread_key] = clithread |
| 4608 | global_config["console_ports"][port] = console_thread_key |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4609 | return clithread |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4610 | except cli.ConsoleProxyExceptionPortUsed as e: |
| 4611 | #port used, try with onoher |
| 4612 | continue |
| 4613 | except cli.ConsoleProxyException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4614 | raise NfvoException(str(e), HTTP_Bad_Request) |
| 4615 | raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4616 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4617 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4618 | def check_tenant(mydb, tenant_id): |
| 4619 | '''check that tenant exists at database''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4620 | tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id}) |
| 4621 | if not tenant: |
| 4622 | raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found) |
| 4623 | return |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4624 | |
| 4625 | def new_tenant(mydb, tenant_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4626 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4627 | tenant_uuid = str(uuid4()) |
| 4628 | tenant_dict['uuid'] = tenant_uuid |
| 4629 | try: |
| 4630 | pub_key, priv_key = create_RO_keypair(tenant_uuid) |
| 4631 | tenant_dict['RO_pub_key'] = pub_key |
| 4632 | tenant_dict['encrypted_RO_priv_key'] = priv_key |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4633 | mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4634 | except db_base_Exception as e: |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 4635 | 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] | 4636 | return tenant_uuid |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4637 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4638 | def delete_tenant(mydb, tenant): |
| 4639 | #get nfvo_tenant info |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4640 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4641 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant') |
| 4642 | mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid']) |
| 4643 | return tenant_dict['uuid'] + " " + tenant_dict["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4644 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4645 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4646 | def new_datacenter(mydb, datacenter_descriptor): |
| tierno | 1c848c0 | 2018-05-21 16:40:33 +0200 | [diff] [blame] | 4647 | sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4648 | if "config" in datacenter_descriptor: |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4649 | sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None) |
| 4650 | datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True, |
| 4651 | width=256) |
| 4652 | # Check that datacenter-type is correct |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4653 | datacenter_type = datacenter_descriptor.get("type", "openvim"); |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4654 | # module_info = None |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4655 | try: |
| 4656 | module = "vimconn_" + datacenter_type |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4657 | pkg = __import__("osm_ro." + module) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4658 | # vim_conn = getattr(pkg, module) |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4659 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 4660 | except (IOError, ImportError): |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 4661 | # if module_info and module_info[0]: |
| 4662 | # file.close(module_info[0]) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4663 | raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type, |
| 4664 | module), |
| 4665 | HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4666 | |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 4667 | 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] | 4668 | if sdn_port_mapping: |
| 4669 | try: |
| 4670 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping) |
| 4671 | except Exception as e: |
| 4672 | mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback |
| 4673 | raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4674 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4675 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4676 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4677 | def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4678 | # obtain data, check that only one exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4679 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4680 | |
| 4681 | # edit data |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4682 | datacenter_id = datacenter['uuid'] |
| tierno | e4e8035 | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4683 | where = {'uuid': datacenter['uuid']} |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4684 | remove_port_mapping = False |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4685 | new_sdn_port_mapping = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4686 | if "config" in datacenter_descriptor: |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4687 | if datacenter_descriptor['config'] != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4688 | try: |
| 4689 | new_config_dict = datacenter_descriptor["config"] |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4690 | if "sdn-port-mapping" in new_config_dict: |
| 4691 | remove_port_mapping = True |
| 4692 | new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping") |
| tierno | e4e8035 | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4693 | # delete null fields |
| 4694 | to_delete = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4695 | for k in new_config_dict: |
| tierno | e4e8035 | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4696 | if new_config_dict[k] is None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4697 | to_delete.append(k) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4698 | if k == 'sdn-controller': |
| 4699 | remove_port_mapping = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4700 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4701 | config_text = datacenter.get("config") |
| 4702 | if not config_text: |
| 4703 | config_text = '{}' |
| 4704 | config_dict = yaml.load(config_text) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4705 | config_dict.update(new_config_dict) |
| tierno | e4e8035 | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4706 | # delete null fields |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4707 | for k in to_delete: |
| 4708 | del config_dict[k] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4709 | except Exception as e: |
| 4710 | raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4711 | if config_dict: |
| 4712 | datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256) |
| 4713 | else: |
| 4714 | datacenter_descriptor["config"] = None |
| 4715 | if remove_port_mapping: |
| 4716 | try: |
| 4717 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id) |
| 4718 | except ovimException as e: |
| tierno | e4e8035 | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4719 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e), HTTP_Conflict) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4720 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4721 | mydb.update_rows('datacenters', datacenter_descriptor, where) |
| tierno | edf3f4f | 2018-05-17 23:02:47 +0200 | [diff] [blame] | 4722 | if new_sdn_port_mapping: |
| 4723 | try: |
| 4724 | datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping) |
| 4725 | except ovimException as e: |
| tierno | e4e8035 | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4726 | # Rollback |
| 4727 | mydb.update_rows('datacenters', datacenter, where) |
| 4728 | raise NfvoException("Error adding datacenter-port-mapping " + str(e), HTTP_Conflict) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4729 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4730 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4731 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4732 | def delete_datacenter(mydb, datacenter): |
| 4733 | #get nfvo_tenant info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4734 | datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter') |
| 4735 | mydb.delete_row_by_id("datacenters", datacenter_dict['uuid']) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 4736 | try: |
| 4737 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid']) |
| 4738 | except ovimException as e: |
| tierno | e4e8035 | 2018-08-29 10:56:13 +0200 | [diff] [blame] | 4739 | raise NfvoException("Error deleting datacenter-port-mapping " + str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4740 | return datacenter_dict['uuid'] + " " + datacenter_dict['name'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4741 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4742 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4743 | def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None, |
| 4744 | vim_username=None, vim_password=None, config=None): |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 4745 | # get datacenter info |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4746 | try: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4747 | if not datacenter_id: |
| 4748 | if not vim_id: |
| 4749 | raise NfvoException("You must provide 'vim_id", http_code=HTTP_Bad_Request) |
| 4750 | datacenter_id = vim_id |
| 4751 | datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4752 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4753 | 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] | 4754 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4755 | # get nfvo_tenant info |
| 4756 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant) |
| 4757 | if vim_tenant_name==None: |
| 4758 | vim_tenant_name=tenant_dict['name'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4759 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4760 | tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id } |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4761 | # #check that this association does not exist before |
| 4762 | # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 4763 | # if len(tenants_datacenters)>0: |
| 4764 | # 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] | 4765 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4766 | vim_tenant_id_exist_atdb=False |
| 4767 | if not create_vim_tenant: |
| 4768 | where_={"datacenter_id": datacenter_id} |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4769 | if vim_tenant!=None: |
| 4770 | where_["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4771 | if vim_tenant_name!=None: |
| 4772 | where_["vim_tenant_name"] = vim_tenant_name |
| 4773 | #check if vim_tenant_id is already at database |
| 4774 | datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_) |
| 4775 | if len(datacenter_tenants_dict)>=1: |
| 4776 | datacenter_tenants_dict = datacenter_tenants_dict[0] |
| 4777 | vim_tenant_id_exist_atdb=True |
| 4778 | #TODO check if a field has changed and edit entry at datacenter_tenants at DB |
| 4779 | else: #result=0 |
| 4780 | datacenter_tenants_dict = {} |
| 4781 | #insert at table datacenter_tenants |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4782 | else: #if vim_tenant==None: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4783 | #create tenant at VIM if not provided |
| 4784 | try: |
| 4785 | _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, |
| 4786 | vim_passwd=vim_password) |
| 4787 | datacenter_name = myvim["name"] |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4788 | 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] | 4789 | except vimconn.vimconnException as e: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4790 | 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] | 4791 | datacenter_tenants_dict = {} |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4792 | datacenter_tenants_dict["created"]="true" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4793 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4794 | #fill datacenter_tenants table |
| 4795 | if not vim_tenant_id_exist_atdb: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4796 | datacenter_tenants_dict["vim_tenant_id"] = vim_tenant |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4797 | datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name |
| 4798 | datacenter_tenants_dict["user"] = vim_username |
| 4799 | datacenter_tenants_dict["passwd"] = vim_password |
| 4800 | datacenter_tenants_dict["datacenter_id"] = datacenter_id |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4801 | if name: |
| 4802 | datacenter_tenants_dict["name"] = name |
| 4803 | else: |
| 4804 | datacenter_tenants_dict["name"] = datacenter_name |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4805 | if config: |
| 4806 | datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256) |
| 4807 | id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True) |
| 4808 | datacenter_tenants_dict["uuid"] = id_ |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4809 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4810 | #fill tenants_datacenters table |
| 4811 | datacenter_tenant_id = datacenter_tenants_dict["uuid"] |
| 4812 | tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id |
| 4813 | mydb.new_row('tenants_datacenters', tenants_datacenter_dict) |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4814 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4815 | # create thread |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4816 | 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] | 4817 | 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] | 4818 | db=db, db_lock=db_lock, ovim=ovim) |
| 4819 | new_thread.start() |
| 4820 | thread_id = datacenter_tenants_dict["uuid"] |
| 4821 | vim_threads["running"][thread_id] = new_thread |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4822 | return thread_id |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4823 | except vimconn.vimconnException as e: |
| 4824 | raise NfvoException(str(e), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4825 | |
| tierno | 9931490 | 2017-04-26 13:23:09 +0200 | [diff] [blame] | 4826 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4827 | def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None, |
| 4828 | 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] | 4829 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4830 | # get vim_account; check is valid for this tenant |
| 4831 | from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id" |
| 4832 | where_ = {"td.nfvo_tenant_id": nfvo_tenant} |
| 4833 | if datacenter_tenant_id: |
| 4834 | where_["dt.uuid"] = datacenter_tenant_id |
| 4835 | if datacenter_id: |
| 4836 | where_["dt.datacenter_id"] = datacenter_id |
| 4837 | vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_) |
| 4838 | if not vim_accounts: |
| 4839 | raise NfvoException("vim_account not found for this tenant", http_code=HTTP_Not_Found) |
| 4840 | elif len(vim_accounts) > 1: |
| 4841 | raise NfvoException("found more than one vim_account for this tenant", http_code=HTTP_Conflict) |
| 4842 | datacenter_tenant_id = vim_accounts[0]["uuid"] |
| 4843 | original_config = vim_accounts[0]["config"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4844 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4845 | update_ = {} |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4846 | if config: |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4847 | original_config_dict = yaml.load(original_config) |
| 4848 | original_config_dict.update(config) |
| 4849 | update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256) |
| 4850 | if name: |
| 4851 | update_['name'] = name |
| 4852 | if vim_tenant: |
| 4853 | update_['vim_tenant_id'] = vim_tenant |
| 4854 | if vim_tenant_name: |
| 4855 | update_['vim_tenant_name'] = vim_tenant_name |
| 4856 | if vim_username: |
| 4857 | update_['user'] = vim_username |
| 4858 | if vim_password: |
| 4859 | update_['passwd'] = vim_password |
| 4860 | if update_: |
| 4861 | 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] | 4862 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4863 | vim_threads["running"][datacenter_tenant_id].insert_task("reload") |
| 4864 | return datacenter_tenant_id |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4865 | |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4866 | def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4867 | #get nfvo_tenant info |
| 4868 | if not tenant_id or tenant_id=="any": |
| 4869 | tenant_uuid = None |
| 4870 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4871 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4872 | tenant_uuid = tenant_dict['uuid'] |
| 4873 | |
| 4874 | #check that this association exist before |
| tierno | d3750b3 | 2018-07-20 15:33:08 +0200 | [diff] [blame] | 4875 | tenants_datacenter_dict = {} |
| 4876 | if datacenter: |
| 4877 | datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter) |
| 4878 | tenants_datacenter_dict["datacenter_id"] = datacenter_id |
| 4879 | elif vim_account_id: |
| 4880 | tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4881 | if tenant_uuid: |
| 4882 | tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4883 | tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 4884 | if len(tenant_datacenter_list)==0 and tenant_uuid: |
| 4885 | 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] | 4886 | |
| 4887 | #delete this association |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4888 | mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4889 | |
| 4890 | #get vim_tenant info and deletes |
| 4891 | warning='' |
| 4892 | for tenant_datacenter_item in tenant_datacenter_list: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4893 | vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 4894 | #try to delete vim:tenant |
| 4895 | try: |
| 4896 | mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 4897 | if vim_tenant_dict['created']=='true': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4898 | #delete tenant at VIM if created by NFVO |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4899 | try: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 4900 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4901 | myvim.delete_tenant(vim_tenant_dict['vim_tenant_id']) |
| 4902 | except vimconn.vimconnException as e: |
| 4903 | warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e)) |
| 4904 | logger.warn(warning) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4905 | except db_base_Exception as e: |
| 4906 | logger.error("Cannot delete datacenter_tenants " + str(e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4907 | 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] | 4908 | thread_id = tenant_datacenter_item["datacenter_tenant_id"] |
| tierno | a357269 | 2018-05-14 13:09:33 +0200 | [diff] [blame] | 4909 | thread = vim_threads["running"].get(thread_id) |
| 4910 | if thread: |
| 4911 | thread.insert_task("exit") |
| 4912 | vim_threads["deleting"][thread_id] = thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4913 | return "datacenter {} detached. {}".format(datacenter_id, warning) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4914 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4915 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4916 | def datacenter_action(mydb, tenant_id, datacenter, action_dict): |
| 4917 | #DEPRECATED |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4918 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4919 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4920 | |
| 4921 | if 'net-update' in action_dict: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4922 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4923 | 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] | 4924 | #print content |
| 4925 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4926 | #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e)) |
| 4927 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4928 | #update nets Change from VIM format to NFVO format |
| 4929 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4930 | for net in nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4931 | net_nfvo={'datacenter_id': datacenter_id} |
| 4932 | net_nfvo['name'] = net['name'] |
| 4933 | #net_nfvo['description']= net['name'] |
| 4934 | net_nfvo['vim_net_id'] = net['id'] |
| 4935 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 4936 | net_nfvo['shared'] = net['shared'] |
| 4937 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| 4938 | net_list.append(net_nfvo) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4939 | inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list) |
| 4940 | logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted) |
| 4941 | return inserted |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4942 | elif 'net-edit' in action_dict: |
| 4943 | net = action_dict['net-edit'].pop('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4944 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4945 | result = mydb.update_rows('datacenter_nets', action_dict['net-edit'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4946 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4947 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4948 | elif 'net-delete' in action_dict: |
| 4949 | net = action_dict['net-deelte'].get('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4950 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4951 | result = mydb.delete_row(FROM='datacenter_nets', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4952 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4953 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4954 | |
| 4955 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4956 | raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4957 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4958 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4959 | def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): |
| 4960 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4961 | datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4962 | |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4963 | what = 'uuid' if utils.check_valid_uuid(netmap) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4964 | result = mydb.update_rows('datacenter_nets', action_dict['netmap'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4965 | WHERE={'datacenter_id':datacenter_id, what: netmap}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4966 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4967 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4968 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4969 | def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): |
| 4970 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4971 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4972 | filter_dict={} |
| 4973 | if action_dict: |
| 4974 | action_dict = action_dict["netmap"] |
| 4975 | if 'vim_id' in action_dict: |
| 4976 | filter_dict["id"] = action_dict['vim_id'] |
| 4977 | if 'vim_name' in action_dict: |
| 4978 | filter_dict["name"] = action_dict['vim_name'] |
| 4979 | else: |
| 4980 | filter_dict["shared"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4981 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4982 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4983 | vim_nets = myvim.get_network_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4984 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4985 | #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e)) |
| 4986 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| 4987 | if len(vim_nets)>1 and action_dict: |
| 4988 | raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict) |
| 4989 | elif len(vim_nets)==0: # and action_dict: |
| 4990 | 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] | 4991 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4992 | for net in vim_nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4993 | net_nfvo={'datacenter_id': datacenter_id} |
| 4994 | if action_dict and "name" in action_dict: |
| 4995 | net_nfvo['name'] = action_dict['name'] |
| 4996 | else: |
| 4997 | net_nfvo['name'] = net['name'] |
| 4998 | #net_nfvo['description']= net['name'] |
| 4999 | net_nfvo['vim_net_id'] = net['id'] |
| 5000 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 5001 | net_nfvo['shared'] = net['shared'] |
| 5002 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5003 | try: |
| 5004 | net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5005 | net_nfvo["status"] = "OK" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5006 | net_nfvo["uuid"] = net_id |
| 5007 | except db_base_Exception as e: |
| 5008 | if action_dict: |
| 5009 | raise |
| 5010 | else: |
| 5011 | net_nfvo["status"] = "FAIL: " + str(e) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5012 | net_list.append(net_nfvo) |
| 5013 | return net_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5014 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5015 | def get_sdn_net_id(mydb, tenant_id, datacenter, network_id): |
| 5016 | # obtain all network data |
| 5017 | try: |
| 5018 | if utils.check_valid_uuid(network_id): |
| 5019 | filter_dict = {"id": network_id} |
| 5020 | else: |
| 5021 | filter_dict = {"name": network_id} |
| 5022 | |
| 5023 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 5024 | network = myvim.get_network_list(filter_dict=filter_dict) |
| 5025 | except vimconn.vimconnException as e: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 5026 | 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] | 5027 | |
| 5028 | # ensure the network is defined |
| 5029 | if len(network) == 0: |
| 5030 | raise NfvoException("Network {} is not present in the system".format(network_id), |
| 5031 | HTTP_Bad_Request) |
| 5032 | |
| 5033 | # ensure there is only one network with the provided name |
| 5034 | if len(network) > 1: |
| 5035 | raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request) |
| 5036 | |
| 5037 | # ensure it is a dataplane network |
| 5038 | if network[0]['type'] != 'data': |
| 5039 | return None |
| 5040 | |
| 5041 | # ensure we use the id |
| 5042 | network_id = network[0]['id'] |
| 5043 | |
| 5044 | # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id |
| 5045 | # and with instance_scenario_id==NULL |
| 5046 | #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None} |
| 5047 | search_dict = {'vim_net_id': network_id} |
| 5048 | |
| 5049 | try: |
| 5050 | #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id'] |
| 5051 | result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict) |
| 5052 | except db_base_Exception as e: |
| 5053 | 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] | 5054 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5055 | |
| 5056 | sdn_net_counter = 0 |
| 5057 | for net in result: |
| 5058 | if net['sdn_net_id'] != None: |
| 5059 | sdn_net_counter+=1 |
| 5060 | sdn_net_id = net['sdn_net_id'] |
| 5061 | |
| 5062 | if sdn_net_counter == 0: |
| 5063 | return None |
| 5064 | elif sdn_net_counter == 1: |
| 5065 | return sdn_net_id |
| 5066 | else: |
| 5067 | raise NfvoException("More than one SDN network is associated to vim network {}".format( |
| 5068 | network_id), HTTP_Internal_Server_Error) |
| 5069 | |
| 5070 | def get_sdn_controller_id(mydb, datacenter): |
| 5071 | # Obtain sdn controller id |
| 5072 | config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}') |
| 5073 | if not config: |
| 5074 | return None |
| 5075 | |
| 5076 | return yaml.load(config).get('sdn-controller') |
| 5077 | |
| 5078 | def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor): |
| 5079 | try: |
| 5080 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5081 | if not sdn_network_id: |
| 5082 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error) |
| 5083 | |
| 5084 | #Obtain sdn controller id |
| 5085 | controller_id = get_sdn_controller_id(mydb, datacenter) |
| 5086 | if not controller_id: |
| 5087 | raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error) |
| 5088 | |
| 5089 | #Obtain sdn controller info |
| 5090 | sdn_controller = ovim.show_of_controller(controller_id) |
| 5091 | |
| 5092 | port_data = { |
| 5093 | 'name': 'external_port', |
| 5094 | 'net_id': sdn_network_id, |
| 5095 | 'ofc_id': controller_id, |
| 5096 | 'switch_dpid': sdn_controller['dpid'], |
| 5097 | 'switch_port': descriptor['port'] |
| 5098 | } |
| 5099 | |
| 5100 | if 'vlan' in descriptor: |
| 5101 | port_data['vlan'] = descriptor['vlan'] |
| 5102 | if 'mac' in descriptor: |
| 5103 | port_data['mac'] = descriptor['mac'] |
| 5104 | |
| 5105 | result = ovim.new_port(port_data) |
| 5106 | except ovimException as e: |
| 5107 | raise NfvoException("ovimException attaching SDN network {} to vim network {}".format( |
| 5108 | sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error) |
| 5109 | except db_base_Exception as e: |
| 5110 | raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5111 | network_id) + str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5112 | |
| 5113 | return 'Port uuid: '+ result |
| 5114 | |
| 5115 | def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None): |
| 5116 | if port_id: |
| 5117 | filter = {'uuid': port_id} |
| 5118 | else: |
| 5119 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 5120 | if not sdn_network_id: |
| 5121 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), |
| 5122 | HTTP_Internal_Server_Error) |
| 5123 | #in case no port_id is specified only ports marked as 'external_port' will be detached |
| 5124 | filter = {'name': 'external_port', 'net_id': sdn_network_id} |
| 5125 | |
| 5126 | try: |
| 5127 | port_list = ovim.get_ports(columns={'uuid'}, filter=filter) |
| 5128 | except ovimException as e: |
| 5129 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| 5130 | HTTP_Internal_Server_Error) |
| 5131 | |
| 5132 | if len(port_list) == 0: |
| 5133 | raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id), |
| 5134 | HTTP_Bad_Request) |
| 5135 | |
| 5136 | port_uuid_list = [] |
| 5137 | for port in port_list: |
| 5138 | try: |
| 5139 | port_uuid_list.append(port['uuid']) |
| 5140 | ovim.delete_port(port['uuid']) |
| 5141 | except ovimException as e: |
| 5142 | raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error) |
| 5143 | |
| 5144 | return 'Detached ports uuid: {}'.format(','.join(port_uuid_list)) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5145 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5146 | def vim_action_get(mydb, tenant_id, datacenter, item, name): |
| 5147 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5148 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5149 | filter_dict={} |
| 5150 | if name: |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 5151 | if utils.check_valid_uuid(name): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5152 | filter_dict["id"] = name |
| 5153 | else: |
| 5154 | filter_dict["name"] = name |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5155 | try: |
| 5156 | if item=="networks": |
| 5157 | #filter_dict['tenant_id'] = myvim['tenant_id'] |
| 5158 | content = myvim.get_network_list(filter_dict=filter_dict) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5159 | |
| 5160 | if len(content) == 0: |
| 5161 | raise NfvoException("Network {} is not present in the system. ".format(name), |
| 5162 | HTTP_Bad_Request) |
| 5163 | |
| 5164 | #Update the networks with the attached ports |
| 5165 | for net in content: |
| 5166 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id']) |
| 5167 | if sdn_network_id != None: |
| 5168 | try: |
| 5169 | #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id}) |
| 5170 | port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id}) |
| 5171 | except ovimException as e: |
| 5172 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error) |
| 5173 | #Remove field name and if port name is external_port save it as 'type' |
| 5174 | for port in port_list: |
| 5175 | if port['name'] == 'external_port': |
| 5176 | port['type'] = "External" |
| 5177 | del port['name'] |
| 5178 | net['sdn_network_id'] = sdn_network_id |
| 5179 | net['sdn_attached_ports'] = port_list |
| 5180 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5181 | elif item=="tenants": |
| 5182 | content = myvim.get_tenant_list(filter_dict=filter_dict) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5183 | elif item == "images": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5184 | |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5185 | content = myvim.get_image_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5186 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5187 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 5188 | 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] | 5189 | if name and len(content)==1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5190 | return {item[:-1]: content[0]} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5191 | elif name and len(content)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5192 | 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] | 5193 | datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5194 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5195 | return {item: content} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5196 | except vimconn.vimconnException as e: |
| 5197 | 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] | 5198 | 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] | 5199 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5200 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5201 | def vim_action_delete(mydb, tenant_id, datacenter, item, name): |
| 5202 | #get datacenter info |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5203 | if tenant_id == "any": |
| 5204 | tenant_id=None |
| 5205 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5206 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5207 | #get uuid name |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5208 | content = vim_action_get(mydb, tenant_id, datacenter, item, name) |
| 5209 | logger.debug("vim_action_delete vim response: " + str(content)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5210 | items = content.values()[0] |
| 5211 | if type(items)==list and len(items)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5212 | raise NfvoException("Not found " + item, HTTP_Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5213 | elif type(items)==list and len(items)>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5214 | 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] | 5215 | else: # it is a dict |
| 5216 | item_id = items["id"] |
| 5217 | item_name = str(items.get("name")) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5218 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5219 | try: |
| 5220 | if item=="networks": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5221 | # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it |
| 5222 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id) |
| 5223 | if sdn_network_id != None: |
| 5224 | #Delete any port attachment to this network |
| 5225 | try: |
| 5226 | port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id}) |
| 5227 | except ovimException as e: |
| 5228 | raise NfvoException( |
| 5229 | "ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| 5230 | HTTP_Internal_Server_Error) |
| 5231 | |
| 5232 | # By calling one by one all ports to be detached we ensure that not only the external_ports get detached |
| 5233 | for port in port_list: |
| 5234 | vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid']) |
| 5235 | |
| 5236 | #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id |
| 5237 | try: |
| 5238 | mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id}) |
| 5239 | except db_base_Exception as e: |
| 5240 | raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) + |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5241 | str(e), e.http_code) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5242 | |
| 5243 | #Delete the SDN network |
| 5244 | try: |
| 5245 | ovim.delete_network(sdn_network_id) |
| 5246 | except ovimException as e: |
| 5247 | logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True) |
| 5248 | raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), |
| 5249 | HTTP_Internal_Server_Error) |
| 5250 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5251 | content = myvim.delete_network(item_id) |
| 5252 | elif item=="tenants": |
| 5253 | content = myvim.delete_tenant(item_id) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 5254 | elif item == "images": |
| 5255 | content = myvim.delete_image(item_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5256 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5257 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5258 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5259 | #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e))) |
| 5260 | 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] | 5261 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5262 | return "{} {} {} deleted".format(item[:-1], item_id,item_name) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5263 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 5264 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5265 | def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): |
| 5266 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5267 | logger.debug("vim_action_create descriptor %s", str(descriptor)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 5268 | if tenant_id == "any": |
| 5269 | tenant_id=None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 5270 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5271 | try: |
| 5272 | if item=="networks": |
| 5273 | net = descriptor["network"] |
| 5274 | net_name = net.pop("name") |
| 5275 | net_type = net.pop("type", "bridge") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 5276 | net_public = net.pop("shared", False) |
| 5277 | net_ipprofile = net.pop("ip_profile", None) |
| tierno | a7d34d0 | 2017-02-23 14:42:07 +0100 | [diff] [blame] | 5278 | net_vlan = net.pop("vlan", None) |
| 5279 | 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] | 5280 | |
| 5281 | #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network |
| 5282 | 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] | 5283 | #obtain datacenter_tenant_id |
| 5284 | datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',), |
| 5285 | FROM='datacenter_tenants', |
| 5286 | WHERE={'datacenter_id': datacenter})[0]['uuid'] |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5287 | try: |
| 5288 | sdn_network = {} |
| 5289 | sdn_network['vlan'] = net_vlan |
| 5290 | sdn_network['type'] = net_type |
| 5291 | sdn_network['name'] = net_name |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5292 | sdn_network['region'] = datacenter_tenant_id |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5293 | ovim_content = ovim.new_network(sdn_network) |
| 5294 | except ovimException as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5295 | logger.error("ovimException creating SDN network={} ".format( |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5296 | sdn_network) + str(e), exc_info=True) |
| 5297 | raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e), |
| 5298 | HTTP_Internal_Server_Error) |
| 5299 | |
| 5300 | # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id |
| 5301 | # use instance_scenario_id=None to distinguish from real instaces of nets |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5302 | correspondence = {'instance_scenario_id': None, |
| 5303 | 'sdn_net_id': ovim_content, |
| 5304 | 'vim_net_id': content, |
| 5305 | 'datacenter_tenant_id': datacenter_tenant_id |
| 5306 | } |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5307 | try: |
| 5308 | mydb.new_row('instance_nets', correspondence, add_uuid=True) |
| 5309 | except db_base_Exception as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 5310 | raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format( |
| tierno | 9c5c832 | 2018-03-23 15:44:03 +0100 | [diff] [blame] | 5311 | correspondence, e), e.http_code) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5312 | elif item=="tenants": |
| 5313 | tenant = descriptor["tenant"] |
| 5314 | content = myvim.new_tenant(tenant["name"], tenant.get("description")) |
| 5315 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 5316 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 5317 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 5318 | 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] | 5319 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5320 | return vim_action_get(mydb, tenant_id, datacenter, item, content) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5321 | |
| 5322 | def sdn_controller_create(mydb, tenant_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5323 | data = ovim.new_of_controller(sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5324 | logger.debug('New SDN controller created with uuid {}'.format(data)) |
| 5325 | return data |
| 5326 | |
| 5327 | def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5328 | data = ovim.edit_of_controller(controller_id, sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5329 | msg = 'SDN controller {} updated'.format(data) |
| 5330 | logger.debug(msg) |
| 5331 | return msg |
| 5332 | |
| 5333 | def sdn_controller_list(mydb, tenant_id, controller_id=None): |
| 5334 | if controller_id == None: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5335 | data = ovim.get_of_controllers() |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5336 | else: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5337 | data = ovim.show_of_controller(controller_id) |
| 5338 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5339 | msg = 'SDN controller list:\n {}'.format(data) |
| 5340 | logger.debug(msg) |
| 5341 | return data |
| 5342 | |
| 5343 | def sdn_controller_delete(mydb, tenant_id, controller_id): |
| 5344 | select_ = ('uuid', 'config') |
| 5345 | datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_) |
| 5346 | for datacenter in datacenters: |
| 5347 | if datacenter['config']: |
| 5348 | config = yaml.load(datacenter['config']) |
| 5349 | if 'sdn-controller' in config and config['sdn-controller'] == controller_id: |
| 5350 | raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict) |
| 5351 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5352 | data = ovim.delete_of_controller(controller_id) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5353 | msg = 'SDN controller {} deleted'.format(data) |
| 5354 | logger.debug(msg) |
| 5355 | return msg |
| 5356 | |
| 5357 | def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping): |
| 5358 | controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id}) |
| 5359 | if len(controller) < 1: |
| 5360 | raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found) |
| 5361 | |
| 5362 | try: |
| 5363 | sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"] |
| 5364 | except: |
| 5365 | raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request) |
| 5366 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5367 | sdn_controller = ovim.show_of_controller(sdn_controller_id) |
| 5368 | switch_dpid = sdn_controller["dpid"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5369 | |
| 5370 | maps = list() |
| 5371 | for compute_node in sdn_port_mapping: |
| 5372 | #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid} |
| 5373 | element = dict() |
| 5374 | element["compute_node"] = compute_node["compute_node"] |
| 5375 | for port in compute_node["ports"]: |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5376 | pci = port.get("pci") |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5377 | element["switch_port"] = port.get("switch_port") |
| 5378 | element["switch_mac"] = port.get("switch_mac") |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5379 | 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] | 5380 | raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'" |
| 5381 | " or 'switch_mac'", HTTP_Bad_Request) |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 5382 | for pci_expanded in utils.expand_brackets(pci): |
| 5383 | element["pci"] = pci_expanded |
| 5384 | maps.append(dict(element)) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5385 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5386 | 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] | 5387 | |
| 5388 | def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 5389 | maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id}) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5390 | |
| 5391 | result = { |
| 5392 | "sdn-controller": None, |
| 5393 | "datacenter-id": datacenter_id, |
| 5394 | "dpid": None, |
| 5395 | "ports_mapping": list() |
| 5396 | } |
| 5397 | |
| 5398 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id) |
| 5399 | if datacenter['config']: |
| 5400 | config = yaml.load(datacenter['config']) |
| 5401 | if 'sdn-controller' in config: |
| 5402 | controller_id = config['sdn-controller'] |
| 5403 | sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id) |
| 5404 | result["sdn-controller"] = controller_id |
| 5405 | result["dpid"] = sdn_controller["dpid"] |
| 5406 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 5407 | if result["sdn-controller"] == None: |
| 5408 | raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request) |
| 5409 | if result["dpid"] == None: |
| 5410 | raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]), |
| 5411 | HTTP_Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 5412 | |
| 5413 | if len(maps) == 0: |
| 5414 | return result |
| 5415 | |
| 5416 | ports_correspondence_dict = dict() |
| 5417 | for link in maps: |
| 5418 | if result["sdn-controller"] != link["ofc_id"]: |
| 5419 | raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 5420 | if result["dpid"] != link["switch_dpid"]: |
| 5421 | raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 5422 | element = dict() |
| 5423 | element["pci"] = link["pci"] |
| 5424 | if link["switch_port"]: |
| 5425 | element["switch_port"] = link["switch_port"] |
| 5426 | if link["switch_mac"]: |
| 5427 | element["switch_mac"] = link["switch_mac"] |
| 5428 | |
| 5429 | if not link["compute_node"] in ports_correspondence_dict: |
| 5430 | content = dict() |
| 5431 | content["compute_node"] = link["compute_node"] |
| 5432 | content["ports"] = list() |
| 5433 | ports_correspondence_dict[link["compute_node"]] = content |
| 5434 | |
| 5435 | ports_correspondence_dict[link["compute_node"]]["ports"].append(element) |
| 5436 | |
| 5437 | for key in sorted(ports_correspondence_dict): |
| 5438 | result["ports_mapping"].append(ports_correspondence_dict[key]) |
| 5439 | |
| 5440 | return result |
| 5441 | |
| 5442 | def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id): |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 5443 | return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id}) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 5444 | |
| 5445 | def create_RO_keypair(tenant_id): |
| 5446 | """ |
| 5447 | Creates a public / private keys for a RO tenant and returns their values |
| 5448 | Params: |
| 5449 | tenant_id: ID of the tenant |
| 5450 | Return: |
| 5451 | public_key: Public key for the RO tenant |
| 5452 | private_key: Encrypted private key for RO tenant |
| 5453 | """ |
| 5454 | |
| 5455 | bits = 2048 |
| 5456 | key = RSA.generate(bits) |
| 5457 | try: |
| 5458 | public_key = key.publickey().exportKey('OpenSSH') |
| 5459 | if isinstance(public_key, ValueError): |
| 5460 | raise NfvoException("Unable to create public key: {}".format(public_key), HTTP_Internal_Server_Error) |
| 5461 | private_key = key.exportKey(passphrase=tenant_id, pkcs=8) |
| 5462 | except (ValueError, NameError) as e: |
| 5463 | raise NfvoException("Unable to create private key: {}".format(e), HTTP_Internal_Server_Error) |
| 5464 | return public_key, private_key |
| 5465 | |
| 5466 | def decrypt_key (key, tenant_id): |
| 5467 | """ |
| 5468 | Decrypts an encrypted RSA key |
| 5469 | Params: |
| 5470 | key: Private key to be decrypted |
| 5471 | tenant_id: ID of the tenant |
| 5472 | Return: |
| 5473 | unencrypted_key: Unencrypted private key for RO tenant |
| 5474 | """ |
| 5475 | try: |
| 5476 | key = RSA.importKey(key,tenant_id) |
| 5477 | unencrypted_key = key.exportKey('PEM') |
| 5478 | if isinstance(unencrypted_key, ValueError): |
| 5479 | raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), HTTP_Internal_Server_Error) |
| 5480 | except ValueError as e: |
| 5481 | raise NfvoException("Unable to decrypt the private key: {}".format(e), HTTP_Internal_Server_Error) |
| 5482 | return unencrypted_key |