| 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 |
| 55 | from itertools import chain |
| 56 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 57 | global global_config |
| 58 | global vimconn_imported |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 59 | global logger |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 60 | global default_volume_size |
| 61 | default_volume_size = '5' #size in GB |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 62 | global ovim |
| 63 | ovim = None |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 64 | global_config = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 65 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 66 | vimconn_imported = {} # dictionary with VIM type as key, loaded module as value |
| 67 | vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 68 | vim_persistent_info = {} |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 69 | logger = logging.getLogger('openmano.nfvo') |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 70 | task_lock = Lock() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 71 | last_task_id = 0.0 |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 72 | db = None |
| 73 | db_lock = Lock() |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 74 | |
| 75 | class NfvoException(Exception): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 76 | def __init__(self, message, http_code): |
| 77 | self.http_code = http_code |
| 78 | Exception.__init__(self, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 79 | |
| 80 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 81 | def get_task_id(): |
| 82 | global last_task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 83 | task_id = t.time() |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 84 | if task_id <= last_task_id: |
| 85 | task_id = last_task_id + 0.000001 |
| 86 | last_task_id = task_id |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 87 | return "ACTION-{:.6f}".format(task_id) |
| 88 | # 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] | 89 | |
| 90 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 91 | def new_task(name, params, depends=None): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 92 | """Deprected!!!""" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 93 | task_id = get_task_id() |
| 94 | task = {"status": "enqueued", "id": task_id, "name": name, "params": params} |
| 95 | if depends: |
| 96 | task["depends"] = depends |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 97 | return task |
| 98 | |
| 99 | |
| 100 | def is_task_id(id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 101 | return True if id[:5] == "TASK-" else False |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 102 | |
| 103 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 104 | def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id): |
| 105 | name = datacenter_name[:16] |
| 106 | if name not in vim_threads["names"]: |
| 107 | vim_threads["names"].append(name) |
| 108 | return name |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 109 | name = datacenter_name[:16] + "." + tenant_name[:16] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 110 | if name not in vim_threads["names"]: |
| 111 | vim_threads["names"].append(name) |
| 112 | return name |
| 113 | name = datacenter_id + "-" + tenant_id |
| 114 | vim_threads["names"].append(name) |
| 115 | return name |
| 116 | |
| 117 | |
| 118 | def start_service(mydb): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 119 | global db, global_config |
| 120 | db = nfvo_db.nfvo_db() |
| 121 | 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] | 122 | global ovim |
| 123 | |
| 124 | # Initialize openvim for SDN control |
| 125 | # TODO: Avoid static configuration by adding new parameters to openmanod.cfg |
| 126 | # TODO: review ovim.py to delete not needed configuration |
| 127 | ovim_configuration = { |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 128 | 'logger_name': 'openmano.ovim', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 129 | 'network_vlan_range_start': 1000, |
| 130 | 'network_vlan_range_end': 4096, |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 131 | 'db_name': global_config["db_ovim_name"], |
| 132 | 'db_host': global_config["db_ovim_host"], |
| 133 | 'db_user': global_config["db_ovim_user"], |
| 134 | 'db_passwd': global_config["db_ovim_passwd"], |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 135 | 'bridge_ifaces': {}, |
| 136 | 'mode': 'normal', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 137 | 'network_type': 'bridge', |
| 138 | #TODO: log_level_of should not be needed. To be modified in ovim |
| 139 | 'log_level_of': 'DEBUG' |
| 140 | } |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 141 | try: |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 142 | # starts ovim library |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 143 | ovim = ovim_module.ovim(ovim_configuration) |
| 144 | ovim.start_service() |
| 145 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 146 | #delete old unneeded vim_actions |
| 147 | clean_db(mydb) |
| 148 | |
| 149 | # starts vim_threads |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 150 | from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\ |
| 151 | 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid' |
| 152 | select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin', |
| 153 | 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id', |
| 154 | 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id', |
| 155 | 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id') |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 156 | vims = mydb.get_rows(FROM=from_, SELECT=select_) |
| 157 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 158 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 159 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 160 | if vim["config"]: |
| 161 | extra.update(yaml.load(vim["config"])) |
| 162 | if vim.get('dt_config'): |
| 163 | extra.update(yaml.load(vim["dt_config"])) |
| 164 | if vim["type"] not in vimconn_imported: |
| 165 | module_info=None |
| 166 | try: |
| 167 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 168 | pkg = __import__("osm_ro." + module) |
| 169 | vim_conn = getattr(pkg, module) |
| 170 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 171 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 172 | vimconn_imported[vim["type"]] = vim_conn |
| 173 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 174 | # if module_info and module_info[0]: |
| 175 | # file.close(module_info[0]) |
| tierno | cdee8cc | 2017-04-25 13:42:06 +0200 | [diff] [blame] | 176 | raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 177 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 178 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 179 | thread_id = vim['datacenter_tenant_id'] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 180 | vim_persistent_info[thread_id] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 181 | try: |
| 182 | #if not tenant: |
| 183 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 184 | myvim = vimconn_imported[ vim["type"] ].vimconnector( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 185 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| 186 | tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'], |
| 187 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| 188 | user=vim['user'], passwd=vim['passwd'], |
| 189 | config=extra, persistent_info=vim_persistent_info[thread_id] |
| 190 | ) |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 191 | except vimconn.vimconnException as e: |
| 192 | myvim = e |
| 193 | logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'], |
| 194 | vim['datacenter_id'], e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 195 | except Exception as e: |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 196 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e), |
| 197 | HTTP_Internal_Server_Error) |
| 198 | thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], |
| 199 | vim['vim_tenant_id']) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 200 | new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'], |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 201 | vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 202 | new_thread.start() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 203 | vim_threads["running"][thread_id] = new_thread |
| 204 | except db_base_Exception as e: |
| 205 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 46df967 | 2017-05-26 13:12:21 +0200 | [diff] [blame] | 206 | except ovim_module.ovimException as e: |
| 207 | message = str(e) |
| 208 | if message[:22] == "DATABASE wrong version": |
| 209 | message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\ |
| 210 | "at host {dbhost}".format( |
| 211 | msg=message[22:-3], dbname=global_config["db_ovim_name"], |
| 212 | dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"], |
| 213 | ver=message[-3:-1], dbhost=global_config["db_ovim_host"]) |
| 214 | raise NfvoException(message, HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 215 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 216 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 217 | def stop_service(): |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 218 | global ovim, global_config |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 219 | if ovim: |
| 220 | ovim.stop_service() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 221 | for thread_id,thread in vim_threads["running"].items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 222 | thread.insert_task("exit") |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 223 | vim_threads["deleting"][thread_id] = thread |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 224 | vim_threads["running"] = {} |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 225 | if global_config and global_config.get("console_thread"): |
| 226 | for thread in global_config["console_thread"]: |
| 227 | thread.terminate = True |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 228 | |
| tierno | 6ddeded | 2017-05-16 15:40:26 +0200 | [diff] [blame] | 229 | def get_version(): |
| 230 | return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"], |
| 231 | global_config["version_date"] )) |
| 232 | |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 233 | def clean_db(mydb): |
| 234 | """ |
| 235 | Clean unused or old entries at database to avoid unlimited growing |
| 236 | :param mydb: database connector |
| 237 | :return: None |
| 238 | """ |
| 239 | # get and delete unused vim_actions: all elements deleted, one week before, instance not present |
| 240 | now = t.time()-3600*24*7 |
| 241 | instance_action_id = None |
| 242 | nb_deleted = 0 |
| 243 | while True: |
| 244 | actions_to_delete = mydb.get_rows( |
| 245 | SELECT=("item", "item_id", "instance_action_id"), |
| 246 | FROM="vim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid " |
| 247 | "left join instance_scenarios as i on ia.instance_id=i.uuid", |
| 248 | WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None, |
| 249 | "va.status": ("DONE", "SUPERSEDED")}, |
| 250 | LIMIT=100 |
| 251 | ) |
| 252 | for to_delete in actions_to_delete: |
| 253 | mydb.delete_row(FROM="vim_actions", WHERE=to_delete) |
| 254 | if instance_action_id != to_delete["instance_action_id"]: |
| 255 | instance_action_id = to_delete["instance_action_id"] |
| 256 | mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id}) |
| 257 | nb_deleted += len(actions_to_delete) |
| 258 | if len(actions_to_delete) < 100: |
| 259 | break |
| 260 | if nb_deleted: |
| 261 | logger.debug("Removed {} unused vim_actions".format(nb_deleted)) |
| 262 | |
| 263 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 264 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 265 | def get_flavorlist(mydb, vnf_id, nfvo_tenant=None): |
| 266 | '''Obtain flavorList |
| 267 | return result, content: |
| 268 | <0, error_text upon error |
| 269 | nb_records, flavor_list on success |
| 270 | ''' |
| 271 | WHERE_dict={} |
| 272 | WHERE_dict['vnf_id'] = vnf_id |
| 273 | if nfvo_tenant is not None: |
| 274 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 275 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 276 | #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict ) |
| 277 | #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] | 278 | flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict ) |
| 279 | #print "get_flavor_list result:", result |
| 280 | #print "get_flavor_list content:", content |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 281 | flavorList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 282 | for flavor in flavors: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 283 | flavorList.append(flavor['flavor_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 284 | return flavorList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 285 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 286 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 287 | def get_imagelist(mydb, vnf_id, nfvo_tenant=None): |
| 288 | '''Obtain imageList |
| 289 | return result, content: |
| 290 | <0, error_text upon error |
| 291 | nb_records, flavor_list on success |
| 292 | ''' |
| 293 | WHERE_dict={} |
| 294 | WHERE_dict['vnf_id'] = vnf_id |
| 295 | if nfvo_tenant is not None: |
| 296 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 297 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 298 | #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 299 | images = mydb.get_rows(FROM='vms join images on vms.image_id=images.uuid',SELECT=('image_id',),WHERE=WHERE_dict ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 300 | imageList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 301 | for image in images: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 302 | imageList.append(image['image_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 303 | return imageList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 304 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 305 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 306 | def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None, |
| 307 | vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 308 | '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 309 | return dictionary with {datacenter_id: vim_class, ... }. vim_class contain: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 310 | '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] | 311 | raise exception upon error |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 312 | ''' |
| 313 | WHERE_dict={} |
| 314 | if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| 315 | if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 316 | 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] | 317 | if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name |
| 318 | 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] | 319 | if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name |
| 320 | 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] | 321 | 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] | 322 | 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] | 323 | '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] | 324 | 'user','passwd', 'dt.config as dt_config') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 325 | else: |
| 326 | from_ = 'datacenters as d' |
| 327 | 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] | 328 | try: |
| 329 | vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict ) |
| 330 | vim_dict={} |
| 331 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 332 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 333 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 334 | if vim["config"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 335 | extra.update(yaml.load(vim["config"])) |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 336 | if vim.get('dt_config'): |
| 337 | extra.update(yaml.load(vim["dt_config"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 338 | if vim["type"] not in vimconn_imported: |
| 339 | module_info=None |
| 340 | try: |
| 341 | module = "vimconn_" + vim["type"] |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 342 | pkg = __import__("osm_ro." + module) |
| 343 | vim_conn = getattr(pkg, module) |
| 344 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| 345 | # vim_conn = imp.load_module(vim["type"], *module_info) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 346 | vimconn_imported[vim["type"]] = vim_conn |
| 347 | except (IOError, ImportError) as e: |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 348 | # if module_info and module_info[0]: |
| 349 | # file.close(module_info[0]) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 350 | raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 351 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 352 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 353 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 354 | if 'datacenter_tenant_id' in vim: |
| 355 | thread_id = vim["datacenter_tenant_id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 356 | if thread_id not in vim_persistent_info: |
| 357 | vim_persistent_info[thread_id] = {} |
| 358 | persistent_info = vim_persistent_info[thread_id] |
| 359 | else: |
| 360 | persistent_info = {} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 361 | #if not tenant: |
| 362 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 363 | vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector( |
| 364 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 365 | tenant_id=vim.get('vim_tenant_id',vim_tenant), |
| 366 | tenant_name=vim.get('vim_tenant_name',vim_tenant_name), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 367 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 368 | user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd), |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 369 | config=extra, persistent_info=persistent_info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 370 | ) |
| 371 | except Exception as e: |
| 372 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error) |
| 373 | return vim_dict |
| 374 | except db_base_Exception as e: |
| 375 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 376 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 377 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 378 | def rollback(mydb, vims, rollback_list): |
| 379 | undeleted_items=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 380 | #delete things by reverse order |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 381 | for i in range(len(rollback_list)-1, -1, -1): |
| 382 | item = rollback_list[i] |
| 383 | if item["where"]=="vim": |
| 384 | if item["vim_id"] not in vims: |
| 385 | continue |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 386 | if is_task_id(item["uuid"]): |
| 387 | continue |
| 388 | vim = vims[item["vim_id"]] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 389 | try: |
| 390 | if item["what"]=="image": |
| 391 | vim.delete_image(item["uuid"]) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 392 | 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] | 393 | elif item["what"]=="flavor": |
| 394 | vim.delete_flavor(item["uuid"]) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 395 | mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]}) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 396 | elif item["what"]=="network": |
| 397 | vim.delete_network(item["uuid"]) |
| 398 | elif item["what"]=="vm": |
| 399 | vim.delete_vminstance(item["uuid"]) |
| 400 | except vimconn.vimconnException as e: |
| 401 | logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e)) |
| 402 | undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 403 | except db_base_Exception as e: |
| 404 | 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] | 405 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 406 | else: # where==mano |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 407 | try: |
| 408 | if item["what"]=="image": |
| 409 | mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]}) |
| 410 | elif item["what"]=="flavor": |
| 411 | mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]}) |
| 412 | except db_base_Exception as e: |
| 413 | logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e)) |
| 414 | undeleted_items.append("{} '{}'".format(item['what'], item["uuid"])) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 415 | if len(undeleted_items)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 416 | return True," Rollback successful." |
| 417 | else: |
| 418 | return False," Rollback fails to delete: " + str(undeleted_items) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 419 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 420 | |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 421 | def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 422 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 423 | #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 424 | vnfc_interfaces={} |
| 425 | for vnfc in vnf_descriptor["vnf"]["VNFC"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 426 | name_dict = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 427 | #dataplane interfaces |
| 428 | for numa in vnfc.get("numas",() ): |
| 429 | for interface in numa.get("interfaces",()): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 430 | if interface["name"] in name_dict: |
| 431 | raise NfvoException( |
| 432 | "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format( |
| 433 | vnfc["name"], interface["name"]), |
| 434 | HTTP_Bad_Request) |
| 435 | name_dict[ interface["name"] ] = "underlay" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 436 | #bridge interfaces |
| 437 | for interface in vnfc.get("bridge-ifaces",() ): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 438 | if interface["name"] in name_dict: |
| 439 | raise NfvoException( |
| 440 | "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format( |
| 441 | vnfc["name"], interface["name"]), |
| 442 | HTTP_Bad_Request) |
| 443 | name_dict[ interface["name"] ] = "overlay" |
| 444 | vnfc_interfaces[ vnfc["name"] ] = name_dict |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 445 | # check bood-data info |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 446 | # if "boot-data" in vnfc: |
| 447 | # # check that user-data is incompatible with users and config-files |
| 448 | # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"): |
| 449 | # raise NfvoException( |
| 450 | # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'", |
| 451 | # HTTP_Bad_Request) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 452 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 453 | #check if the info in external_connections matches with the one in the vnfcs |
| 454 | name_list=[] |
| 455 | for external_connection in vnf_descriptor["vnf"].get("external-connections",() ): |
| 456 | if external_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 457 | raise NfvoException( |
| 458 | "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format( |
| 459 | external_connection["name"]), |
| 460 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 461 | name_list.append(external_connection["name"]) |
| 462 | if external_connection["VNFC"] not in vnfc_interfaces: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 463 | raise NfvoException( |
| 464 | "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format( |
| 465 | external_connection["name"], external_connection["VNFC"]), |
| 466 | HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 467 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 468 | if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 469 | raise NfvoException( |
| 470 | "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 471 | external_connection["name"], |
| 472 | external_connection["local_iface_name"]), |
| 473 | HTTP_Bad_Request ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 474 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 475 | #check if the info in internal_connections matches with the one in the vnfcs |
| 476 | name_list=[] |
| 477 | for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ): |
| 478 | if internal_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 479 | raise NfvoException( |
| 480 | "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format( |
| 481 | internal_connection["name"]), |
| 482 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 483 | name_list.append(internal_connection["name"]) |
| 484 | #We should check that internal-connections of type "ptp" have only 2 elements |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 485 | |
| 486 | if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"): |
| 487 | raise NfvoException( |
| 488 | "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format( |
| 489 | internal_connection["name"], |
| 490 | 'ptp' if vnf_descriptor_version==1 else 'e-line', |
| 491 | 'data' if vnf_descriptor_version==1 else "e-lan"), |
| 492 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 493 | for port in internal_connection["elements"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 494 | vnf = port["VNFC"] |
| 495 | iface = port["local_iface_name"] |
| 496 | if vnf not in vnfc_interfaces: |
| 497 | raise NfvoException( |
| 498 | "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format( |
| 499 | internal_connection["name"], vnf), |
| 500 | HTTP_Bad_Request) |
| 501 | if iface not in vnfc_interfaces[ vnf ]: |
| 502 | raise NfvoException( |
| 503 | "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 504 | internal_connection["name"], iface), |
| 505 | HTTP_Bad_Request) |
| 506 | return -HTTP_Bad_Request, |
| 507 | if vnf_descriptor_version==1 and "type" not in internal_connection: |
| 508 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 509 | internal_connection["type"] = "bridge" |
| 510 | else: |
| 511 | internal_connection["type"] = "data" |
| 512 | if vnf_descriptor_version==2 and "implementation" not in internal_connection: |
| 513 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 514 | internal_connection["implementation"] = "overlay" |
| 515 | else: |
| 516 | internal_connection["implementation"] = "underlay" |
| 517 | if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \ |
| 518 | internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay": |
| 519 | raise NfvoException( |
| 520 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 521 | internal_connection["name"], |
| 522 | iface, 'bridge' if vnf_descriptor_version==1 else 'overlay', |
| 523 | 'data' if vnf_descriptor_version==1 else 'underlay'), |
| 524 | HTTP_Bad_Request) |
| 525 | if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \ |
| 526 | vnfc_interfaces[vnf][iface] == "underlay": |
| 527 | raise NfvoException( |
| 528 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 529 | internal_connection["name"], iface, |
| 530 | 'data' if vnf_descriptor_version==1 else 'underlay', |
| 531 | 'bridge' if vnf_descriptor_version==1 else 'overlay'), |
| 532 | HTTP_Bad_Request) |
| 533 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 534 | |
| tierno | 56d73d2 | 2017-08-02 13:53:02 +0200 | [diff] [blame] | 535 | 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] | 536 | #look if image exist |
| 537 | if only_create_at_vim: |
| 538 | image_mano_id = image_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 539 | if return_on_error == None: |
| 540 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 541 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 542 | if image_dict['location']: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 543 | images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']}) |
| 544 | else: |
| 545 | 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] | 546 | if len(images)>=1: |
| 547 | image_mano_id = images[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 548 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 549 | #create image in MANO DB |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 550 | temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None), |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 551 | 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None), |
| 552 | 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 553 | } |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 554 | #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] | 555 | image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True) |
| 556 | rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 557 | #create image at every vim |
| 558 | for vim_id,vim in vims.iteritems(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 559 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 560 | image_created="false" |
| 561 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 562 | image_db = mydb.get_rows(FROM="datacenters_images", |
| 563 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 564 | #look at VIM if this image exist |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 565 | try: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 566 | if image_dict['location'] is not None: |
| 567 | image_vim_id = vim.get_image_id_from_path(image_dict['location']) |
| 568 | else: |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 569 | filter_dict = {} |
| 570 | filter_dict['name'] = image_dict['universal_name'] |
| 571 | if image_dict.get('checksum') != None: |
| 572 | filter_dict['checksum'] = image_dict['checksum'] |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 573 | #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 574 | vim_images = vim.get_image_list(filter_dict) |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 575 | #logger.debug('>>>>>>>> VIM images: %s', str(vim_images)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 576 | if len(vim_images) > 1: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 577 | 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] | 578 | elif len(vim_images) == 0: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 579 | 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] | 580 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 581 | #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0])) |
| 582 | image_vim_id = vim_images[0]['id'] |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 583 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 584 | except vimconn.vimconnNotFoundException as e: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 585 | #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] | 586 | try: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 587 | #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None |
| 588 | if image_dict['location']: |
| 589 | image_vim_id = vim.new_image(image_dict) |
| 590 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id}) |
| 591 | image_created="true" |
| 592 | else: |
| garciadeblas | b6153a2 | 2017-02-06 15:38:33 +0100 | [diff] [blame] | 593 | #If we reach this point, then the image has image name, and optionally checksum, and could not be found |
| 594 | raise vimconn.vimconnException(str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 595 | except vimconn.vimconnException as e: |
| 596 | if return_on_error: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 597 | logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 598 | raise |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 599 | image_vim_id = None |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 600 | logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 601 | continue |
| 602 | except vimconn.vimconnException as e: |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 603 | if return_on_error: |
| 604 | logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e)) |
| 605 | raise |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 606 | 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] | 607 | image_vim_id = None |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 608 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 609 | #if we reach here, the image has been created or existed |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 610 | if len(image_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 611 | #add new vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 612 | mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id, |
| 613 | 'image_id':image_mano_id, |
| 614 | 'vim_id': image_vim_id, |
| 615 | 'created':image_created}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 616 | elif image_db[0]["vim_id"]!=image_vim_id: |
| 617 | #modify existing vim_id at datacenters_images |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 618 | 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] | 619 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 620 | return image_vim_id if only_create_at_vim else image_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 621 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 622 | |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 623 | def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 624 | temp_flavor_dict= {'disk':flavor_dict.get('disk',1), |
| 625 | 'ram':flavor_dict.get('ram'), |
| 626 | 'vcpus':flavor_dict.get('vcpus'), |
| 627 | } |
| 628 | if 'extended' in flavor_dict and flavor_dict['extended']==None: |
| 629 | del flavor_dict['extended'] |
| 630 | if 'extended' in flavor_dict: |
| 631 | temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256) |
| 632 | |
| 633 | #look if flavor exist |
| 634 | if only_create_at_vim: |
| 635 | flavor_mano_id = flavor_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 636 | if return_on_error == None: |
| 637 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 638 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 639 | flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 640 | if len(flavors)>=1: |
| 641 | flavor_mano_id = flavors[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 642 | else: |
| 643 | #create flavor |
| 644 | #create one by one the images of aditional disks |
| 645 | dev_image_list=[] #list of images |
| 646 | if 'extended' in flavor_dict and flavor_dict['extended']!=None: |
| 647 | dev_nb=0 |
| 648 | for device in flavor_dict['extended'].get('devices',[]): |
| garciadeblas | 41f18be | 2016-10-04 09:09:58 +0200 | [diff] [blame] | 649 | if "image" not in device and "image name" not in device: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 650 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 651 | image_dict={} |
| 652 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 653 | image_dict['universal_name']=device.get('image name') |
| 654 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 655 | image_dict['location']=device.get('image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 656 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 657 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 658 | image_metadata_dict = device.get('image metadata', None) |
| 659 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 660 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 661 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 662 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 663 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 664 | #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] | 665 | dev_image_list.append(image_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 666 | dev_nb += 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 667 | temp_flavor_dict['name'] = flavor_dict['name'] |
| 668 | temp_flavor_dict['description'] = flavor_dict.get('description',None) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 669 | content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True) |
| 670 | flavor_mano_id= content |
| 671 | rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 672 | #create flavor at every vim |
| 673 | if 'uuid' in flavor_dict: |
| 674 | del flavor_dict['uuid'] |
| 675 | flavor_vim_id=None |
| 676 | for vim_id,vim in vims.items(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 677 | datacenter_vim_id = vim["config"]["datacenter_tenant_id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 678 | flavor_created="false" |
| 679 | #look at database |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 680 | flavor_db = mydb.get_rows(FROM="datacenters_flavors", |
| 681 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 682 | #look at VIM if this flavor exist SKIPPED |
| 683 | #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location']) |
| 684 | #if res_vim < 0: |
| 685 | # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id |
| 686 | # continue |
| 687 | #elif res_vim==0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 688 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 689 | # Create the flavor in VIM |
| 690 | # Translate images at devices from MANO id to VIM id |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 691 | disk_list = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 692 | 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] | 693 | # make a copy of original devices |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 694 | devices_original=[] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 695 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 696 | for device in flavor_dict["extended"].get("devices",[]): |
| 697 | dev={} |
| 698 | dev.update(device) |
| 699 | devices_original.append(dev) |
| 700 | if 'image' in device: |
| 701 | del device['image'] |
| 702 | if 'image metadata' in device: |
| 703 | del device['image metadata'] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 704 | if 'image checksum' in device: |
| 705 | del device['image checksum'] |
| 706 | dev_nb = 0 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 707 | for index in range(0,len(devices_original)) : |
| 708 | device=devices_original[index] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 709 | if "image" not in device and "image name" not in device: |
| 710 | if 'size' in device: |
| 711 | disk_list.append({'size': device.get('size', default_volume_size)}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 712 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 713 | image_dict={} |
| 714 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 715 | image_dict['universal_name']=device.get('image name') |
| 716 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 717 | image_dict['location']=device.get('image') |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 718 | # image_dict['new_location']=device.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 719 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 720 | image_metadata_dict = device.get('image metadata', None) |
| 721 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 722 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 723 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 724 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 725 | 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] | 726 | image_dict["uuid"]=image_mano_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 727 | 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] | 728 | |
| 729 | #save disk information (image must be based on and size |
| 730 | disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)}) |
| 731 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 732 | flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id |
| 733 | dev_nb += 1 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 734 | if len(flavor_db)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 735 | #check that this vim_id exist in VIM, if not create |
| 736 | flavor_vim_id=flavor_db[0]["vim_id"] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 737 | try: |
| 738 | vim.get_flavor(flavor_vim_id) |
| 739 | continue #flavor exist |
| 740 | except vimconn.vimconnException: |
| 741 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 742 | #create flavor at vim |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 743 | logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"]) |
| 744 | try: |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 745 | flavor_vim_id = None |
| 746 | flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict) |
| 747 | flavor_create="false" |
| 748 | except vimconn.vimconnException as e: |
| 749 | pass |
| 750 | try: |
| 751 | if not flavor_vim_id: |
| 752 | flavor_vim_id = vim.new_flavor(flavor_dict) |
| 753 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id}) |
| 754 | flavor_created="true" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 755 | except vimconn.vimconnException as e: |
| 756 | if return_on_error: |
| 757 | logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 758 | raise |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 759 | logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 760 | flavor_vim_id = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 761 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 762 | #if reach here the flavor has been create or exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 763 | if len(flavor_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 764 | #add new vim_id at datacenters_flavors |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 765 | extended_devices_yaml = None |
| 766 | if len(disk_list) > 0: |
| 767 | extended_devices = dict() |
| 768 | extended_devices['disks'] = disk_list |
| 769 | extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256) |
| 770 | mydb.new_row('datacenters_flavors', |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 771 | {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id, |
| 772 | 'created': flavor_created, 'extended': extended_devices_yaml}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 773 | elif flavor_db[0]["vim_id"]!=flavor_vim_id: |
| 774 | #modify existing vim_id at datacenters_flavors |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 775 | mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, |
| 776 | WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id}) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 777 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 778 | return flavor_vim_id if only_create_at_vim else flavor_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 779 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 780 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 781 | def get_str(obj, field, length): |
| 782 | """ |
| 783 | Obtain the str value, |
| 784 | :param obj: |
| 785 | :param length: |
| 786 | :return: |
| 787 | """ |
| 788 | value = obj.get(field) |
| 789 | if value is not None: |
| 790 | value = str(value)[:length] |
| 791 | return value |
| 792 | |
| 793 | def _lookfor_or_create_image(db_image, mydb, descriptor): |
| 794 | """ |
| 795 | fill image content at db_image dictionary. Check if the image with this image and checksum exist |
| 796 | :param db_image: dictionary to insert data |
| 797 | :param mydb: database connector |
| 798 | :param descriptor: yang descriptor |
| 799 | :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image |
| 800 | """ |
| 801 | |
| 802 | db_image["name"] = get_str(descriptor, "image", 255) |
| 803 | db_image["checksum"] = get_str(descriptor, "image-checksum", 32) |
| 804 | if not db_image["checksum"]: # Ensure that if empty string, None is stored |
| 805 | db_image["checksum"] = None |
| 806 | if db_image["name"].startswith("/"): |
| 807 | db_image["location"] = db_image["name"] |
| 808 | existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]}) |
| 809 | else: |
| 810 | db_image["universal_name"] = db_image["name"] |
| 811 | existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'], |
| 812 | 'checksum': db_image['checksum']}) |
| 813 | if existing_images: |
| 814 | return existing_images[0]["uuid"] |
| 815 | else: |
| 816 | image_uuid = str(uuid4()) |
| 817 | db_image["uuid"] = image_uuid |
| 818 | return None |
| 819 | |
| 820 | def new_vnfd_v3(mydb, tenant_id, vnf_descriptor): |
| 821 | """ |
| 822 | Parses an OSM IM vnfd_catalog and insert at DB |
| 823 | :param mydb: |
| 824 | :param tenant_id: |
| 825 | :param vnf_descriptor: |
| 826 | :return: The list of cretated vnf ids |
| 827 | """ |
| 828 | try: |
| 829 | myvnfd = vnfd_catalog.vnfd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 830 | try: |
| 831 | pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd) |
| 832 | except Exception as e: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 833 | raise NfvoException("Error. Invalid VNF descriptor format " + str(e), HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 834 | db_vnfs = [] |
| 835 | db_nets = [] |
| 836 | db_vms = [] |
| 837 | db_vms_index = 0 |
| 838 | db_interfaces = [] |
| 839 | db_images = [] |
| 840 | db_flavors = [] |
| 841 | uuid_list = [] |
| 842 | vnfd_uuid_list = [] |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 843 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog") |
| 844 | if not vnfd_catalog_descriptor: |
| 845 | vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog") |
| 846 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd") |
| 847 | if not vnfd_descriptor_list: |
| 848 | vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd") |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 849 | for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues(): |
| 850 | vnfd = vnfd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 851 | |
| 852 | # table vnf |
| 853 | vnf_uuid = str(uuid4()) |
| 854 | uuid_list.append(vnf_uuid) |
| 855 | vnfd_uuid_list.append(vnf_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 856 | vnfd_id = get_str(vnfd, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 857 | db_vnf = { |
| 858 | "uuid": vnf_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 859 | "osm_id": vnfd_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 860 | "name": get_str(vnfd, "name", 255), |
| 861 | "description": get_str(vnfd, "description", 255), |
| 862 | "tenant_id": tenant_id, |
| 863 | "vendor": get_str(vnfd, "vendor", 255), |
| 864 | "short_name": get_str(vnfd, "short-name", 255), |
| 865 | "descriptor": str(vnf_descriptor)[:60000] |
| 866 | } |
| 867 | |
| tierno | e18ba43 | 2017-10-12 10:22:45 +0200 | [diff] [blame] | 868 | for vnfd_descriptor in vnfd_descriptor_list: |
| 869 | if vnfd_descriptor["id"] == str(vnfd["id"]): |
| 870 | break |
| 871 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 872 | # table nets (internal-vld) |
| 873 | net_id2uuid = {} # for mapping interface with network |
| 874 | for vld in vnfd.get("internal-vld").itervalues(): |
| 875 | net_uuid = str(uuid4()) |
| 876 | uuid_list.append(net_uuid) |
| 877 | db_net = { |
| 878 | "name": get_str(vld, "name", 255), |
| 879 | "vnf_id": vnf_uuid, |
| 880 | "uuid": net_uuid, |
| 881 | "description": get_str(vld, "description", 255), |
| 882 | "type": "bridge", # TODO adjust depending on connection point type |
| 883 | } |
| 884 | net_id2uuid[vld.get("id")] = net_uuid |
| 885 | db_nets.append(db_net) |
| 886 | |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 887 | # connection points vaiable declaration |
| 888 | cp_name2iface_uuid = {} |
| 889 | cp_name2vm_uuid = {} |
| 890 | cp_name2db_interface = {} |
| 891 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 892 | # table vms (vdus) |
| 893 | vdu_id2uuid = {} |
| 894 | vdu_id2db_table_index = {} |
| 895 | for vdu in vnfd.get("vdu").itervalues(): |
| 896 | vm_uuid = str(uuid4()) |
| 897 | uuid_list.append(vm_uuid) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 898 | vdu_id = get_str(vdu, "id", 255) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 899 | db_vm = { |
| 900 | "uuid": vm_uuid, |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 901 | "osm_id": vdu_id, |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 902 | "name": get_str(vdu, "name", 255), |
| 903 | "description": get_str(vdu, "description", 255), |
| 904 | "vnf_id": vnf_uuid, |
| 905 | } |
| 906 | vdu_id2uuid[db_vm["osm_id"]] = vm_uuid |
| 907 | vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index |
| 908 | if vdu.get("count"): |
| 909 | db_vm["count"] = int(vdu["count"]) |
| 910 | |
| 911 | # table image |
| 912 | image_present = False |
| 913 | if vdu.get("image"): |
| 914 | image_present = True |
| 915 | db_image = {} |
| 916 | image_uuid = _lookfor_or_create_image(db_image, mydb, vdu) |
| 917 | if not image_uuid: |
| 918 | image_uuid = db_image["uuid"] |
| 919 | db_images.append(db_image) |
| 920 | db_vm["image_id"] = image_uuid |
| 921 | |
| 922 | # volumes |
| 923 | devices = [] |
| 924 | if vdu.get("volumes"): |
| 925 | for volume_key in sorted(vdu["volumes"]): |
| 926 | volume = vdu["volumes"][volume_key] |
| 927 | if not image_present: |
| 928 | # Convert the first volume to vnfc.image |
| 929 | image_present = True |
| 930 | db_image = {} |
| 931 | image_uuid = _lookfor_or_create_image(db_image, mydb, volume) |
| 932 | if not image_uuid: |
| 933 | image_uuid = db_image["uuid"] |
| 934 | db_images.append(db_image) |
| 935 | db_vm["image_id"] = image_uuid |
| 936 | else: |
| 937 | # Add Openmano devices |
| 938 | device = {} |
| 939 | device["type"] = str(volume.get("device-type")) |
| 940 | if volume.get("size"): |
| 941 | device["size"] = int(volume["size"]) |
| 942 | if volume.get("image"): |
| 943 | device["image name"] = str(volume["image"]) |
| 944 | if volume.get("image-checksum"): |
| 945 | device["image checksum"] = str(volume["image-checksum"]) |
| 946 | devices.append(device) |
| 947 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 948 | # cloud-init |
| 949 | boot_data = {} |
| 950 | if vdu.get("cloud-init"): |
| 951 | boot_data["user-data"] = str(vdu["cloud-init"]) |
| 952 | elif vdu.get("cloud-init-file"): |
| 953 | # TODO Where this file content is present??? |
| 954 | # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]] |
| 955 | boot_data["user-data"] = str(vdu["cloud-init-file"]) |
| 956 | |
| 957 | if vdu.get("supplemental-boot-data"): |
| 958 | if vdu["supplemental-boot-data"].get('boot-data-drive'): |
| 959 | boot_data['boot-data-drive'] = True |
| 960 | if vdu["supplemental-boot-data"].get('config-file'): |
| 961 | om_cfgfile_list = list() |
| 962 | for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues(): |
| 963 | # TODO Where this file content is present??? |
| 964 | cfg_source = str(custom_config_file["source"]) |
| 965 | om_cfgfile_list.append({"dest": custom_config_file["dest"], |
| 966 | "content": cfg_source}) |
| 967 | boot_data['config-files'] = om_cfgfile_list |
| 968 | if boot_data: |
| 969 | db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256) |
| 970 | |
| 971 | db_vms.append(db_vm) |
| 972 | db_vms_index += 1 |
| 973 | |
| 974 | # table interfaces (internal/external interfaces) |
| 975 | flavor_epa_interfaces = [] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 976 | vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU |
| 977 | # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()): |
| 978 | for iface in vdu.get("interface").itervalues(): |
| 979 | flavor_epa_interface = {} |
| 980 | iface_uuid = str(uuid4()) |
| 981 | uuid_list.append(iface_uuid) |
| 982 | db_interface = { |
| 983 | "uuid": iface_uuid, |
| 984 | "internal_name": get_str(iface, "name", 255), |
| 985 | "vm_id": vm_uuid, |
| 986 | } |
| 987 | flavor_epa_interface["name"] = db_interface["internal_name"] |
| 988 | if iface.get("virtual-interface").get("vpci"): |
| 989 | db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12) |
| 990 | flavor_epa_interface["vpci"] = db_interface["vpci"] |
| 991 | |
| 992 | if iface.get("virtual-interface").get("bandwidth"): |
| 993 | bps = int(iface.get("virtual-interface").get("bandwidth")) |
| 994 | db_interface["bw"] = int(math.ceil(bps/1000000.0)) |
| 995 | flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"]) |
| 996 | |
| 997 | if iface.get("virtual-interface").get("type") == "OM-MGMT": |
| 998 | db_interface["type"] = "mgmt" |
| 999 | elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000"): |
| 1000 | db_interface["type"] = "bridge" |
| 1001 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1002 | elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 1003 | db_interface["type"] = "data" |
| 1004 | db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12) |
| 1005 | flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \ |
| 1006 | else "yes" |
| 1007 | flavor_epa_interfaces.append(flavor_epa_interface) |
| 1008 | else: |
| 1009 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual" |
| 1010 | "-interface':'type':'{}'. Interface type is not supported".format( |
| 1011 | vnfd_id, vdu_id, iface.get("virtual-interface").get("type")), |
| 1012 | HTTP_Bad_Request) |
| 1013 | |
| 1014 | if iface.get("external-connection-point-ref"): |
| 1015 | try: |
| 1016 | cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")] |
| 1017 | db_interface["external_name"] = get_str(cp, "name", 255) |
| 1018 | cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid |
| 1019 | cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid |
| 1020 | cp_name2db_interface[db_interface["external_name"]] = db_interface |
| 1021 | for cp_descriptor in vnfd_descriptor["connection-point"]: |
| 1022 | if cp_descriptor["name"] == db_interface["external_name"]: |
| 1023 | break |
| 1024 | else: |
| 1025 | raise KeyError() |
| 1026 | |
| 1027 | if vdu_id in vdu_id2cp_name: |
| 1028 | vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU |
| 1029 | else: |
| 1030 | vdu_id2cp_name[vdu_id] = db_interface["external_name"] |
| 1031 | |
| 1032 | # port security |
| 1033 | if str(cp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1034 | db_interface["port_security"] = 0 |
| 1035 | elif str(cp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1036 | db_interface["port_security"] = 1 |
| 1037 | except KeyError: |
| 1038 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| 1039 | "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present" |
| 1040 | " at connection-point".format( |
| 1041 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| 1042 | cp=iface.get("vnfd-connection-point-ref")), |
| 1043 | HTTP_Bad_Request) |
| 1044 | elif iface.get("internal-connection-point-ref"): |
| 1045 | try: |
| 1046 | for vld in vnfd.get("internal-vld").itervalues(): |
| 1047 | for cp in vld.get("internal-connection-point").itervalues(): |
| 1048 | if cp.get("id-ref") == iface.get("internal-connection-point-ref"): |
| 1049 | db_interface["net_id"] = net_id2uuid[vld.get("id")] |
| 1050 | for cp_descriptor in vnfd_descriptor["connection-point"]: |
| gcalvino | d9f2b88 | 2017-12-04 16:47:40 +0100 | [diff] [blame] | 1051 | if cp_descriptor["name"] == db_interface["internal_name"]: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1052 | break |
| 1053 | if str(cp_descriptor.get("port-security-enabled")).lower() == "false": |
| 1054 | db_interface["port_security"] = 0 |
| 1055 | elif str(cp_descriptor.get("port-security-enabled")).lower() == "true": |
| 1056 | db_interface["port_security"] = 1 |
| 1057 | break |
| 1058 | except KeyError: |
| 1059 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':" |
| gcalvino | d9f2b88 | 2017-12-04 16:47:40 +0100 | [diff] [blame] | 1060 | "'interface[{iface}]':'internal-connection-point-ref':'{cp}' is not" |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1061 | " referenced by any internal-vld".format( |
| 1062 | vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], |
| gcalvino | d9f2b88 | 2017-12-04 16:47:40 +0100 | [diff] [blame] | 1063 | cp=iface.get("internal-connection-point-ref")), |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1064 | HTTP_Bad_Request) |
| 1065 | if iface.get("position") is not None: |
| 1066 | db_interface["created_at"] = int(iface.get("position")) - 1000 |
| 1067 | db_interfaces.append(db_interface) |
| 1068 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1069 | # table flavors |
| 1070 | db_flavor = { |
| 1071 | "name": get_str(vdu, "name", 250) + "-flv", |
| 1072 | "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)), |
| 1073 | "ram": int(vdu["vm-flavor"].get("memory-mb", 1)), |
| 1074 | "disk": int(vdu["vm-flavor"].get("storage-gb", 1)), |
| 1075 | } |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1076 | # TODO revise the case of several numa-node-policy node |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1077 | extended = {} |
| 1078 | numa = {} |
| 1079 | if devices: |
| 1080 | extended["devices"] = devices |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1081 | if flavor_epa_interfaces: |
| 1082 | numa["interfaces"] = flavor_epa_interfaces |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1083 | if vdu.get("guest-epa"): # TODO or dedicated_int: |
| 1084 | epa_vcpu_set = False |
| 1085 | if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int: |
| 1086 | numa_node_policy = vdu["guest-epa"].get("numa-node-policy") |
| 1087 | if numa_node_policy.get("node"): |
| tierno | cf59669 | 2017-11-20 15:47:51 +0100 | [diff] [blame] | 1088 | numa_node = numa_node_policy["node"].values()[0] |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1089 | if numa_node.get("num-cores"): |
| 1090 | numa["cores"] = numa_node["num-cores"] |
| 1091 | epa_vcpu_set = True |
| 1092 | if numa_node.get("paired-threads"): |
| 1093 | if numa_node["paired-threads"].get("num-paired-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1094 | numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1095 | epa_vcpu_set = True |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1096 | if len(numa_node["paired-threads"].get("paired-thread-ids")): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1097 | numa["paired-threads-id"] = [] |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1098 | for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues(): |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1099 | numa["paired-threads-id"].append( |
| 1100 | (str(pair["thread-a"]), str(pair["thread-b"])) |
| 1101 | ) |
| 1102 | if numa_node.get("num-threads"): |
| tierno | 39dddcc | 2017-10-05 18:48:06 +0200 | [diff] [blame] | 1103 | numa["threads"] = int(numa_node["num-threads"]) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1104 | epa_vcpu_set = True |
| 1105 | if numa_node.get("memory-mb"): |
| 1106 | numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1) |
| 1107 | if vdu["guest-epa"].get("mempage-size"): |
| 1108 | if vdu["guest-epa"]["mempage-size"] != "SMALL": |
| 1109 | numa["memory"] = max(int(db_flavor["ram"] / 1024), 1) |
| 1110 | if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set: |
| 1111 | if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED": |
| 1112 | if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \ |
| 1113 | vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER": |
| 1114 | numa["cores"] = max(db_flavor["vcpus"], 1) |
| 1115 | else: |
| 1116 | numa["threads"] = max(db_flavor["vcpus"], 1) |
| 1117 | if numa: |
| 1118 | extended["numas"] = [numa] |
| 1119 | if extended: |
| 1120 | extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256) |
| 1121 | db_flavor["extended"] = extended_text |
| 1122 | # look if flavor exist |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1123 | temp_flavor_dict = {'disk': db_flavor.get('disk', 1), |
| 1124 | 'ram': db_flavor.get('ram'), |
| 1125 | 'vcpus': db_flavor.get('vcpus'), |
| 1126 | 'extended': db_flavor.get('extended') |
| 1127 | } |
| 1128 | existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 1129 | if existing_flavors: |
| 1130 | flavor_uuid = existing_flavors[0]["uuid"] |
| 1131 | else: |
| 1132 | flavor_uuid = str(uuid4()) |
| 1133 | uuid_list.append(flavor_uuid) |
| 1134 | db_flavor["uuid"] = flavor_uuid |
| 1135 | db_flavors.append(db_flavor) |
| 1136 | db_vm["flavor_id"] = flavor_uuid |
| 1137 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1138 | # VNF affinity and antiaffinity |
| 1139 | for pg in vnfd.get("placement-groups").itervalues(): |
| 1140 | pg_name = get_str(pg, "name", 255) |
| 1141 | for vdu in pg.get("member-vdus").itervalues(): |
| 1142 | vdu_id = get_str(vdu, "member-vdu-ref", 255) |
| 1143 | if vdu_id not in vdu_id2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1144 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':" |
| 1145 | "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1146 | vnf=vnfd_id, pg=pg_name, vdu=vdu_id), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1147 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1148 | db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name |
| 1149 | # TODO consider the case of isolation and not colocation |
| 1150 | # if pg.get("strategy") == "ISOLATION": |
| 1151 | |
| 1152 | # VNF mgmt configuration |
| 1153 | mgmt_access = {} |
| 1154 | if vnfd["mgmt-interface"].get("vdu-id"): |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1155 | mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255) |
| 1156 | if mgmt_vdu_id not in vdu_id2uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1157 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':" |
| 1158 | "'{vdu}'. Reference to a non-existing vdu".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1159 | vnf=vnfd_id, vdu=mgmt_vdu_id), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1160 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1161 | mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1162 | # if only one cp is defined by this VDU, mark this interface as of type "mgmt" |
| 1163 | if vdu_id2cp_name.get(mgmt_vdu_id): |
| 1164 | cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt" |
| 1165 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1166 | if vnfd["mgmt-interface"].get("ip-address"): |
| 1167 | mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address")) |
| 1168 | if vnfd["mgmt-interface"].get("cp"): |
| 1169 | if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1170 | raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. " |
| 1171 | "Reference to a non-existing connection-point".format( |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 1172 | vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]), |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1173 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1174 | mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]] |
| 1175 | mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]] |
| tierno | e2ff1ce | 2017-11-02 17:01:10 +0100 | [diff] [blame] | 1176 | # mark this interface as of type mgmt |
| 1177 | cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt" |
| 1178 | |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 1179 | 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] | 1180 | "default-user", 64) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1181 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1182 | if default_user: |
| 1183 | mgmt_access["default_user"] = default_user |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1184 | required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}), |
| 1185 | "required", 6) |
| 1186 | if required: |
| 1187 | mgmt_access["required"] = required |
| 1188 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1189 | if mgmt_access: |
| 1190 | db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256) |
| 1191 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 1192 | |
| 1193 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1194 | db_vnfs.append(db_vnf) |
| 1195 | db_tables=[ |
| 1196 | {"vnfs": db_vnfs}, |
| 1197 | {"nets": db_nets}, |
| 1198 | {"images": db_images}, |
| 1199 | {"flavors": db_flavors}, |
| 1200 | {"vms": db_vms}, |
| 1201 | {"interfaces": db_interfaces}, |
| 1202 | ] |
| 1203 | |
| 1204 | logger.debug("create_vnf Deployment done vnfDict: %s", |
| 1205 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 1206 | mydb.new_rows(db_tables, uuid_list) |
| 1207 | return vnfd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 1208 | except NfvoException: |
| 1209 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1210 | except Exception as e: |
| 1211 | logger.error("Exception {}".format(e)) |
| 1212 | raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request) |
| 1213 | |
| 1214 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1215 | def new_vnf(mydb, tenant_id, vnf_descriptor): |
| 1216 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1217 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1218 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1219 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1220 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1221 | vims = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1222 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1223 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1224 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1225 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1226 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 1227 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1228 | else: |
| 1229 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1230 | # 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] | 1231 | if global_config["auto_push_VNF_to_VIMs"]: |
| 1232 | vims = get_vim(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1233 | |
| 1234 | # Step 4. Review the descriptor and add missing fields |
| 1235 | #print vnf_descriptor |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1236 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1237 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1238 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1239 | if "physical" in vnf_descriptor['vnf']: |
| 1240 | del vnf_descriptor['vnf']['physical'] |
| 1241 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1242 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1243 | # 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] | 1244 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1245 | 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] | 1246 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1247 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1248 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1249 | 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] | 1250 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1251 | 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] | 1252 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1253 | VNFCitem={} |
| 1254 | VNFCitem["name"] = vnfc['name'] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 1255 | VNFCitem["availability_zone"] = vnfc.get('availability_zone') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1256 | 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] | 1257 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1258 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1259 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1260 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1261 | 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] | 1262 | myflavorDict["description"] = VNFCitem["description"] |
| 1263 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1264 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| 1265 | myflavorDict["disk"] = vnfc.get("disk", 1) |
| 1266 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1267 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1268 | devices = vnfc.get("devices") |
| 1269 | if devices != None: |
| 1270 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1271 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1272 | # TODO: |
| 1273 | # 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] | 1274 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1275 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1276 | # Previous code has been commented |
| 1277 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1278 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1279 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1280 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1281 | #else: |
| 1282 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1283 | # if result2: |
| 1284 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 1285 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 1286 | # else: |
| 1287 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 1288 | 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] | 1289 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1290 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1291 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1292 | |
| 1293 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1294 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1295 | # Step 6.2 New flavors are created in the VIM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1296 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1297 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1298 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1299 | VNFCitem["flavor_id"] = flavor_id |
| 1300 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1301 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1302 | logger.debug("Creating new images in the VIM for each VNFC") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1303 | # Step 6.3 New images are created in the VIM |
| 1304 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1305 | #This "for" loop might be integrated with the previous one |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1306 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1307 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1308 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1309 | image_dict={} |
| 1310 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1311 | image_dict['universal_name']=vnfc.get('image name') |
| 1312 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1313 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1314 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1315 | image_dict['checksum']=vnfc.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1316 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1317 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1318 | if image_metadata_dict is not None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1319 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1320 | image_dict['metadata']=image_metadata_str |
| 1321 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1322 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1323 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1324 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1325 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1326 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1327 | if vnfc.get("boot-data"): |
| 1328 | 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] | 1329 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1330 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1331 | # Step 7. Storing the VNF descriptor in the repository |
| 1332 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1333 | 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] | 1334 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1335 | # Step 8. Adding the VNF to the NFVO DB |
| 1336 | vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1337 | return vnf_id |
| 1338 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1339 | _, message = rollback(mydb, vims, rollback_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1340 | if isinstance(e, db_base_Exception): |
| 1341 | error_text = "Exception at database" |
| 1342 | elif isinstance(e, KeyError): |
| 1343 | error_text = "KeyError exception " |
| 1344 | e.http_code = HTTP_Internal_Server_Error |
| 1345 | else: |
| 1346 | error_text = "Exception at VIM" |
| 1347 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1348 | #logger.error("start_scenario %s", error_text) |
| 1349 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1350 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1351 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1352 | def new_vnf_v02(mydb, tenant_id, vnf_descriptor): |
| 1353 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1354 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1355 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1356 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1357 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 1358 | vims = {} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1359 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1360 | check_tenant(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1361 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 1362 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| 1363 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 1364 | HTTP_Unauthorized) |
| 1365 | else: |
| 1366 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 1367 | # 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] | 1368 | if global_config["auto_push_VNF_to_VIMs"]: |
| 1369 | vims = get_vim(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1370 | |
| 1371 | # Step 4. Review the descriptor and add missing fields |
| 1372 | #print vnf_descriptor |
| 1373 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| 1374 | vnf_name = vnf_descriptor['vnf']['name'] |
| 1375 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 1376 | if "physical" in vnf_descriptor['vnf']: |
| 1377 | del vnf_descriptor['vnf']['physical'] |
| 1378 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 1379 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1380 | # 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] | 1381 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 1382 | 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] | 1383 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1384 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 1385 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 1386 | rollback_list = [] # It will contain the new images created in mano. It is used for rollback |
| 1387 | try: |
| 1388 | logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC") |
| 1389 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1390 | VNFCitem={} |
| 1391 | VNFCitem["name"] = vnfc['name'] |
| 1392 | 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] | 1393 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1394 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1395 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1396 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1397 | 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] | 1398 | myflavorDict["description"] = VNFCitem["description"] |
| 1399 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 1400 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| 1401 | myflavorDict["disk"] = vnfc.get("disk", 1) |
| 1402 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1403 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1404 | devices = vnfc.get("devices") |
| 1405 | if devices != None: |
| 1406 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1407 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1408 | # TODO: |
| 1409 | # 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] | 1410 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 1411 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1412 | # Previous code has been commented |
| 1413 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 1414 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 1415 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 1416 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 1417 | #else: |
| 1418 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 1419 | # if result2: |
| 1420 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 1421 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 1422 | # else: |
| 1423 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 1424 | 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] | 1425 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1426 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 1427 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 1428 | |
| 1429 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1430 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1431 | # Step 6.2 New flavors are created in the VIM |
| 1432 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| 1433 | |
| 1434 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| 1435 | VNFCitem["flavor_id"] = flavor_id |
| 1436 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1437 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1438 | logger.debug("Creating new images in the VIM for each VNFC") |
| 1439 | # Step 6.3 New images are created in the VIM |
| 1440 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1441 | #This "for" loop might be integrated with the previous one |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1442 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 1443 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 1444 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1445 | image_dict={} |
| 1446 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 1447 | image_dict['universal_name']=vnfc.get('image name') |
| 1448 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 1449 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 1450 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1451 | image_dict['checksum']=vnfc.get('image checksum') |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1452 | image_metadata_dict = vnfc.get('image metadata', None) |
| 1453 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1454 | if image_metadata_dict is not None: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1455 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 1456 | image_dict['metadata']=image_metadata_str |
| 1457 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| 1458 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 1459 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| 1460 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 1461 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 1462 | VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1463 | if vnfc.get("boot-data"): |
| 1464 | 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] | 1465 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1466 | # Step 7. Storing the VNF descriptor in the repository |
| 1467 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 1468 | 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] | 1469 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1470 | # Step 8. Adding the VNF to the NFVO DB |
| 1471 | vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 1472 | return vnf_id |
| 1473 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| 1474 | _, message = rollback(mydb, vims, rollback_list) |
| 1475 | if isinstance(e, db_base_Exception): |
| 1476 | error_text = "Exception at database" |
| 1477 | elif isinstance(e, KeyError): |
| 1478 | error_text = "KeyError exception " |
| 1479 | e.http_code = HTTP_Internal_Server_Error |
| 1480 | else: |
| 1481 | error_text = "Exception at VIM" |
| 1482 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1483 | #logger.error("start_scenario %s", error_text) |
| 1484 | raise NfvoException(error_text, e.http_code) |
| 1485 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1486 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1487 | def get_vnf_id(mydb, tenant_id, vnf_id): |
| 1488 | #check valid tenant_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1489 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1490 | #obtain data |
| 1491 | where_or = {} |
| 1492 | if tenant_id != "any": |
| 1493 | where_or["tenant_id"] = tenant_id |
| 1494 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1495 | vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") |
| 1496 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 1497 | vnf_id = vnf["uuid"] |
| 1498 | filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1499 | 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] | 1500 | #change_keys_http2db(filtered_content, http2db_vnf, reverse=True) |
| 1501 | data={'vnf' : filtered_content} |
| 1502 | #GET VM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1503 | 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] | 1504 | SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description', |
| 1505 | 'boot_data'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1506 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1507 | if len(content)==0: |
| 1508 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1509 | # change boot_data into boot-data |
| 1510 | for vm in content: |
| 1511 | if vm.get("boot_data"): |
| 1512 | vm["boot-data"] = yaml.safe_load(vm["boot_data"]) |
| 1513 | del vm["boot_data"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1514 | |
| 1515 | data['vnf']['VNFC'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1516 | #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] | 1517 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1518 | #GET NET |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1519 | 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] | 1520 | SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'), |
| 1521 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1522 | data['vnf']['nets'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1523 | |
| 1524 | #GET ip-profile for each net |
| 1525 | for net in data['vnf']['nets']: |
| 1526 | ipprofiles = mydb.get_rows(FROM='ip_profiles', |
| 1527 | SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'), |
| 1528 | WHERE={'net_id': net["uuid"]} ) |
| 1529 | if len(ipprofiles)==1: |
| 1530 | net["ip_profile"] = ipprofiles[0] |
| 1531 | elif len(ipprofiles)>1: |
| 1532 | 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] | 1533 | |
| 1534 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1535 | #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] | 1536 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1537 | #GET External Interfaces |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1538 | 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] | 1539 | SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \ |
| 1540 | '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] | 1541 | WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1542 | #print content |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1543 | data['vnf']['external-connections'] = content |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1544 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1545 | return data |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1546 | |
| 1547 | |
| 1548 | def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None): |
| 1549 | # Check tenant exist |
| 1550 | if tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1551 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1552 | # Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1553 | vims = get_vim(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1554 | else: |
| 1555 | vims={} |
| 1556 | |
| 1557 | # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided" |
| 1558 | where_or = {} |
| 1559 | if tenant_id != "any": |
| 1560 | where_or["tenant_id"] = tenant_id |
| 1561 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1562 | 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] | 1563 | vnf_id = vnf["uuid"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1564 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1565 | # "Getting the list of flavors and tenants of the VNF" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1566 | flavorList = get_flavorlist(mydb, vnf_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1567 | if len(flavorList)==0: |
| 1568 | 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] | 1569 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1570 | imageList = get_imagelist(mydb, vnf_id) |
| 1571 | if len(imageList)==0: |
| 1572 | 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] | 1573 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1574 | deleted = mydb.delete_row_by_id('vnfs', vnf_id) |
| 1575 | if deleted == 0: |
| 1576 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1577 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1578 | undeletedItems = [] |
| 1579 | for flavor in flavorList: |
| 1580 | #check if flavor is used by other vnf |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1581 | try: |
| 1582 | c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} ) |
| 1583 | if len(c) > 0: |
| 1584 | logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor) |
| 1585 | continue |
| 1586 | #flavor not used, must be deleted |
| 1587 | #delelte at VIM |
| 1588 | c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1589 | for flavor_vim in c: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 1590 | if flavor_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1591 | continue |
| 1592 | if flavor_vim['created']=='false': #skip this flavor because not created by openmano |
| 1593 | continue |
| 1594 | myvim=vims[ flavor_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1595 | try: |
| 1596 | myvim.delete_flavor(flavor_vim["vim_id"]) |
| 1597 | except vimconn.vimconnNotFoundException as e: |
| 1598 | logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] ) |
| 1599 | except vimconn.vimconnException as e: |
| 1600 | logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s", |
| 1601 | flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1602 | undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] )) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1603 | #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors |
| 1604 | mydb.delete_row_by_id('flavors', flavor) |
| 1605 | except db_base_Exception as e: |
| 1606 | logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1607 | undeletedItems.append("flavor %s" % flavor) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1608 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1609 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1610 | for image in imageList: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1611 | try: |
| 1612 | #check if image is used by other vnf |
| 1613 | c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} ) |
| 1614 | if len(c) > 0: |
| 1615 | logger.debug("Image '%s' not deleted because it is being used by another VNF", image) |
| 1616 | continue |
| 1617 | #image not used, must be deleted |
| 1618 | #delelte at VIM |
| 1619 | c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1620 | for image_vim in c: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 1621 | 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] | 1622 | continue |
| 1623 | if image_vim['created']=='false': #skip this image because not created by openmano |
| 1624 | continue |
| 1625 | myvim=vims[ image_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1626 | try: |
| 1627 | myvim.delete_image(image_vim["vim_id"]) |
| 1628 | except vimconn.vimconnNotFoundException as e: |
| 1629 | logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] ) |
| 1630 | except vimconn.vimconnException as e: |
| 1631 | logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s", |
| 1632 | image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1633 | 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] | 1634 | #delete image from Database, using table images and with cascade foreign key also at datacenters_images |
| 1635 | mydb.delete_row_by_id('images', image) |
| 1636 | except db_base_Exception as e: |
| 1637 | 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] | 1638 | undeletedItems.append("image %s" % image) |
| 1639 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1640 | return vnf_id + " " + vnf["name"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1641 | #if undeletedItems: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1642 | # return "delete_vnf. Undeleted: %s" %(undeletedItems) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1643 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1644 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1645 | def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None): |
| 1646 | result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name) |
| 1647 | if result < 0: |
| 1648 | return result, vims |
| 1649 | elif result == 0: |
| 1650 | return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name |
| 1651 | myvim = vims.values()[0] |
| 1652 | result,servers = myvim.get_hosts_info() |
| 1653 | if result < 0: |
| 1654 | return result, servers |
| 1655 | topology = {'name':myvim['name'] , 'servers': servers} |
| 1656 | return result, topology |
| 1657 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1658 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1659 | def get_hosts(mydb, nfvo_tenant_id): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1660 | vims = get_vim(mydb, nfvo_tenant_id) |
| 1661 | if len(vims) == 0: |
| 1662 | raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found) |
| 1663 | elif len(vims)>1: |
| 1664 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 1665 | 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] | 1666 | myvim = vims.values()[0] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1667 | try: |
| 1668 | hosts = myvim.get_hosts() |
| 1669 | 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] | 1670 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1671 | datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] } |
| 1672 | for host in hosts: |
| 1673 | server={'name':host['name'], 'vms':[]} |
| 1674 | for vm in host['instances']: |
| 1675 | #get internal name and model |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1676 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1677 | c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\ |
| 1678 | WHERE={'vim_vm_id':vm['id']} ) |
| 1679 | if len(c) == 0: |
| 1680 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id'])) |
| 1681 | continue |
| 1682 | server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1683 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1684 | except db_base_Exception as e: |
| 1685 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e))) |
| 1686 | datacenter['Datacenters'][0]['servers'].append(server) |
| 1687 | #return -400, "en construccion" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1688 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1689 | #print 'datacenters '+ json.dumps(datacenter, indent=4) |
| 1690 | return datacenter |
| 1691 | except vimconn.vimconnException as e: |
| 1692 | 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] | 1693 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1694 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1695 | def new_scenario(mydb, tenant_id, topo): |
| 1696 | |
| 1697 | # result, vims = get_vim(mydb, tenant_id) |
| 1698 | # if result < 0: |
| 1699 | # return result, vims |
| 1700 | #1: parse input |
| 1701 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1702 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1703 | if "tenant_id" in topo: |
| 1704 | if topo["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1705 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id), |
| 1706 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1707 | else: |
| 1708 | tenant_id=None |
| 1709 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1710 | #1.1: get VNFs and external_networks (other_nets). |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1711 | vnfs={} |
| 1712 | other_nets={} #external_networks, bridge_networks and data_networkds |
| 1713 | nodes = topo['topology']['nodes'] |
| 1714 | for k in nodes.keys(): |
| 1715 | if nodes[k]['type'] == 'VNF': |
| 1716 | vnfs[k] = nodes[k] |
| 1717 | vnfs[k]['ifaces'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1718 | elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1719 | other_nets[k] = nodes[k] |
| 1720 | other_nets[k]['external']=True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1721 | elif nodes[k]['type'] == 'network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1722 | other_nets[k] = nodes[k] |
| 1723 | other_nets[k]['external']=False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1724 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1725 | |
| 1726 | #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces |
| 1727 | for name,vnf in vnfs.items(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1728 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1729 | error_text = "" |
| 1730 | error_pos = "'topology':'nodes':'" + name + "'" |
| 1731 | if 'vnf_id' in vnf: |
| 1732 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1733 | where['uuid'] = vnf['vnf_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1734 | if 'VNF model' in vnf: |
| 1735 | error_text += " 'VNF model' " + vnf['VNF model'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1736 | where['name'] = vnf['VNF model'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1737 | if len(where) == 1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1738 | 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] | 1739 | |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1740 | vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), |
| 1741 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1742 | WHERE=where) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1743 | if len(vnf_db)==0: |
| 1744 | raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| 1745 | elif len(vnf_db)>1: |
| 1746 | 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] | 1747 | vnf['uuid']=vnf_db[0]['uuid'] |
| 1748 | vnf['description']=vnf_db[0]['description'] |
| 1749 | #get external interfaces |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1750 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), |
| 1751 | 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] | 1752 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1753 | for ext_iface in ext_ifaces: |
| 1754 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']} |
| 1755 | |
| 1756 | #1.4 get list of connections |
| 1757 | conections = topo['topology']['connections'] |
| 1758 | conections_list = [] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1759 | conections_list_name = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1760 | for k in conections.keys(): |
| 1761 | if type(conections[k]['nodes'])==dict: #dict with node:iface pairs |
| 1762 | ifaces_list = conections[k]['nodes'].items() |
| 1763 | elif type(conections[k]['nodes'])==list: #list with dictionary |
| 1764 | ifaces_list=[] |
| 1765 | conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] ) |
| 1766 | for k2 in conection_pair_list: |
| 1767 | ifaces_list += k2 |
| 1768 | |
| 1769 | con_type = conections[k].get("type", "link") |
| 1770 | if con_type != "link": |
| 1771 | if k in other_nets: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1772 | 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] | 1773 | other_nets[k] = {'external': False} |
| 1774 | if conections[k].get("graph"): |
| 1775 | other_nets[k]["graph"] = conections[k]["graph"] |
| 1776 | ifaces_list.append( (k, None) ) |
| 1777 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1778 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1779 | if con_type == "external_network": |
| 1780 | other_nets[k]['external'] = True |
| 1781 | if conections[k].get("model"): |
| 1782 | other_nets[k]["model"] = conections[k]["model"] |
| 1783 | else: |
| 1784 | other_nets[k]["model"] = k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1785 | if con_type == "dataplane_net" or con_type == "bridge_net": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1786 | other_nets[k]["model"] = con_type |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1787 | |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1788 | conections_list_name.append(k) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1789 | 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) |
| 1790 | #print set(ifaces_list) |
| 1791 | #check valid VNF and iface names |
| 1792 | for iface in ifaces_list: |
| 1793 | if iface[0] not in vnfs and iface[0] not in other_nets : |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1794 | raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format( |
| 1795 | str(k), iface[0]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1796 | 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] | 1797 | raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format( |
| 1798 | str(k), iface[0], iface[1]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1799 | |
| 1800 | #1.5 unify connections from the pair list to a consolidated list |
| 1801 | index=0 |
| 1802 | while index < len(conections_list): |
| 1803 | index2 = index+1 |
| 1804 | while index2 < len(conections_list): |
| 1805 | if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets |
| 1806 | conections_list[index] |= conections_list[index2] |
| 1807 | del conections_list[index2] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1808 | del conections_list_name[index2] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1809 | else: |
| 1810 | index2 += 1 |
| 1811 | conections_list[index] = list(conections_list[index]) # from set to list again |
| 1812 | index += 1 |
| 1813 | #for k in conections_list: |
| 1814 | # print k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1815 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1816 | |
| 1817 | |
| 1818 | #1.6 Delete non external nets |
| 1819 | # for k in other_nets.keys(): |
| 1820 | # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net': |
| 1821 | # for con in conections_list: |
| 1822 | # delete_indexes=[] |
| 1823 | # for index in range(0,len(con)): |
| 1824 | # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower |
| 1825 | # for index in delete_indexes: |
| 1826 | # del con[index] |
| 1827 | # del other_nets[k] |
| 1828 | #1.7: Check external_ports are present at database table datacenter_nets |
| 1829 | for k,net in other_nets.items(): |
| 1830 | error_pos = "'topology':'nodes':'" + k + "'" |
| 1831 | if net['external']==False: |
| 1832 | if 'name' not in net: |
| 1833 | net['name']=k |
| 1834 | if 'model' not in net: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1835 | raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1836 | if net['model']=='bridge_net': |
| 1837 | net['type']='bridge'; |
| 1838 | elif net['model']=='dataplane_net': |
| 1839 | net['type']='data'; |
| 1840 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1841 | raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1842 | else: #external |
| 1843 | #IF we do not want to check that external network exist at datacenter |
| 1844 | pass |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1845 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1846 | # error_text = "" |
| 1847 | # WHERE_={} |
| 1848 | # if 'net_id' in net: |
| 1849 | # error_text += " 'net_id' " + net['net_id'] |
| 1850 | # WHERE_['uuid'] = net['net_id'] |
| 1851 | # if 'model' in net: |
| 1852 | # error_text += " 'model' " + net['model'] |
| 1853 | # WHERE_['name'] = net['model'] |
| 1854 | # if len(WHERE_) == 0: |
| 1855 | # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos |
| 1856 | # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'), |
| 1857 | # FROM='datacenter_nets', WHERE=WHERE_ ) |
| 1858 | # if r<0: |
| 1859 | # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db |
| 1860 | # elif r==0: |
| 1861 | # print "nfvo.new_scenario Error" +error_text+ " is not present at database" |
| 1862 | # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos |
| 1863 | # elif r>1: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1864 | # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database" |
| 1865 | # 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] | 1866 | # other_nets[k].update(net_db[0]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1867 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1868 | net_list={} |
| 1869 | net_nb=0 #Number of nets |
| 1870 | for con in conections_list: |
| 1871 | #check if this is connected to a external net |
| 1872 | other_net_index=-1 |
| 1873 | #print |
| 1874 | #print "con", con |
| 1875 | for index in range(0,len(con)): |
| 1876 | #check if this is connected to a external net |
| 1877 | for net_key in other_nets.keys(): |
| 1878 | if con[index][0]==net_key: |
| 1879 | if other_net_index>=0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1880 | 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] | 1881 | #print "nfvo.new_scenario " + error_text |
| 1882 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1883 | else: |
| 1884 | other_net_index = index |
| 1885 | net_target = net_key |
| 1886 | break |
| 1887 | #print "other_net_index", other_net_index |
| 1888 | try: |
| 1889 | if other_net_index>=0: |
| 1890 | del con[other_net_index] |
| 1891 | #IF we do not want to check that external network exist at datacenter |
| 1892 | if other_nets[net_target]['external'] : |
| 1893 | if "name" not in other_nets[net_target]: |
| 1894 | other_nets[net_target]['name'] = other_nets[net_target]['model'] |
| 1895 | if other_nets[net_target]["type"] == "external_network": |
| 1896 | if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data": |
| 1897 | other_nets[net_target]["type"] = "data" |
| 1898 | else: |
| 1899 | other_nets[net_target]["type"] = "bridge" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1900 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1901 | # if other_nets[net_target]['external'] : |
| 1902 | # 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 |
| 1903 | # if type_=='data' and other_nets[net_target]['type']=="ptp": |
| 1904 | # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target) |
| 1905 | # print "nfvo.new_scenario " + error_text |
| 1906 | # return -HTTP_Bad_Request, error_text |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1907 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1908 | for iface in con: |
| 1909 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 1910 | else: |
| 1911 | #create a net |
| 1912 | net_type_bridge=False |
| 1913 | net_type_data=False |
| 1914 | net_target = "__-__net"+str(net_nb) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1915 | 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] | 1916 | 'description':"net-%s in scenario %s" %(net_nb,topo['name']), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1917 | 'external':False} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1918 | for iface in con: |
| 1919 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 1920 | iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type'] |
| 1921 | if iface_type=='mgmt' or iface_type=='bridge': |
| 1922 | net_type_bridge = True |
| 1923 | else: |
| 1924 | net_type_data = True |
| 1925 | if net_type_bridge and net_type_data: |
| 1926 | 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] | 1927 | #print "nfvo.new_scenario " + error_text |
| 1928 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1929 | elif net_type_bridge: |
| 1930 | type_='bridge' |
| 1931 | else: |
| 1932 | type_='data' if len(con)>2 else 'ptp' |
| 1933 | net_list[net_target]['type'] = type_ |
| 1934 | net_nb+=1 |
| 1935 | except Exception: |
| 1936 | 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] | 1937 | #print "nfvo.new_scenario " + error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1938 | #raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1939 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1940 | |
| 1941 | #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] | 1942 | #1.8.1 obtain management net |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1943 | mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1944 | FROM='datacenter_nets', WHERE={'name':'mgmt'} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1945 | #1.8.2 check all interfaces from all vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1946 | if len(mgmt_net)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1947 | add_mgmt_net = False |
| 1948 | for vnf in vnfs.values(): |
| 1949 | for iface in vnf['ifaces'].values(): |
| 1950 | if iface['type']=='mgmt' and 'net_key' not in iface: |
| 1951 | #iface not connected |
| 1952 | iface['net_key'] = 'mgmt' |
| 1953 | add_mgmt_net = True |
| 1954 | if add_mgmt_net and 'mgmt' not in net_list: |
| 1955 | net_list['mgmt']=mgmt_net[0] |
| 1956 | net_list['mgmt']['external']=True |
| 1957 | net_list['mgmt']['graph']={'visible':False} |
| 1958 | |
| 1959 | net_list.update(other_nets) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1960 | #print |
| 1961 | #print 'net_list', net_list |
| 1962 | #print |
| 1963 | #print 'vnfs', vnfs |
| 1964 | #print |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1965 | |
| 1966 | #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1967 | c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list, |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1968 | 'tenant_id':tenant_id, 'name':topo['name'], |
| 1969 | 'description':topo.get('description',topo['name']), |
| 1970 | 'public': topo.get('public', False) |
| 1971 | }) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1972 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1973 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1974 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1975 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1976 | def new_scenario_v02(mydb, tenant_id, scenario_dict, version): |
| 1977 | """ This creates a new scenario for version 0.2 and 0.3""" |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1978 | scenario = scenario_dict["scenario"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1979 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1980 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1981 | if "tenant_id" in scenario: |
| 1982 | if scenario["tenant_id"] != tenant_id: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1983 | # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1984 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format( |
| 1985 | scenario["tenant_id"], tenant_id), HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1986 | else: |
| 1987 | tenant_id=None |
| 1988 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1989 | # 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] | 1990 | for name,vnf in scenario["vnfs"].iteritems(): |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 1991 | where = {"OR": {"tenant_id": tenant_id, 'public': "true"}} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1992 | error_text = "" |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 1993 | error_pos = "'scenario':'vnfs':'" + name + "'" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1994 | if 'vnf_id' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1995 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1996 | where['uuid'] = vnf['vnf_id'] |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1997 | if 'vnf_name' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1998 | error_text += " 'vnf_name' " + vnf['vnf_name'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1999 | where['name'] = vnf['vnf_name'] |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2000 | if len(where) == 1: |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 2001 | 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] | 2002 | vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'), |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 2003 | FROM='vnfs', |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 2004 | WHERE=where) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2005 | if len(vnf_db) == 0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2006 | raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2007 | elif len(vnf_db) > 1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2008 | 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] | 2009 | vnf['uuid'] = vnf_db[0]['uuid'] |
| 2010 | vnf['description'] = vnf_db[0]['description'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2011 | vnf['ifaces'] = {} |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2012 | # get external interfaces |
| 2013 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'), |
| 2014 | 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] | 2015 | WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2016 | for ext_iface in ext_ifaces: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2017 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']} |
| 2018 | # 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] | 2019 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2020 | # 2: Insert net_key and ip_address at every vnf interface |
| 2021 | for net_name, net in scenario["networks"].items(): |
| 2022 | net_type_bridge = False |
| 2023 | net_type_data = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2024 | for iface_dict in net["interfaces"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2025 | if version == "0.2": |
| 2026 | temp_dict = iface_dict |
| 2027 | ip_address = None |
| 2028 | elif version == "0.3": |
| 2029 | temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]} |
| 2030 | ip_address = iface_dict.get('ip_address', None) |
| 2031 | for vnf, iface in temp_dict.items(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2032 | if vnf not in scenario["vnfs"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2033 | error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format( |
| 2034 | net_name, vnf) |
| 2035 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2036 | raise NfvoException(error_text, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2037 | if iface not in scenario["vnfs"][vnf]['ifaces']: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2038 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\ |
| 2039 | .format(net_name, iface) |
| 2040 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2041 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2042 | if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2043 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\ |
| 2044 | "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key']) |
| 2045 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2046 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2047 | scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2048 | scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2049 | iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type'] |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2050 | if iface_type == 'mgmt' or iface_type == 'bridge': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2051 | net_type_bridge = True |
| 2052 | else: |
| 2053 | net_type_data = True |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2054 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2055 | if net_type_bridge and net_type_data: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2056 | error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\ |
| 2057 | .format(net_name) |
| 2058 | # logger.debug("nfvo.new_scenario " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2059 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2060 | elif net_type_bridge: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2061 | type_ = 'bridge' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2062 | else: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2063 | type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp' |
| 2064 | |
| 2065 | if net.get("implementation"): # for v0.3 |
| 2066 | if type_ == "bridge" and net["implementation"] == "underlay": |
| 2067 | error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\ |
| 2068 | "'network':'{}'".format(net_name) |
| 2069 | # logger.debug(error_text) |
| 2070 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2071 | elif type_ != "bridge" and net["implementation"] == "overlay": |
| 2072 | error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\ |
| 2073 | "'network':'{}'".format(net_name) |
| 2074 | # logger.debug(error_text) |
| 2075 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2076 | net.pop("implementation") |
| 2077 | if "type" in net and version == "0.3": # for v0.3 |
| 2078 | if type_ == "data" and net["type"] == "e-line": |
| 2079 | error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\ |
| 2080 | "'e-line' at 'network':'{}'".format(net_name) |
| 2081 | # logger.debug(error_text) |
| 2082 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 2083 | elif type_ == "ptp" and net["type"] == "e-lan": |
| 2084 | type_ = "data" |
| 2085 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2086 | net['type'] = type_ |
| 2087 | net['name'] = net_name |
| 2088 | net['external'] = net.get('external', False) |
| 2089 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2090 | # 3: insert at database |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2091 | scenario["nets"] = scenario["networks"] |
| 2092 | scenario['tenant_id'] = tenant_id |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 2093 | scenario_id = mydb.new_scenario(scenario) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2094 | return scenario_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2095 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2096 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2097 | def new_nsd_v3(mydb, tenant_id, nsd_descriptor): |
| 2098 | """ |
| 2099 | Parses an OSM IM nsd_catalog and insert at DB |
| 2100 | :param mydb: |
| 2101 | :param tenant_id: |
| 2102 | :param nsd_descriptor: |
| 2103 | :return: The list of cretated NSD ids |
| 2104 | """ |
| 2105 | try: |
| 2106 | mynsd = nsd_catalog.nsd() |
| tierno | a955020 | 2017-09-22 13:31:35 +0200 | [diff] [blame] | 2107 | try: |
| 2108 | pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd) |
| 2109 | except Exception as e: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2110 | raise NfvoException("Error. Invalid NS descriptor format: " + str(e), HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2111 | db_scenarios = [] |
| 2112 | db_sce_nets = [] |
| 2113 | db_sce_vnfs = [] |
| 2114 | db_sce_interfaces = [] |
| 2115 | db_ip_profiles = [] |
| 2116 | db_ip_profiles_index = 0 |
| 2117 | uuid_list = [] |
| 2118 | nsd_uuid_list = [] |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2119 | for nsd_yang in mynsd.nsd_catalog.nsd.itervalues(): |
| 2120 | nsd = nsd_yang.get() |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2121 | |
| 2122 | # table sceanrios |
| 2123 | scenario_uuid = str(uuid4()) |
| 2124 | uuid_list.append(scenario_uuid) |
| 2125 | nsd_uuid_list.append(scenario_uuid) |
| 2126 | db_scenario = { |
| 2127 | "uuid": scenario_uuid, |
| 2128 | "osm_id": get_str(nsd, "id", 255), |
| 2129 | "name": get_str(nsd, "name", 255), |
| 2130 | "description": get_str(nsd, "description", 255), |
| 2131 | "tenant_id": tenant_id, |
| 2132 | "vendor": get_str(nsd, "vendor", 255), |
| 2133 | "short_name": get_str(nsd, "short-name", 255), |
| 2134 | "descriptor": str(nsd_descriptor)[:60000], |
| 2135 | } |
| 2136 | db_scenarios.append(db_scenario) |
| 2137 | |
| 2138 | # table sce_vnfs (constituent-vnfd) |
| 2139 | vnf_index2scevnf_uuid = {} |
| 2140 | vnf_index2vnf_uuid = {} |
| 2141 | for vnf in nsd.get("constituent-vnfd").itervalues(): |
| 2142 | existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255], |
| 2143 | 'tenant_id': tenant_id}) |
| 2144 | if not existing_vnf: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2145 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':" |
| 2146 | "'{}'. Reference to a non-existing VNFD in the catalog".format( |
| 2147 | str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]), |
| 2148 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2149 | sce_vnf_uuid = str(uuid4()) |
| 2150 | uuid_list.append(sce_vnf_uuid) |
| 2151 | db_sce_vnf = { |
| 2152 | "uuid": sce_vnf_uuid, |
| 2153 | "scenario_id": scenario_uuid, |
| 2154 | "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 5), |
| 2155 | "vnf_id": existing_vnf[0]["uuid"], |
| 2156 | "member_vnf_index": int(vnf["member-vnf-index"]), |
| 2157 | # TODO 'start-by-default': True |
| 2158 | } |
| 2159 | vnf_index2scevnf_uuid[int(vnf['member-vnf-index'])] = sce_vnf_uuid |
| 2160 | vnf_index2vnf_uuid[int(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"] |
| 2161 | db_sce_vnfs.append(db_sce_vnf) |
| 2162 | |
| 2163 | # table ip_profiles (ip-profiles) |
| 2164 | ip_profile_name2db_table_index = {} |
| 2165 | for ip_profile in nsd.get("ip-profiles").itervalues(): |
| 2166 | db_ip_profile = { |
| 2167 | "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")), |
| 2168 | "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")), |
| 2169 | "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")), |
| 2170 | "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)), |
| 2171 | "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")), |
| 2172 | "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")), |
| 2173 | } |
| 2174 | dns_list = [] |
| 2175 | for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues(): |
| 2176 | dns_list.append(str(dns.get("address"))) |
| 2177 | db_ip_profile["dns_address"] = ";".join(dns_list) |
| 2178 | if ip_profile["ip-profile-params"].get('security-group'): |
| 2179 | db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group'] |
| 2180 | ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index |
| 2181 | db_ip_profiles_index += 1 |
| 2182 | db_ip_profiles.append(db_ip_profile) |
| 2183 | |
| 2184 | # table sce_nets (internal-vld) |
| 2185 | for vld in nsd.get("vld").itervalues(): |
| 2186 | sce_net_uuid = str(uuid4()) |
| 2187 | uuid_list.append(sce_net_uuid) |
| 2188 | db_sce_net = { |
| 2189 | "uuid": sce_net_uuid, |
| 2190 | "name": get_str(vld, "name", 255), |
| 2191 | "scenario_id": scenario_uuid, |
| 2192 | # "type": #TODO |
| 2193 | "multipoint": not vld.get("type") == "ELINE", |
| 2194 | # "external": #TODO |
| 2195 | "description": get_str(vld, "description", 255), |
| 2196 | } |
| 2197 | # guess type of network |
| 2198 | if vld.get("mgmt-network"): |
| 2199 | db_sce_net["type"] = "bridge" |
| 2200 | db_sce_net["external"] = True |
| 2201 | elif vld.get("provider-network").get("overlay-type") == "VLAN": |
| 2202 | db_sce_net["type"] = "data" |
| 2203 | else: |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2204 | # later on it will be fixed to bridge or data depending on the type of interfaces attached to it |
| 2205 | db_sce_net["type"] = None |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2206 | db_sce_nets.append(db_sce_net) |
| 2207 | |
| 2208 | # ip-profile, link db_ip_profile with db_sce_net |
| 2209 | if vld.get("ip-profile-ref"): |
| 2210 | ip_profile_name = vld.get("ip-profile-ref") |
| 2211 | if ip_profile_name not in ip_profile_name2db_table_index: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2212 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'." |
| 2213 | " Reference to a non-existing 'ip_profiles'".format( |
| 2214 | str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])), |
| 2215 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2216 | db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid |
| 2217 | |
| 2218 | # table sce_interfaces (vld:vnfd-connection-point-ref) |
| 2219 | for iface in vld.get("vnfd-connection-point-ref").itervalues(): |
| 2220 | vnf_index = int(iface['member-vnf-index-ref']) |
| 2221 | # check correct parameters |
| 2222 | if vnf_index not in vnf_index2vnf_uuid: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2223 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2224 | "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at " |
| 2225 | "'nsd':'constituent-vnfd'".format( |
| 2226 | str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])), |
| 2227 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2228 | |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2229 | 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] | 2230 | FROM="interfaces as i join vms on i.vm_id=vms.uuid", |
| 2231 | WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], |
| 2232 | 'external_name': get_str(iface, "vnfd-connection-point-ref", |
| 2233 | 255)}) |
| 2234 | if not existing_ifaces: |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2235 | raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point" |
| 2236 | "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing " |
| 2237 | "connection-point name at VNFD '{}'".format( |
| 2238 | str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]), |
| 2239 | str(iface.get("vnfd-id-ref"))[:255]), |
| 2240 | HTTP_Bad_Request) |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2241 | interface_uuid = existing_ifaces[0]["uuid"] |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2242 | if existing_ifaces[0]["iface_type"] == "data" and not db_sce_net["type"]: |
| 2243 | db_sce_net["type"] = "data" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2244 | sce_interface_uuid = str(uuid4()) |
| 2245 | uuid_list.append(sce_net_uuid) |
| 2246 | db_sce_interface = { |
| 2247 | "uuid": sce_interface_uuid, |
| 2248 | "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index], |
| 2249 | "sce_net_id": sce_net_uuid, |
| 2250 | "interface_id": interface_uuid, |
| 2251 | # "ip_address": #TODO |
| 2252 | } |
| 2253 | db_sce_interfaces.append(db_sce_interface) |
| tierno | 66eba6e | 2017-11-10 17:09:18 +0100 | [diff] [blame] | 2254 | if not db_sce_net["type"]: |
| 2255 | db_sce_net["type"] = "bridge" |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2256 | |
| 2257 | db_tables = [ |
| 2258 | {"scenarios": db_scenarios}, |
| 2259 | {"sce_nets": db_sce_nets}, |
| 2260 | {"ip_profiles": db_ip_profiles}, |
| 2261 | {"sce_vnfs": db_sce_vnfs}, |
| 2262 | {"sce_interfaces": db_sce_interfaces}, |
| 2263 | ] |
| 2264 | |
| 2265 | logger.debug("create_vnf Deployment done vnfDict: %s", |
| 2266 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 2267 | mydb.new_rows(db_tables, uuid_list) |
| 2268 | return nsd_uuid_list |
| tierno | b2880eb | 2017-10-04 15:04:53 +0200 | [diff] [blame] | 2269 | except NfvoException: |
| 2270 | raise |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2271 | except Exception as e: |
| 2272 | logger.error("Exception {}".format(e)) |
| 2273 | raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request) |
| 2274 | |
| 2275 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2276 | def edit_scenario(mydb, tenant_id, scenario_id, data): |
| 2277 | data["uuid"] = scenario_id |
| 2278 | data["tenant_id"] = tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2279 | c = mydb.edit_scenario( data ) |
| 2280 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2281 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2282 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2283 | 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] | 2284 | #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] | 2285 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant) |
| 2286 | vims = {datacenter_id: myvim} |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 2287 | myvim_tenant = myvim['tenant_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2288 | datacenter_name = myvim['name'] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2289 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2290 | rollbackList=[] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2291 | try: |
| 2292 | #print "Checking that the scenario_id exists and getting the scenario dictionary" |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2293 | scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2294 | scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] } |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2295 | scenarioDict['datacenter_id'] = datacenter_id |
| 2296 | #print '================scenarioDict=======================' |
| 2297 | #print json.dumps(scenarioDict, indent=4) |
| 2298 | #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] | 2299 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2300 | logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs'])) |
| 2301 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2302 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2303 | auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id |
| 2304 | auxNetDict['scenario'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2305 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2306 | logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM") |
| 2307 | for sce_net in scenarioDict['nets']: |
| 2308 | #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2309 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2310 | myNetName = "%s.%s" % (instance_scenario_name, sce_net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2311 | myNetName = myNetName[0:255] #limit length |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2312 | myNetType = sce_net['type'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2313 | myNetDict = {} |
| 2314 | myNetDict["name"] = myNetName |
| 2315 | myNetDict["type"] = myNetType |
| 2316 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2317 | myNetIPProfile = sce_net.get('ip_profile', None) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2318 | #TODO: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2319 | #We should use the dictionary as input parameter for new_network |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2320 | #print myNetDict |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2321 | if not sce_net["external"]: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2322 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2323 | #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id) |
| 2324 | sce_net['vim_id'] = network_id |
| 2325 | auxNetDict['scenario'][sce_net['uuid']] = network_id |
| 2326 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2327 | sce_net["created"] = True |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2328 | else: |
| 2329 | if sce_net['vim_id'] == None: |
| 2330 | error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name']) |
| 2331 | _, message = rollback(mydb, vims, rollbackList) |
| 2332 | logger.error("nfvo.start_scenario: %s", error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2333 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2334 | logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id']) |
| 2335 | auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2336 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2337 | logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM") |
| 2338 | #For each vnf net, we create it and we add it to instanceNetlist. |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2339 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2340 | for sce_vnf in scenarioDict['vnfs']: |
| 2341 | for net in sce_vnf['nets']: |
| 2342 | #print "Net name: %s. Description: %s" % (net["name"], net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2343 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2344 | myNetName = "%s.%s" % (instance_scenario_name,net['name']) |
| 2345 | myNetName = myNetName[0:255] #limit length |
| 2346 | myNetType = net['type'] |
| 2347 | myNetDict = {} |
| 2348 | myNetDict["name"] = myNetName |
| 2349 | myNetDict["type"] = myNetType |
| 2350 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2351 | myNetIPProfile = net.get('ip_profile', None) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2352 | #print myNetDict |
| 2353 | #TODO: |
| 2354 | #We should use the dictionary as input parameter for new_network |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2355 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2356 | #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id) |
| 2357 | net['vim_id'] = network_id |
| 2358 | if sce_vnf['uuid'] not in auxNetDict: |
| 2359 | auxNetDict[sce_vnf['uuid']] = {} |
| 2360 | auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id |
| 2361 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2362 | net["created"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2363 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2364 | #print "auxNetDict:" |
| 2365 | #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2366 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2367 | logger.debug("start_scenario 3. Creating new vm instances in the VIM") |
| 2368 | #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 2369 | i = 0 |
| 2370 | for sce_vnf in scenarioDict['vnfs']: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2371 | vnf_availability_zones = [] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2372 | for vm in sce_vnf['vms']: |
| 2373 | vm_av = vm.get('availability_zone') |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2374 | if vm_av and vm_av not in vnf_availability_zones: |
| 2375 | vnf_availability_zones.append(vm_av) |
| 2376 | |
| 2377 | # check if there is enough availability zones available at vim level. |
| 2378 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 2379 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| 2380 | raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request) |
| 2381 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2382 | for vm in sce_vnf['vms']: |
| 2383 | i += 1 |
| 2384 | myVMDict = {} |
| 2385 | #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name']) |
| tierno | ae65a48 | 2016-11-24 16:20:05 +0100 | [diff] [blame] | 2386 | myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2387 | #myVMDict['description'] = vm['description'] |
| 2388 | myVMDict['description'] = myVMDict['name'][0:99] |
| 2389 | if not startvms: |
| 2390 | myVMDict['start'] = "no" |
| 2391 | myVMDict['name'] = myVMDict['name'][0:255] #limit name length |
| 2392 | #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2393 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2394 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2395 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2396 | image_id = create_or_use_image(mydb, vims, image_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2397 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2398 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2399 | #create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2400 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2401 | if flavor_dict['extended']!=None: |
| 2402 | flavor_dict['extended']= yaml.load(flavor_dict['extended']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2403 | flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2404 | vm['vim_flavor_id'] = flavor_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2405 | |
| 2406 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2407 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 2408 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 2409 | myVMDict['networks'] = [] |
| 2410 | for iface in vm['interfaces']: |
| 2411 | netDict = {} |
| 2412 | if iface['type']=="data": |
| 2413 | netDict['type'] = iface['model'] |
| 2414 | elif "model" in iface and iface["model"]!=None: |
| 2415 | netDict['model']=iface['model'] |
| 2416 | #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model |
| 2417 | #discover type of interface looking at flavor |
| 2418 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 2419 | for flavor_iface in numa.get('interfaces',[]): |
| 2420 | if flavor_iface.get('name') == iface['internal_name']: |
| 2421 | if flavor_iface['dedicated'] == 'yes': |
| 2422 | netDict['type']="PF" #passthrough |
| 2423 | elif flavor_iface['dedicated'] == 'no': |
| 2424 | netDict['type']="VF" #siov |
| 2425 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 2426 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 2427 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 2428 | break; |
| 2429 | netDict["use"]=iface['type'] |
| 2430 | if netDict["use"]=="data" and not netDict.get("type"): |
| 2431 | #print "netDict", netDict |
| 2432 | #print "iface", iface |
| 2433 | 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']) |
| 2434 | if flavor_dict.get('extended')==None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2435 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 2436 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2437 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2438 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2439 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 2440 | netDict["type"]="virtual" |
| 2441 | if "vpci" in iface and iface["vpci"] is not None: |
| 2442 | netDict['vpci'] = iface['vpci'] |
| 2443 | if "mac" in iface and iface["mac"] is not None: |
| 2444 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 2445 | if "port-security" in iface and iface["port-security"] is not None: |
| 2446 | netDict['port_security'] = iface['port-security'] |
| 2447 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 2448 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2449 | netDict['name'] = iface['internal_name'] |
| 2450 | if iface['net_id'] is None: |
| 2451 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2452 | #print iface |
| 2453 | #print vnf_iface |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2454 | if vnf_iface['interface_id']==iface['uuid']: |
| 2455 | netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ] |
| 2456 | break |
| 2457 | else: |
| 2458 | netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| 2459 | #skip bridge ifaces not connected to any net |
| 2460 | #if 'net_id' not in netDict or netDict['net_id']==None: |
| 2461 | # continue |
| 2462 | myVMDict['networks'].append(netDict) |
| 2463 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 2464 | #print myVMDict['name'] |
| 2465 | #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 2466 | #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 2467 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2468 | |
| 2469 | if 'availability_zone' in myVMDict: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2470 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2471 | else: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2472 | av_index = None |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2473 | |
| tierno | 98e909c | 2017-10-14 13:27:03 +0200 | [diff] [blame] | 2474 | vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None), |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2475 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 2476 | availability_zone_index=av_index, |
| 2477 | availability_zone_list=vnf_availability_zones) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2478 | #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id) |
| 2479 | vm['vim_id'] = vm_id |
| 2480 | rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id}) |
| 2481 | #put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 2482 | for net in myVMDict['networks']: |
| 2483 | if "vim_id" in net: |
| 2484 | for iface in vm['interfaces']: |
| 2485 | if net["name"]==iface["internal_name"]: |
| 2486 | iface["vim_id"]=net["vim_id"] |
| 2487 | break |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2488 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2489 | logger.debug("start scenario Deployment done") |
| 2490 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| 2491 | #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] | 2492 | instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict) |
| 2493 | return mydb.get_instance_scenario(instance_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2494 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2495 | except (db_base_Exception, vimconn.vimconnException) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2496 | _, message = rollback(mydb, vims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2497 | if isinstance(e, db_base_Exception): |
| 2498 | error_text = "Exception at database" |
| 2499 | else: |
| 2500 | error_text = "Exception at VIM" |
| 2501 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 2502 | #logger.error("start_scenario %s", error_text) |
| 2503 | raise NfvoException(error_text, e.http_code) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2504 | |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2505 | def unify_cloud_config(cloud_config_preserve, cloud_config): |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2506 | """ join the cloud config information into cloud_config_preserve. |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2507 | In case of conflict cloud_config_preserve preserves |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2508 | None is allowed |
| 2509 | """ |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2510 | if not cloud_config_preserve and not cloud_config: |
| 2511 | return None |
| 2512 | |
| 2513 | new_cloud_config = {"key-pairs":[], "users":[]} |
| 2514 | # key-pairs |
| 2515 | if cloud_config_preserve: |
| 2516 | for key in cloud_config_preserve.get("key-pairs", () ): |
| 2517 | if key not in new_cloud_config["key-pairs"]: |
| 2518 | new_cloud_config["key-pairs"].append(key) |
| 2519 | if cloud_config: |
| 2520 | for key in cloud_config.get("key-pairs", () ): |
| 2521 | if key not in new_cloud_config["key-pairs"]: |
| 2522 | new_cloud_config["key-pairs"].append(key) |
| 2523 | if not new_cloud_config["key-pairs"]: |
| 2524 | del new_cloud_config["key-pairs"] |
| 2525 | |
| 2526 | # users |
| 2527 | if cloud_config: |
| 2528 | new_cloud_config["users"] += cloud_config.get("users", () ) |
| 2529 | if cloud_config_preserve: |
| 2530 | new_cloud_config["users"] += cloud_config_preserve.get("users", () ) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2531 | index_to_delete = [] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2532 | users = new_cloud_config.get("users", []) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2533 | for index0 in range(0,len(users)): |
| 2534 | if index0 in index_to_delete: |
| 2535 | continue |
| 2536 | for index1 in range(index0+1,len(users)): |
| 2537 | if index1 in index_to_delete: |
| 2538 | continue |
| 2539 | if users[index0]["name"] == users[index1]["name"]: |
| 2540 | index_to_delete.append(index1) |
| 2541 | for key in users[index1].get("key-pairs",()): |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2542 | if "key-pairs" not in users[index0]: |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2543 | users[index0]["key-pairs"] = [key] |
| 2544 | elif key not in users[index0]["key-pairs"]: |
| 2545 | users[index0]["key-pairs"].append(key) |
| 2546 | index_to_delete.sort(reverse=True) |
| 2547 | for index in index_to_delete: |
| 2548 | del users[index] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2549 | if not new_cloud_config["users"]: |
| 2550 | del new_cloud_config["users"] |
| 2551 | |
| 2552 | #boot-data-drive |
| 2553 | if cloud_config and cloud_config.get("boot-data-drive") != None: |
| 2554 | new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"] |
| 2555 | if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None: |
| 2556 | new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"] |
| 2557 | |
| 2558 | # user-data |
| tierno | 40e1bce | 2017-08-09 09:12:04 +0200 | [diff] [blame] | 2559 | new_cloud_config["user-data"] = [] |
| 2560 | if cloud_config and cloud_config.get("user-data"): |
| 2561 | if isinstance(cloud_config["user-data"], list): |
| 2562 | new_cloud_config["user-data"] += cloud_config["user-data"] |
| 2563 | else: |
| 2564 | new_cloud_config["user-data"].append(cloud_config["user-data"]) |
| 2565 | if cloud_config_preserve and cloud_config_preserve.get("user-data"): |
| 2566 | if isinstance(cloud_config_preserve["user-data"], list): |
| 2567 | new_cloud_config["user-data"] += cloud_config_preserve["user-data"] |
| 2568 | else: |
| 2569 | new_cloud_config["user-data"].append(cloud_config_preserve["user-data"]) |
| 2570 | if not new_cloud_config["user-data"]: |
| 2571 | del new_cloud_config["user-data"] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2572 | |
| 2573 | # config files |
| 2574 | new_cloud_config["config-files"] = [] |
| 2575 | if cloud_config and cloud_config.get("config-files") != None: |
| 2576 | new_cloud_config["config-files"] += cloud_config["config-files"] |
| 2577 | if cloud_config_preserve: |
| 2578 | for file in cloud_config_preserve.get("config-files", ()): |
| 2579 | for index in range(0, len(new_cloud_config["config-files"])): |
| 2580 | if new_cloud_config["config-files"][index]["dest"] == file["dest"]: |
| 2581 | new_cloud_config["config-files"][index] = file |
| 2582 | break |
| 2583 | else: |
| 2584 | new_cloud_config["config-files"].append(file) |
| 2585 | if not new_cloud_config["config-files"]: |
| 2586 | del new_cloud_config["config-files"] |
| 2587 | return new_cloud_config |
| 2588 | |
| 2589 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2590 | 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] | 2591 | datacenter_id = None |
| 2592 | datacenter_name = None |
| 2593 | thread = None |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2594 | try: |
| 2595 | if datacenter_tenant_id: |
| 2596 | thread_id = datacenter_tenant_id |
| 2597 | thread = vim_threads["running"].get(datacenter_tenant_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2598 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2599 | where_={"td.nfvo_tenant_id": tenant_id} |
| 2600 | if datacenter_id_name: |
| 2601 | if utils.check_valid_uuid(datacenter_id_name): |
| 2602 | datacenter_id = datacenter_id_name |
| 2603 | where_["dt.datacenter_id"] = datacenter_id |
| 2604 | else: |
| 2605 | datacenter_name = datacenter_id_name |
| 2606 | where_["d.name"] = datacenter_name |
| 2607 | if datacenter_tenant_id: |
| 2608 | where_["dt.uuid"] = datacenter_tenant_id |
| 2609 | datacenters = mydb.get_rows( |
| 2610 | SELECT=("dt.uuid as datacenter_tenant_id",), |
| 2611 | FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id " |
| 2612 | "join datacenters as d on d.uuid=dt.datacenter_id", |
| 2613 | WHERE=where_) |
| 2614 | if len(datacenters) > 1: |
| 2615 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 2616 | elif datacenters: |
| 2617 | thread_id = datacenters[0]["datacenter_tenant_id"] |
| 2618 | thread = vim_threads["running"].get(thread_id) |
| 2619 | if not thread: |
| 2620 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2621 | return thread_id, thread |
| 2622 | except db_base_Exception as e: |
| 2623 | raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2624 | |
| tierno | f575596 | 2017-07-13 15:44:34 +0200 | [diff] [blame] | 2625 | |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2626 | def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name): |
| 2627 | WHERE_dict={} |
| 2628 | if utils.check_valid_uuid(datacenter_id_name): |
| 2629 | WHERE_dict['d.uuid'] = datacenter_id_name |
| 2630 | else: |
| 2631 | WHERE_dict['d.name'] = datacenter_id_name |
| 2632 | |
| 2633 | if tenant_id: |
| 2634 | WHERE_dict['nfvo_tenant_id'] = tenant_id |
| 2635 | from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \ |
| 2636 | " dt on td.datacenter_tenant_id=dt.uuid" |
| 2637 | else: |
| 2638 | from_ = 'datacenters as d' |
| 2639 | vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid",), WHERE=WHERE_dict ) |
| 2640 | if len(vimaccounts) == 0: |
| 2641 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2642 | elif len(vimaccounts)>1: |
| 2643 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 2644 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 2645 | return vimaccounts[0]["uuid"] |
| 2646 | |
| 2647 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2648 | 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] | 2649 | datacenter_id = None |
| 2650 | datacenter_name = None |
| 2651 | if datacenter_id_name: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2652 | if utils.check_valid_uuid(datacenter_id_name): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2653 | datacenter_id = datacenter_id_name |
| 2654 | else: |
| 2655 | datacenter_name = datacenter_id_name |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2656 | vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2657 | if len(vims) == 0: |
| 2658 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 2659 | elif len(vims)>1: |
| 2660 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 2661 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 2662 | return vims.keys()[0], vims.values()[0] |
| 2663 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2664 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2665 | def update(d, u): |
| 2666 | '''Takes dict d and updates it with the values in dict u.''' |
| 2667 | '''It merges all depth levels''' |
| 2668 | for k, v in u.iteritems(): |
| 2669 | if isinstance(v, collections.Mapping): |
| 2670 | r = update(d.get(k, {}), v) |
| 2671 | d[k] = r |
| 2672 | else: |
| 2673 | d[k] = u[k] |
| 2674 | return d |
| 2675 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2676 | def create_instance(mydb, tenant_id, instance_dict): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2677 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| 2678 | # logger.debug("Creating instance...") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2679 | scenario = instance_dict["scenario"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2680 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2681 | # find main datacenter |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2682 | myvims = {} |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2683 | myvim_threads_id = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2684 | datacenter = instance_dict.get("datacenter") |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2685 | default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 2686 | myvims[default_datacenter_id] = vim |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2687 | 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] | 2688 | tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2689 | # myvim_tenant = myvim['tenant_id'] |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 2690 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2691 | rollbackList=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2692 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2693 | # print "Checking that the scenario exists and getting the scenario dictionary" |
| 2694 | scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id], |
| 2695 | datacenter_id=default_datacenter_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2696 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2697 | # logger.debug(">>>>>> Dictionaries before merging") |
| 2698 | # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256))) |
| 2699 | # 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] | 2700 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2701 | db_instance_vnfs = [] |
| 2702 | db_instance_vms = [] |
| 2703 | db_instance_interfaces = [] |
| 2704 | db_ip_profiles = [] |
| 2705 | db_vim_actions = [] |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2706 | uuid_list = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2707 | task_index = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2708 | instance_name = instance_dict["name"] |
| 2709 | instance_uuid = str(uuid4()) |
| 2710 | uuid_list.append(instance_uuid) |
| 2711 | db_instance_scenario = { |
| 2712 | "uuid": instance_uuid, |
| 2713 | "name": instance_name, |
| 2714 | "tenant_id": tenant_id, |
| 2715 | "scenario_id": scenarioDict['uuid'], |
| 2716 | "datacenter_id": default_datacenter_id, |
| 2717 | # filled bellow 'datacenter_tenant_id' |
| 2718 | "description": instance_dict.get("description"), |
| 2719 | } |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2720 | if scenarioDict.get("cloud-config"): |
| 2721 | db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"], |
| 2722 | default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2723 | instance_action_id = get_task_id() |
| 2724 | db_instance_action = { |
| 2725 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 2726 | "tenant_id": tenant_id, |
| 2727 | "instance_id": instance_uuid, |
| 2728 | "description": "CREATE", |
| 2729 | } |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2730 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2731 | # Auxiliary dictionaries from x to y |
| 2732 | vnf_net2instance = {} |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2733 | sce_net2instance = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2734 | net2task_id = {'scenario': {}} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2735 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2736 | # logger.debug("Creating instance from scenario-dict:\n%s", |
| 2737 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2738 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2739 | # 0 check correct parameters |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2740 | for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems(): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2741 | found = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2742 | for scenario_net in scenarioDict['nets']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2743 | if net_name == scenario_net["name"]: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2744 | found = True |
| 2745 | break |
| 2746 | if not found: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2747 | raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), |
| 2748 | HTTP_Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2749 | if "sites" not in net_instance_desc: |
| 2750 | net_instance_desc["sites"] = [ {} ] |
| 2751 | site_without_datacenter_field = False |
| 2752 | for site in net_instance_desc["sites"]: |
| 2753 | if site.get("datacenter"): |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2754 | site["datacenter"] = get_datacenter_uuid(mydb, tenant_id, site["datacenter"]) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2755 | if site["datacenter"] not in myvims: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2756 | # Add this datacenter to myvims |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2757 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"]) |
| 2758 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2759 | myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"]) |
| 2760 | site["datacenter"] = d # change name to id |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2761 | else: |
| 2762 | if site_without_datacenter_field: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2763 | raise NfvoException("Found more than one entries without datacenter field at " |
| 2764 | "instance:networks:{}:sites".format(net_name), HTTP_Bad_Request) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2765 | site_without_datacenter_field = True |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2766 | site["datacenter"] = default_datacenter_id # change name to id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2767 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2768 | for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems(): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2769 | found = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2770 | for scenario_vnf in scenarioDict['vnfs']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2771 | if vnf_name == scenario_vnf['name']: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2772 | found = True |
| 2773 | break |
| 2774 | if not found: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2775 | raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request) |
| 2776 | if "datacenter" in vnf_instance_desc: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2777 | # Add this datacenter to myvims |
| tierno | a15c4b9 | 2017-10-05 12:41:44 +0200 | [diff] [blame] | 2778 | 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] | 2779 | if vnf_instance_desc["datacenter"] not in myvims: |
| 2780 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| 2781 | myvims[d] = v |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2782 | 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] | 2783 | scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"] |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 2784 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2785 | # 0.1 parse cloud-config parameters |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2786 | cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config")) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 2787 | # We add the RO key to cloud_config |
| 2788 | if tenant[0].get('RO_pub_key'): |
| 2789 | RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]} |
| 2790 | cloud_config = unify_cloud_config(cloud_config, RO_key) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2791 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2792 | # 0.2 merge instance information into scenario |
| 2793 | # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict) |
| 2794 | # However, this is not possible yet. |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2795 | for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems(): |
| 2796 | for scenario_net in scenarioDict['nets']: |
| 2797 | if net_name == scenario_net["name"]: |
| 2798 | if 'ip-profile' in net_instance_desc: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 2799 | # translate from input format to database format |
| 2800 | ipprofile_in = net_instance_desc['ip-profile'] |
| 2801 | ipprofile_db = {} |
| 2802 | ipprofile_db['subnet_address'] = ipprofile_in.get('subnet-address') |
| 2803 | ipprofile_db['ip_version'] = ipprofile_in.get('ip-version', 'IPv4') |
| 2804 | ipprofile_db['gateway_address'] = ipprofile_in.get('gateway-address') |
| 2805 | ipprofile_db['dns_address'] = ipprofile_in.get('dns-address') |
| 2806 | if isinstance(ipprofile_db['dns_address'], (list, tuple)): |
| 2807 | ipprofile_db['dns_address'] = ";".join(ipprofile_db['dns_address']) |
| 2808 | if 'dhcp' in ipprofile_in: |
| 2809 | ipprofile_db['dhcp_start_address'] = ipprofile_in['dhcp'].get('start-address') |
| 2810 | ipprofile_db['dhcp_enabled'] = ipprofile_in['dhcp'].get('enabled', True) |
| 2811 | ipprofile_db['dhcp_count'] = ipprofile_in['dhcp'].get('count' ) |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 2812 | if 'ip_profile' not in scenario_net: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 2813 | scenario_net['ip_profile'] = ipprofile_db |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 2814 | else: |
| tierno | 455612d | 2017-05-30 16:40:10 +0200 | [diff] [blame] | 2815 | update(scenario_net['ip_profile'], ipprofile_db) |
| tierno | e6c58ce | 2016-09-14 16:02:49 +0200 | [diff] [blame] | 2816 | for interface in net_instance_desc.get('interfaces', () ): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2817 | if 'ip_address' in interface: |
| 2818 | for vnf in scenarioDict['vnfs']: |
| 2819 | if interface['vnf'] == vnf['name']: |
| 2820 | for vnf_interface in vnf['interfaces']: |
| 2821 | if interface['vnf_interface'] == vnf_interface['external_name']: |
| 2822 | vnf_interface['ip_address']=interface['ip_address'] |
| 2823 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2824 | # logger.debug(">>>>>>>> Merged dictionary") |
| 2825 | # logger.debug("Creating instance scenario-dict MERGED:\n%s", |
| 2826 | # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2827 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2828 | # 1. Creating new nets (sce_nets) in the VIM" |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2829 | db_instance_nets = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2830 | for sce_net in scenarioDict['nets']: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2831 | descriptor_net = instance_dict.get("networks", {}).get(sce_net["name"], {}) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2832 | net_name = descriptor_net.get("vim-network-name") |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2833 | sce_net2instance[sce_net['uuid']] = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2834 | net2task_id['scenario'][sce_net['uuid']] = {} |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2835 | |
| 2836 | sites = descriptor_net.get("sites", [ {} ]) |
| 2837 | for site in sites: |
| 2838 | if site.get("datacenter"): |
| 2839 | vim = myvims[ site["datacenter"] ] |
| 2840 | datacenter_id = site["datacenter"] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2841 | myvim_thread_id = myvim_threads_id[ site["datacenter"] ] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2842 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2843 | vim = myvims[ default_datacenter_id ] |
| 2844 | datacenter_id = default_datacenter_id |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2845 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2846 | net_type = sce_net['type'] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2847 | lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2848 | |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 2849 | if not net_name: |
| 2850 | if sce_net["external"]: |
| 2851 | net_name = sce_net["name"] |
| 2852 | else: |
| 2853 | net_name = "{}.{}".format(instance_name, sce_net["name"]) |
| 2854 | net_name = net_name[:255] # limit length |
| 2855 | |
| 2856 | if "netmap-use" in site or "netmap-create" in site: |
| 2857 | create_network = False |
| 2858 | lookfor_network = False |
| 2859 | if "netmap-use" in site: |
| 2860 | lookfor_network = True |
| 2861 | if utils.check_valid_uuid(site["netmap-use"]): |
| 2862 | filter_text = "scenario id '%s'" % site["netmap-use"] |
| 2863 | lookfor_filter["id"] = site["netmap-use"] |
| 2864 | else: |
| 2865 | filter_text = "scenario name '%s'" % site["netmap-use"] |
| 2866 | lookfor_filter["name"] = site["netmap-use"] |
| 2867 | if "netmap-create" in site: |
| 2868 | create_network = True |
| 2869 | net_vim_name = net_name |
| 2870 | if site["netmap-create"]: |
| 2871 | net_vim_name = site["netmap-create"] |
| 2872 | elif sce_net["external"]: |
| 2873 | if sce_net['vim_id'] != None: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2874 | # there is a netmap at datacenter_nets database # TODO REVISE!!!! |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2875 | create_network = False |
| 2876 | lookfor_network = True |
| 2877 | lookfor_filter["id"] = sce_net['vim_id'] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2878 | filter_text = "vim_id '{}' datacenter_netmap name '{}'. Try to reload vims with "\ |
| 2879 | "datacenter-net-update".format(sce_net['vim_id'], sce_net["name"]) |
| 2880 | # look for network at datacenter and return error |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2881 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2882 | # 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] | 2883 | create_network = True |
| 2884 | lookfor_network = True |
| 2885 | lookfor_filter["name"] = sce_net["name"] |
| 2886 | net_vim_name = sce_net["name"] |
| 2887 | filter_text = "scenario name '%s'" % sce_net["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2888 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2889 | net_vim_name = net_name |
| 2890 | create_network = True |
| 2891 | lookfor_network = False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2892 | |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 2893 | task_extra = {} |
| 2894 | if create_network: |
| 2895 | task_action = "CREATE" |
| 2896 | task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None)) |
| 2897 | if lookfor_network: |
| 2898 | task_extra["find"] = (lookfor_filter,) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2899 | elif lookfor_network: |
| 2900 | task_action = "FIND" |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 2901 | task_extra["params"] = (lookfor_filter,) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2902 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2903 | # fill database content |
| 2904 | net_uuid = str(uuid4()) |
| 2905 | uuid_list.append(net_uuid) |
| 2906 | sce_net2instance[sce_net['uuid']][datacenter_id] = net_uuid |
| 2907 | db_net = { |
| 2908 | "uuid": net_uuid, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2909 | 'vim_net_id': None, |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2910 | "instance_scenario_id": instance_uuid, |
| 2911 | "sce_net_id": sce_net["uuid"], |
| 2912 | "created": create_network, |
| 2913 | 'datacenter_id': datacenter_id, |
| 2914 | 'datacenter_tenant_id': myvim_thread_id, |
| 2915 | 'status': 'BUILD' if create_network else "ACTIVE" |
| 2916 | } |
| 2917 | db_instance_nets.append(db_net) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2918 | db_vim_action = { |
| 2919 | "instance_action_id": instance_action_id, |
| 2920 | "status": "SCHEDULED", |
| 2921 | "task_index": task_index, |
| 2922 | "datacenter_vim_id": myvim_thread_id, |
| 2923 | "action": task_action, |
| 2924 | "item": "instance_nets", |
| 2925 | "item_id": net_uuid, |
| tierno | f145087 | 2017-10-17 23:15:08 +0200 | [diff] [blame] | 2926 | "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2927 | } |
| 2928 | net2task_id['scenario'][sce_net['uuid']][datacenter_id] = task_index |
| 2929 | task_index += 1 |
| 2930 | db_vim_actions.append(db_vim_action) |
| 2931 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2932 | if 'ip_profile' in sce_net: |
| 2933 | db_ip_profile={ |
| 2934 | 'instance_net_id': net_uuid, |
| 2935 | 'ip_version': sce_net['ip_profile']['ip_version'], |
| 2936 | 'subnet_address': sce_net['ip_profile']['subnet_address'], |
| 2937 | 'gateway_address': sce_net['ip_profile']['gateway_address'], |
| 2938 | 'dns_address': sce_net['ip_profile']['dns_address'], |
| 2939 | 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'], |
| 2940 | 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'], |
| 2941 | 'dhcp_count': sce_net['ip_profile']['dhcp_count'], |
| 2942 | } |
| 2943 | db_ip_profiles.append(db_ip_profile) |
| 2944 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2945 | # 2. Creating new nets (vnf internal nets) in the VIM" |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 2946 | # For each vnf net, we create it and we add it to instanceNetlist. |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2947 | for sce_vnf in scenarioDict['vnfs']: |
| 2948 | for net in sce_vnf['nets']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2949 | if sce_vnf.get("datacenter"): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2950 | datacenter_id = sce_vnf["datacenter"] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2951 | myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2952 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2953 | datacenter_id = default_datacenter_id |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2954 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2955 | descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2956 | net_name = descriptor_net.get("name") |
| 2957 | if not net_name: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2958 | net_name = "{}.{}".format(instance_name, net["name"]) |
| 2959 | net_name = net_name[:255] # limit length |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2960 | net_type = net['type'] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2961 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2962 | if sce_vnf['uuid'] not in vnf_net2instance: |
| 2963 | vnf_net2instance[sce_vnf['uuid']] = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2964 | if sce_vnf['uuid'] not in net2task_id: |
| 2965 | net2task_id[sce_vnf['uuid']] = {} |
| 2966 | net2task_id[sce_vnf['uuid']][net['uuid']] = task_index |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2967 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2968 | # fill database content |
| 2969 | net_uuid = str(uuid4()) |
| 2970 | uuid_list.append(net_uuid) |
| 2971 | vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid |
| 2972 | db_net = { |
| 2973 | "uuid": net_uuid, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2974 | 'vim_net_id': None, |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2975 | "instance_scenario_id": instance_uuid, |
| 2976 | "net_id": net["uuid"], |
| 2977 | "created": True, |
| 2978 | 'datacenter_id': datacenter_id, |
| 2979 | 'datacenter_tenant_id': myvim_thread_id, |
| 2980 | } |
| 2981 | db_instance_nets.append(db_net) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 2982 | |
| 2983 | db_vim_action = { |
| 2984 | "instance_action_id": instance_action_id, |
| 2985 | "task_index": task_index, |
| 2986 | "datacenter_vim_id": myvim_thread_id, |
| 2987 | "status": "SCHEDULED", |
| 2988 | "action": "CREATE", |
| 2989 | "item": "instance_nets", |
| 2990 | "item_id": net_uuid, |
| 2991 | "extra": yaml.safe_dump({"params": (net_name, net_type, net.get('ip_profile',None))}, |
| 2992 | default_flow_style=True, width=256) |
| 2993 | } |
| 2994 | task_index += 1 |
| 2995 | db_vim_actions.append(db_vim_action) |
| 2996 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 2997 | if 'ip_profile' in net: |
| 2998 | db_ip_profile = { |
| 2999 | 'instance_net_id': net_uuid, |
| 3000 | 'ip_version': net['ip_profile']['ip_version'], |
| 3001 | 'subnet_address': net['ip_profile']['subnet_address'], |
| 3002 | 'gateway_address': net['ip_profile']['gateway_address'], |
| 3003 | 'dns_address': net['ip_profile']['dns_address'], |
| 3004 | 'dhcp_enabled': net['ip_profile']['dhcp_enabled'], |
| 3005 | 'dhcp_start_address': net['ip_profile']['dhcp_start_address'], |
| 3006 | 'dhcp_count': net['ip_profile']['dhcp_count'], |
| 3007 | } |
| 3008 | db_ip_profiles.append(db_ip_profile) |
| 3009 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3010 | # print "vnf_net2instance:" |
| 3011 | # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3012 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3013 | # 3. Creating new vm instances in the VIM |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3014 | # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 3015 | sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) |
| garciadeblas | acd4e78 | 2017-07-23 19:44:55 +0200 | [diff] [blame] | 3016 | for sce_vnf in sce_vnf_list: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 3017 | vnf_availability_zones = [] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3018 | for vm in sce_vnf['vms']: |
| 3019 | vm_av = vm.get('availability_zone') |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 3020 | if vm_av and vm_av not in vnf_availability_zones: |
| 3021 | vnf_availability_zones.append(vm_av) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3022 | |
| 3023 | # check if there is enough availability zones available at vim level. |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 3024 | if myvims[datacenter_id].availability_zone and vnf_availability_zones: |
| 3025 | if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone): |
| 3026 | raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3027 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3028 | if sce_vnf.get("datacenter"): |
| 3029 | vim = myvims[ sce_vnf["datacenter"] ] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3030 | myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3031 | datacenter_id = sce_vnf["datacenter"] |
| 3032 | else: |
| 3033 | vim = myvims[ default_datacenter_id ] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3034 | myvim_thread_id = myvim_threads_id[ default_datacenter_id ] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3035 | datacenter_id = default_datacenter_id |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3036 | sce_vnf["datacenter_id"] = datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3037 | i = 0 |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3038 | |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3039 | vnf_uuid = str(uuid4()) |
| 3040 | uuid_list.append(vnf_uuid) |
| 3041 | db_instance_vnf = { |
| 3042 | 'uuid': vnf_uuid, |
| 3043 | 'instance_scenario_id': instance_uuid, |
| 3044 | 'vnf_id': sce_vnf['vnf_id'], |
| 3045 | 'sce_vnf_id': sce_vnf['uuid'], |
| 3046 | 'datacenter_id': datacenter_id, |
| 3047 | 'datacenter_tenant_id': myvim_thread_id, |
| 3048 | } |
| 3049 | db_instance_vnfs.append(db_instance_vnf) |
| 3050 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3051 | for vm in sce_vnf['vms']: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3052 | myVMDict = {} |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3053 | myVMDict['name'] = "{}.{}.{}".format(instance_name[:64], sce_vnf['name'][:64], vm["name"][:64]) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3054 | myVMDict['description'] = myVMDict['name'][0:99] |
| 3055 | # if not startvms: |
| 3056 | # myVMDict['start'] = "no" |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3057 | myVMDict['name'] = myVMDict['name'][0:255] # limit name length |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3058 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3059 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 3060 | image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3061 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3062 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3063 | # create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3064 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3065 | if flavor_dict['extended']!=None: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3066 | flavor_dict['extended'] = yaml.load(flavor_dict['extended']) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 3067 | flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True) |
| 3068 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3069 | # Obtain information for additional disks |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 3070 | extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id}) |
| 3071 | if not extended_flavor_dict: |
| 3072 | raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found) |
| 3073 | return |
| 3074 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3075 | # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0]) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 3076 | myVMDict['disks'] = None |
| 3077 | extended_info = extended_flavor_dict[0]['extended'] |
| 3078 | if extended_info != None: |
| 3079 | extended_flavor_dict_yaml = yaml.load(extended_info) |
| 3080 | if 'disks' in extended_flavor_dict_yaml: |
| 3081 | myVMDict['disks'] = extended_flavor_dict_yaml['disks'] |
| 3082 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3083 | vm['vim_flavor_id'] = flavor_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3084 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 3085 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3086 | myVMDict['availability_zone'] = vm.get('availability_zone') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3087 | myVMDict['networks'] = [] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3088 | task_depends_on = [] |
| 3089 | # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3090 | db_vm_ifaces = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3091 | for iface in vm['interfaces']: |
| 3092 | netDict = {} |
| 3093 | if iface['type']=="data": |
| 3094 | netDict['type'] = iface['model'] |
| 3095 | elif "model" in iface and iface["model"]!=None: |
| 3096 | netDict['model']=iface['model'] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3097 | # TODO in future, remove this because mac_address will not be set, and the type of PV,VF |
| 3098 | # is obtained from iterface table model |
| 3099 | # discover type of interface looking at flavor |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3100 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 3101 | for flavor_iface in numa.get('interfaces',[]): |
| 3102 | if flavor_iface.get('name') == iface['internal_name']: |
| 3103 | if flavor_iface['dedicated'] == 'yes': |
| 3104 | netDict['type']="PF" #passthrough |
| 3105 | elif flavor_iface['dedicated'] == 'no': |
| 3106 | netDict['type']="VF" #siov |
| 3107 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 3108 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 3109 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 3110 | break; |
| 3111 | netDict["use"]=iface['type'] |
| 3112 | if netDict["use"]=="data" and not netDict.get("type"): |
| 3113 | #print "netDict", netDict |
| 3114 | #print "iface", iface |
| 3115 | 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']) |
| 3116 | if flavor_dict.get('extended')==None: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3117 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 3118 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3119 | else: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3120 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3121 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 3122 | netDict["type"]="virtual" |
| 3123 | if "vpci" in iface and iface["vpci"] is not None: |
| 3124 | netDict['vpci'] = iface['vpci'] |
| 3125 | if "mac" in iface and iface["mac"] is not None: |
| 3126 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 3127 | if "port-security" in iface and iface["port-security"] is not None: |
| 3128 | netDict['port_security'] = iface['port-security'] |
| 3129 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 3130 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3131 | netDict['name'] = iface['internal_name'] |
| 3132 | if iface['net_id'] is None: |
| 3133 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3134 | # print iface |
| 3135 | # print vnf_iface |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3136 | if vnf_iface['interface_id']==iface['uuid']: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3137 | netDict['net_id'] = "TASK-{}".format(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]) |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3138 | instance_net_id = sce_net2instance[ vnf_iface['sce_net_id'] ][datacenter_id] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3139 | task_depends_on.append(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3140 | break |
| 3141 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3142 | netDict['net_id'] = "TASK-{}".format(net2task_id[ sce_vnf['uuid'] ][ iface['net_id'] ]) |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3143 | instance_net_id = vnf_net2instance[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3144 | task_depends_on.append(net2task_id[sce_vnf['uuid'] ][ iface['net_id']]) |
| 3145 | # skip bridge ifaces not connected to any net |
| 3146 | if 'net_id' not in netDict or netDict['net_id']==None: |
| 3147 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3148 | myVMDict['networks'].append(netDict) |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3149 | db_vm_iface={ |
| 3150 | # "uuid" |
| 3151 | # 'instance_vm_id': instance_vm_uuid, |
| 3152 | "instance_net_id": instance_net_id, |
| 3153 | 'interface_id': iface['uuid'], |
| 3154 | # 'vim_interface_id': , |
| 3155 | 'type': 'external' if iface['external_name'] is not None else 'internal', |
| 3156 | 'ip_address': iface.get('ip_address'), |
| 3157 | 'floating_ip': int(iface.get('floating-ip', False)), |
| 3158 | 'port_security': int(iface.get('port-security', True)) |
| 3159 | } |
| 3160 | db_vm_ifaces.append(db_vm_iface) |
| 3161 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 3162 | # print myVMDict['name'] |
| 3163 | # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 3164 | # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 3165 | # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 3166 | if vm.get("boot_data"): |
| 3167 | cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config) |
| 3168 | else: |
| 3169 | cloud_config_vm = cloud_config |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 3170 | if myVMDict.get('availability_zone'): |
| 3171 | av_index = vnf_availability_zones.index(myVMDict['availability_zone']) |
| mirabal | 2935631 | 2017-07-27 12:21:22 +0200 | [diff] [blame] | 3172 | else: |
| tierno | 5a3273c | 2017-08-29 11:43:46 +0200 | [diff] [blame] | 3173 | av_index = None |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3174 | for vm_index in range(0, vm.get('count', 1)): |
| 3175 | vm_index_name = "" |
| 3176 | if vm.get('count', 1) > 1: |
| 3177 | vm_index_name += "." + chr(97 + vm_index) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3178 | task_params = (myVMDict['name']+vm_index_name, myVMDict['description'], myVMDict.get('start', None), |
| 3179 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm, |
| 3180 | myVMDict['disks'], av_index, vnf_availability_zones) |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3181 | # put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 3182 | for net in myVMDict['networks']: |
| 3183 | if "vim_id" in net: |
| 3184 | for iface in vm['interfaces']: |
| 3185 | if net["name"]==iface["internal_name"]: |
| 3186 | iface["vim_id"]=net["vim_id"] |
| 3187 | break |
| 3188 | vm_uuid = str(uuid4()) |
| 3189 | uuid_list.append(vm_uuid) |
| 3190 | db_vm = { |
| 3191 | "uuid": vm_uuid, |
| 3192 | 'instance_vnf_id': vnf_uuid, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3193 | #TODO delete "vim_vm_id": vm_id, |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3194 | "vm_id": vm["uuid"], |
| 3195 | # "status": |
| 3196 | } |
| 3197 | db_instance_vms.append(db_vm) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3198 | |
| 3199 | iface_index = 0 |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3200 | for db_vm_iface in db_vm_ifaces: |
| 3201 | iface_uuid = str(uuid4()) |
| 3202 | uuid_list.append(iface_uuid) |
| 3203 | db_vm_iface_instance = { |
| 3204 | "uuid": iface_uuid, |
| 3205 | "instance_vm_id": vm_uuid |
| 3206 | } |
| 3207 | db_vm_iface_instance.update(db_vm_iface) |
| 3208 | if db_vm_iface_instance.get("ip_address"): # increment ip_address |
| 3209 | ip = db_vm_iface_instance.get("ip_address") |
| 3210 | i = ip.rfind(".") |
| 3211 | if i > 0: |
| 3212 | try: |
| 3213 | i += 1 |
| 3214 | ip = ip[i:] + str(int(ip[:i]) +1) |
| 3215 | db_vm_iface_instance["ip_address"] = ip |
| 3216 | except: |
| 3217 | db_vm_iface_instance["ip_address"] = None |
| 3218 | db_instance_interfaces.append(db_vm_iface_instance) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3219 | myVMDict['networks'][iface_index]["uuid"] = iface_uuid |
| 3220 | iface_index += 1 |
| 3221 | |
| 3222 | db_vim_action = { |
| 3223 | "instance_action_id": instance_action_id, |
| 3224 | "task_index": task_index, |
| 3225 | "datacenter_vim_id": myvim_thread_id, |
| 3226 | "action": "CREATE", |
| 3227 | "status": "SCHEDULED", |
| 3228 | "item": "instance_vms", |
| 3229 | "item_id": vm_uuid, |
| 3230 | "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on}, |
| 3231 | default_flow_style=True, width=256) |
| 3232 | } |
| 3233 | task_index += 1 |
| 3234 | db_vim_actions.append(db_vim_action) |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3235 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3236 | scenarioDict["datacenter2tenant"] = myvim_threads_id |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3237 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3238 | db_instance_action["number_tasks"] = task_index |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3239 | db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id] |
| 3240 | db_instance_scenario['datacenter_id'] = default_datacenter_id |
| 3241 | db_tables=[ |
| 3242 | {"instance_scenarios": db_instance_scenario}, |
| 3243 | {"instance_vnfs": db_instance_vnfs}, |
| 3244 | {"instance_nets": db_instance_nets}, |
| 3245 | {"ip_profiles": db_ip_profiles}, |
| 3246 | {"instance_vms": db_instance_vms}, |
| 3247 | {"instance_interfaces": db_instance_interfaces}, |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3248 | {"instance_actions": db_instance_action}, |
| 3249 | {"vim_actions": db_vim_actions} |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3250 | ] |
| 3251 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3252 | logger.debug("create_instance done DB tables: %s", |
| tierno | 8e69032 | 2017-08-10 15:58:50 +0200 | [diff] [blame] | 3253 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False) ) |
| 3254 | mydb.new_rows(db_tables, uuid_list) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3255 | for myvim_thread_id in myvim_threads_id.values(): |
| 3256 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3257 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3258 | returned_instance = mydb.get_instance_scenario(instance_uuid) |
| 3259 | returned_instance["action_id"] = instance_action_id |
| 3260 | return returned_instance |
| 3261 | except (NfvoException, vimconn.vimconnException, db_base_Exception) as e: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3262 | message = rollback(mydb, myvims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3263 | if isinstance(e, db_base_Exception): |
| 3264 | error_text = "database Exception" |
| 3265 | elif isinstance(e, vimconn.vimconnException): |
| 3266 | error_text = "VIM Exception" |
| 3267 | else: |
| 3268 | error_text = "Exception" |
| 3269 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3270 | # logger.error("create_instance: %s", error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3271 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3272 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3273 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3274 | def delete_instance(mydb, tenant_id, instance_id): |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3275 | # print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3276 | instanceDict = mydb.get_instance_scenario(instance_id, tenant_id) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3277 | # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3278 | tenant_id = instanceDict["tenant_id"] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3279 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3280 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3281 | # 1. Delete from Database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3282 | message = mydb.delete_instance_scenario(instance_id, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3283 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3284 | # 2. delete from VIM |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3285 | error_msg = "" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3286 | myvims = {} |
| 3287 | myvim_threads = {} |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3288 | vimthread_affected = {} |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 3289 | net2vm_dependencies = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3290 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3291 | task_index = 0 |
| 3292 | instance_action_id = get_task_id() |
| 3293 | db_vim_actions = [] |
| 3294 | db_instance_action = { |
| 3295 | "uuid": instance_action_id, # same uuid for the instance and the action on create |
| 3296 | "tenant_id": tenant_id, |
| 3297 | "instance_id": instance_id, |
| 3298 | "description": "DELETE", |
| 3299 | # "number_tasks": 0 # filled bellow |
| 3300 | } |
| 3301 | |
| 3302 | # 2.1 deleting VMs |
| 3303 | # vm_fail_list=[] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3304 | for sce_vnf in instanceDict['vnfs']: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3305 | datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3306 | vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3307 | if datacenter_key not in myvims: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3308 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3309 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3310 | except NfvoException as e: |
| 3311 | logger.error(str(e)) |
| 3312 | myvim_thread = None |
| 3313 | myvim_threads[datacenter_key] = myvim_thread |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3314 | vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"], |
| 3315 | datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 3316 | if len(vims) == 0: |
| 3317 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], |
| 3318 | sce_vnf["datacenter_tenant_id"])) |
| 3319 | myvims[datacenter_key] = None |
| 3320 | else: |
| 3321 | myvims[datacenter_key] = vims.values()[0] |
| 3322 | myvim = myvims[datacenter_key] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3323 | myvim_thread = myvim_threads[datacenter_key] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3324 | for vm in sce_vnf['vms']: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3325 | if not myvim: |
| 3326 | error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"]) |
| 3327 | continue |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 3328 | db_vim_action = { |
| 3329 | "instance_action_id": instance_action_id, |
| 3330 | "task_index": task_index, |
| 3331 | "datacenter_vim_id": sce_vnf["datacenter_tenant_id"], |
| 3332 | "action": "DELETE", |
| 3333 | "status": "SCHEDULED", |
| 3334 | "item": "instance_vms", |
| 3335 | "item_id": vm["uuid"], |
| 3336 | "extra": yaml.safe_dump({"params": vm["interfaces"]}, |
| 3337 | default_flow_style=True, width=256) |
| 3338 | } |
| 3339 | db_vim_actions.append(db_vim_action) |
| 3340 | for interface in vm["interfaces"]: |
| 3341 | if not interface.get("instance_net_id"): |
| 3342 | continue |
| 3343 | if interface["instance_net_id"] not in net2vm_dependencies: |
| 3344 | net2vm_dependencies[interface["instance_net_id"]] = [] |
| 3345 | net2vm_dependencies[interface["instance_net_id"]].append(task_index) |
| 3346 | task_index += 1 |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3347 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3348 | # 2.2 deleting NETS |
| 3349 | # net_fail_list=[] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3350 | for net in instanceDict['nets']: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3351 | vimthread_affected[net["datacenter_tenant_id"]] = None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3352 | datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 3353 | if datacenter_key not in myvims: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3354 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3355 | _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3356 | except NfvoException as e: |
| 3357 | logger.error(str(e)) |
| 3358 | myvim_thread = None |
| 3359 | myvim_threads[datacenter_key] = myvim_thread |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3360 | vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"], |
| 3361 | datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 3362 | if len(vims) == 0: |
| 3363 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 3364 | myvims[datacenter_key] = None |
| 3365 | else: |
| 3366 | myvims[datacenter_key] = vims.values()[0] |
| 3367 | myvim = myvims[datacenter_key] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3368 | myvim_thread = myvim_threads[datacenter_key] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3369 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3370 | if not myvim: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3371 | error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"]) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3372 | continue |
| tierno | 3fcfdb7 | 2017-10-24 07:48:24 +0200 | [diff] [blame] | 3373 | extra = {"params": (net['vim_net_id'], net['sdn_net_id'])} |
| 3374 | if net2vm_dependencies.get(net["uuid"]): |
| 3375 | extra["depends_on"] = net2vm_dependencies[net["uuid"]] |
| 3376 | db_vim_action = { |
| 3377 | "instance_action_id": instance_action_id, |
| 3378 | "task_index": task_index, |
| 3379 | "datacenter_vim_id": net["datacenter_tenant_id"], |
| 3380 | "action": "DELETE", |
| 3381 | "status": "SCHEDULED", |
| 3382 | "item": "instance_nets", |
| 3383 | "item_id": net["uuid"], |
| 3384 | "extra": yaml.safe_dump(extra, default_flow_style=True, width=256) |
| 3385 | } |
| 3386 | task_index += 1 |
| 3387 | db_vim_actions.append(db_vim_action) |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3388 | |
| 3389 | db_instance_action["number_tasks"] = task_index |
| 3390 | db_tables = [ |
| 3391 | {"instance_actions": db_instance_action}, |
| 3392 | {"vim_actions": db_vim_actions} |
| 3393 | ] |
| 3394 | |
| 3395 | logger.debug("delete_instance done DB tables: %s", |
| 3396 | yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) |
| 3397 | mydb.new_rows(db_tables, ()) |
| 3398 | for myvim_thread_id in vimthread_affected.keys(): |
| 3399 | vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions) |
| 3400 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3401 | if len(error_msg) > 0: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3402 | return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\ |
| 3403 | '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3404 | else: |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3405 | return "action_id={} instance {} deleted".format(instance_action_id, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3406 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3407 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3408 | def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None): |
| 3409 | '''Refreshes a scenario instance. It modifies instanceDict''' |
| 3410 | '''Returns: |
| 3411 | - 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 |
| 3412 | - error_msg |
| 3413 | ''' |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3414 | # # Assumption: nfvo_tenant and instance_id were checked before entering into this function |
| 3415 | # #print "nfvo.refresh_instance begins" |
| 3416 | # #print json.dumps(instanceDict, indent=4) |
| 3417 | # |
| 3418 | # #print "Getting the VIM URL and the VIM tenant_id" |
| 3419 | # myvims={} |
| 3420 | # |
| 3421 | # # 1. Getting VIM vm and net list |
| 3422 | # vms_updated = [] #List of VM instance uuids in openmano that were updated |
| 3423 | # vms_notupdated=[] |
| 3424 | # vm_list = {} |
| 3425 | # for sce_vnf in instanceDict['vnfs']: |
| 3426 | # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 3427 | # if datacenter_key not in vm_list: |
| 3428 | # vm_list[datacenter_key] = [] |
| 3429 | # if datacenter_key not in myvims: |
| 3430 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"], |
| 3431 | # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 3432 | # if len(vims) == 0: |
| 3433 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])) |
| 3434 | # myvims[datacenter_key] = None |
| 3435 | # else: |
| 3436 | # myvims[datacenter_key] = vims.values()[0] |
| 3437 | # for vm in sce_vnf['vms']: |
| 3438 | # vm_list[datacenter_key].append(vm['vim_vm_id']) |
| 3439 | # vms_notupdated.append(vm["uuid"]) |
| 3440 | # |
| 3441 | # nets_updated = [] #List of VM instance uuids in openmano that were updated |
| 3442 | # nets_notupdated=[] |
| 3443 | # net_list = {} |
| 3444 | # for net in instanceDict['nets']: |
| 3445 | # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 3446 | # if datacenter_key not in net_list: |
| 3447 | # net_list[datacenter_key] = [] |
| 3448 | # if datacenter_key not in myvims: |
| 3449 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"], |
| 3450 | # datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 3451 | # if len(vims) == 0: |
| 3452 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 3453 | # myvims[datacenter_key] = None |
| 3454 | # else: |
| 3455 | # myvims[datacenter_key] = vims.values()[0] |
| 3456 | # |
| 3457 | # net_list[datacenter_key].append(net['vim_net_id']) |
| 3458 | # nets_notupdated.append(net["uuid"]) |
| 3459 | # |
| 3460 | # # 1. Getting the status of all VMs |
| 3461 | # vm_dict={} |
| 3462 | # for datacenter_key in myvims: |
| 3463 | # if not vm_list.get(datacenter_key): |
| 3464 | # continue |
| 3465 | # failed = True |
| 3466 | # failed_message="" |
| 3467 | # if not myvims[datacenter_key]: |
| 3468 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 3469 | # else: |
| 3470 | # try: |
| 3471 | # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) ) |
| 3472 | # failed = False |
| 3473 | # except vimconn.vimconnException as e: |
| 3474 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 3475 | # failed_message = str(e) |
| 3476 | # if failed: |
| 3477 | # for vm in vm_list[datacenter_key]: |
| 3478 | # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 3479 | # |
| 3480 | # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed |
| 3481 | # for sce_vnf in instanceDict['vnfs']: |
| 3482 | # for vm in sce_vnf['vms']: |
| 3483 | # vm_id = vm['vim_vm_id'] |
| 3484 | # interfaces = vm_dict[vm_id].pop('interfaces', []) |
| 3485 | # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE |
| 3486 | # has_mgmt_iface = False |
| 3487 | # for iface in vm["interfaces"]: |
| 3488 | # if iface["type"]=="mgmt": |
| 3489 | # has_mgmt_iface = True |
| 3490 | # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface: |
| 3491 | # vm_dict[vm_id]['status'] = "ACTIVE" |
| 3492 | # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024: |
| 3493 | # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:] |
| 3494 | # 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'): |
| 3495 | # vm['status'] = vm_dict[vm_id]['status'] |
| 3496 | # vm['error_msg'] = vm_dict[vm_id].get('error_msg') |
| 3497 | # vm['vim_info'] = vm_dict[vm_id].get('vim_info') |
| 3498 | # # 2.1. Update in openmano DB the VMs whose status changed |
| 3499 | # try: |
| 3500 | # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]}) |
| 3501 | # vms_notupdated.remove(vm["uuid"]) |
| 3502 | # if updates>0: |
| 3503 | # vms_updated.append(vm["uuid"]) |
| 3504 | # except db_base_Exception as e: |
| 3505 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 3506 | # # 2.2. Update in openmano DB the interface VMs |
| 3507 | # for interface in interfaces: |
| 3508 | # #translate from vim_net_id to instance_net_id |
| 3509 | # network_id_list=[] |
| 3510 | # for net in instanceDict['nets']: |
| 3511 | # if net["vim_net_id"] == interface["vim_net_id"]: |
| 3512 | # network_id_list.append(net["uuid"]) |
| 3513 | # if not network_id_list: |
| 3514 | # continue |
| 3515 | # del interface["vim_net_id"] |
| 3516 | # try: |
| 3517 | # for network_id in network_id_list: |
| 3518 | # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id}) |
| 3519 | # except db_base_Exception as e: |
| 3520 | # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id) |
| 3521 | # |
| 3522 | # # 3. Getting the status of all nets |
| 3523 | # net_dict = {} |
| 3524 | # for datacenter_key in myvims: |
| 3525 | # if not net_list.get(datacenter_key): |
| 3526 | # continue |
| 3527 | # failed = True |
| 3528 | # failed_message = "" |
| 3529 | # if not myvims[datacenter_key]: |
| 3530 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 3531 | # else: |
| 3532 | # try: |
| 3533 | # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) ) |
| 3534 | # failed = False |
| 3535 | # except vimconn.vimconnException as e: |
| 3536 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 3537 | # failed_message = str(e) |
| 3538 | # if failed: |
| 3539 | # for net in net_list[datacenter_key]: |
| 3540 | # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 3541 | # |
| 3542 | # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed |
| 3543 | # # TODO: update nets inside a vnf |
| 3544 | # for net in instanceDict['nets']: |
| 3545 | # net_id = net['vim_net_id'] |
| 3546 | # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024: |
| 3547 | # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:] |
| 3548 | # 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'): |
| 3549 | # net['status'] = net_dict[net_id]['status'] |
| 3550 | # net['error_msg'] = net_dict[net_id].get('error_msg') |
| 3551 | # net['vim_info'] = net_dict[net_id].get('vim_info') |
| 3552 | # # 5.1. Update in openmano DB the nets whose status changed |
| 3553 | # try: |
| 3554 | # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]}) |
| 3555 | # nets_notupdated.remove(net["uuid"]) |
| 3556 | # if updated>0: |
| 3557 | # nets_updated.append(net["uuid"]) |
| 3558 | # except db_base_Exception as e: |
| 3559 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 3560 | # |
| 3561 | # # Returns appropriate output |
| 3562 | # #print "nfvo.refresh_instance finishes" |
| 3563 | # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s", |
| 3564 | # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3565 | instance_id = instanceDict['uuid'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 3566 | # if len(vms_notupdated)+len(nets_notupdated)>0: |
| 3567 | # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated) |
| 3568 | # 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] | 3569 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3570 | return 0, 'Scenario instance ' + instance_id + ' refreshed.' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3571 | |
| 3572 | def instance_action(mydb,nfvo_tenant,instance_id, action_dict): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3573 | #print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3574 | instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3575 | #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| 3576 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3577 | #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] | 3578 | vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id']) |
| 3579 | if len(vims) == 0: |
| 3580 | raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3581 | myvim = vims.values()[0] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3582 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3583 | if action_dict.get("create-vdu"): |
| 3584 | for vdu in action_dict["create-vdu"]: |
| 3585 | vdu_id = vdu.get("vdu-id") |
| 3586 | vdu_count = vdu.get("count", 1) |
| 3587 | # get from database TODO |
| 3588 | # insert tasks TODO |
| 3589 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3590 | |
| 3591 | input_vnfs = action_dict.pop("vnfs", []) |
| 3592 | input_vms = action_dict.pop("vms", []) |
| 3593 | action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False |
| 3594 | vm_result = {} |
| 3595 | vm_error = 0 |
| 3596 | vm_ok = 0 |
| 3597 | for sce_vnf in instanceDict['vnfs']: |
| 3598 | for vm in sce_vnf['vms']: |
| 3599 | if not action_over_all: |
| 3600 | if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \ |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3601 | vm['uuid'] not in input_vms and vm['name'] not in input_vms: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3602 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3603 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3604 | if "add_public_key" in action_dict: |
| 3605 | mgmt_access = {} |
| 3606 | if sce_vnf.get('mgmt_access'): |
| 3607 | mgmt_access = yaml.load(sce_vnf['mgmt_access']) |
| 3608 | ssh_access = mgmt_access['config-access']['ssh-access'] |
| 3609 | tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3610 | try: |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3611 | if ssh_access['required'] and ssh_access['default-user']: |
| 3612 | if 'ip_address' in vm: |
| 3613 | mgmt_ip = vm['ip_address'].split(';') |
| 3614 | password = mgmt_access['config-access'].get('password') |
| 3615 | priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid']) |
| 3616 | myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'], |
| 3617 | action_dict['add_public_key'], |
| 3618 | password=password, ro_key=priv_RO_key) |
| 3619 | else: |
| 3620 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 3621 | HTTP_Internal_Server_Error) |
| 3622 | except KeyError: |
| 3623 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 3624 | HTTP_Internal_Server_Error) |
| 3625 | else: |
| 3626 | raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']), |
| 3627 | HTTP_Internal_Server_Error) |
| 3628 | else: |
| 3629 | data = myvim.action_vminstance(vm['vim_vm_id'], action_dict) |
| 3630 | if "console" in action_dict: |
| 3631 | if not global_config["http_console_proxy"]: |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 3632 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 3633 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 3634 | protocol=data["protocol"], |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3635 | ip = data["server"], |
| 3636 | port = data["port"], |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 3637 | suffix = data["suffix"]), |
| 3638 | "name":vm['name'] |
| 3639 | } |
| 3640 | vm_ok +=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3641 | elif data["server"]=="127.0.0.1" or data["server"]=="localhost": |
| 3642 | vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized, |
| 3643 | "description": "this console is only reachable by local interface", |
| 3644 | "name":vm['name'] |
| 3645 | } |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 3646 | vm_error+=1 |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3647 | else: |
| 3648 | #print "console data", data |
| 3649 | try: |
| 3650 | console_thread = create_or_use_console_proxy_thread(data["server"], data["port"]) |
| 3651 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 3652 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 3653 | protocol=data["protocol"], |
| 3654 | ip = global_config["http_console_host"], |
| 3655 | port = console_thread.port, |
| 3656 | suffix = data["suffix"]), |
| 3657 | "name":vm['name'] |
| 3658 | } |
| 3659 | vm_ok +=1 |
| 3660 | except NfvoException as e: |
| 3661 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 3662 | vm_error+=1 |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 3663 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3664 | else: |
| 3665 | vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']} |
| 3666 | vm_ok +=1 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3667 | except vimconn.vimconnException as e: |
| 3668 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 3669 | vm_error+=1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3670 | |
| 3671 | if vm_ok==0: #all goes wrong |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 3672 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3673 | else: |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 3674 | return vm_result |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3675 | |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3676 | def instance_action_get(mydb, nfvo_tenant, instance_id, action_id): |
| 3677 | filter={} |
| 3678 | if nfvo_tenant and nfvo_tenant != "any": |
| 3679 | filter["tenant_id"] = nfvo_tenant |
| 3680 | if instance_id and instance_id != "any": |
| 3681 | filter["instance_id"] = instance_id |
| 3682 | if action_id: |
| 3683 | filter["uuid"] = action_id |
| 3684 | rows = mydb.get_rows(FROM="instance_actions", WHERE=filter) |
| 3685 | if not rows and action_id: |
| 3686 | raise NfvoException("Not found any action with this criteria", HTTP_Not_Found) |
| 3687 | return {"ations": rows} |
| 3688 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3689 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3690 | def create_or_use_console_proxy_thread(console_server, console_port): |
| 3691 | #look for a non-used port |
| 3692 | console_thread_key = console_server + ":" + str(console_port) |
| 3693 | if console_thread_key in global_config["console_thread"]: |
| 3694 | #global_config["console_thread"][console_thread_key].start_timeout() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3695 | return global_config["console_thread"][console_thread_key] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3696 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3697 | for port in global_config["console_port_iterator"](): |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 3698 | #print "create_or_use_console_proxy_thread() port:", port |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3699 | if port in global_config["console_ports"]: |
| 3700 | continue |
| 3701 | try: |
| 3702 | clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port) |
| 3703 | clithread.start() |
| 3704 | global_config["console_thread"][console_thread_key] = clithread |
| 3705 | global_config["console_ports"][port] = console_thread_key |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3706 | return clithread |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3707 | except cli.ConsoleProxyExceptionPortUsed as e: |
| 3708 | #port used, try with onoher |
| 3709 | continue |
| 3710 | except cli.ConsoleProxyException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3711 | raise NfvoException(str(e), HTTP_Bad_Request) |
| 3712 | raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3713 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3714 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3715 | def check_tenant(mydb, tenant_id): |
| 3716 | '''check that tenant exists at database''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3717 | tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id}) |
| 3718 | if not tenant: |
| 3719 | raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found) |
| 3720 | return |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3721 | |
| 3722 | def new_tenant(mydb, tenant_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3723 | |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3724 | tenant_uuid = str(uuid4()) |
| 3725 | tenant_dict['uuid'] = tenant_uuid |
| 3726 | try: |
| 3727 | pub_key, priv_key = create_RO_keypair(tenant_uuid) |
| 3728 | tenant_dict['RO_pub_key'] = pub_key |
| 3729 | tenant_dict['encrypted_RO_priv_key'] = priv_key |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 3730 | mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 3731 | except db_base_Exception as e: |
| 3732 | raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), HTTP_Internal_Server_Error) |
| 3733 | return tenant_uuid |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3734 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3735 | def delete_tenant(mydb, tenant): |
| 3736 | #get nfvo_tenant info |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3737 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3738 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant') |
| 3739 | mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid']) |
| 3740 | return tenant_dict['uuid'] + " " + tenant_dict["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3741 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3742 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3743 | def new_datacenter(mydb, datacenter_descriptor): |
| 3744 | if "config" in datacenter_descriptor: |
| 3745 | datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256) |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 3746 | #Check that datacenter-type is correct |
| 3747 | datacenter_type = datacenter_descriptor.get("type", "openvim"); |
| 3748 | module_info = None |
| 3749 | try: |
| 3750 | module = "vimconn_" + datacenter_type |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 3751 | pkg = __import__("osm_ro." + module) |
| 3752 | vim_conn = getattr(pkg, module) |
| 3753 | # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 3754 | except (IOError, ImportError): |
| tierno | 361275f | 2017-04-25 16:24:34 +0200 | [diff] [blame] | 3755 | # if module_info and module_info[0]: |
| 3756 | # file.close(module_info[0]) |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 3757 | raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3758 | |
| gcalvino | c62cfa5 | 2017-10-05 18:21:25 +0200 | [diff] [blame] | 3759 | datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3760 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3761 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3762 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3763 | def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3764 | # obtain data, check that only one exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3765 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3766 | |
| 3767 | # edit data |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3768 | datacenter_id = datacenter['uuid'] |
| 3769 | where={'uuid': datacenter['uuid']} |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3770 | remove_port_mapping = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3771 | if "config" in datacenter_descriptor: |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3772 | if datacenter_descriptor['config'] != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3773 | try: |
| 3774 | new_config_dict = datacenter_descriptor["config"] |
| 3775 | #delete null fields |
| 3776 | to_delete=[] |
| 3777 | for k in new_config_dict: |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3778 | if new_config_dict[k] == None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3779 | to_delete.append(k) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3780 | if k == 'sdn-controller': |
| 3781 | remove_port_mapping = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3782 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3783 | config_text = datacenter.get("config") |
| 3784 | if not config_text: |
| 3785 | config_text = '{}' |
| 3786 | config_dict = yaml.load(config_text) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3787 | config_dict.update(new_config_dict) |
| 3788 | #delete null fields |
| 3789 | for k in to_delete: |
| 3790 | del config_dict[k] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3791 | except Exception as e: |
| 3792 | raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3793 | if config_dict: |
| 3794 | datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256) |
| 3795 | else: |
| 3796 | datacenter_descriptor["config"] = None |
| 3797 | if remove_port_mapping: |
| 3798 | try: |
| 3799 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id) |
| 3800 | except ovimException as e: |
| 3801 | logger.error("Error deleting datacenter-port-mapping " + str(e)) |
| 3802 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3803 | mydb.update_rows('datacenters', datacenter_descriptor, where) |
| 3804 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3805 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3806 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3807 | def delete_datacenter(mydb, datacenter): |
| 3808 | #get nfvo_tenant info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3809 | datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter') |
| 3810 | mydb.delete_row_by_id("datacenters", datacenter_dict['uuid']) |
| tierno | 8fe7a49 | 2017-07-11 13:50:04 +0200 | [diff] [blame] | 3811 | try: |
| 3812 | datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid']) |
| 3813 | except ovimException as e: |
| 3814 | logger.error("Error deleting datacenter-port-mapping " + str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3815 | return datacenter_dict['uuid'] + " " + datacenter_dict['name'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3816 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3817 | |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 3818 | def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None): |
| tierno | 9c22f2d | 2017-10-09 16:23:55 +0200 | [diff] [blame] | 3819 | # get datacenter info |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3820 | try: |
| 3821 | datacenter_id = get_datacenter_uuid(mydb, None, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3822 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3823 | create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3824 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3825 | # get nfvo_tenant info |
| 3826 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant) |
| 3827 | if vim_tenant_name==None: |
| 3828 | vim_tenant_name=tenant_dict['name'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3829 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3830 | #check that this association does not exist before |
| 3831 | tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id } |
| 3832 | tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 3833 | if len(tenants_datacenters)>0: |
| 3834 | 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] | 3835 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3836 | vim_tenant_id_exist_atdb=False |
| 3837 | if not create_vim_tenant: |
| 3838 | where_={"datacenter_id": datacenter_id} |
| 3839 | if vim_tenant_id!=None: |
| 3840 | where_["vim_tenant_id"] = vim_tenant_id |
| 3841 | if vim_tenant_name!=None: |
| 3842 | where_["vim_tenant_name"] = vim_tenant_name |
| 3843 | #check if vim_tenant_id is already at database |
| 3844 | datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_) |
| 3845 | if len(datacenter_tenants_dict)>=1: |
| 3846 | datacenter_tenants_dict = datacenter_tenants_dict[0] |
| 3847 | vim_tenant_id_exist_atdb=True |
| 3848 | #TODO check if a field has changed and edit entry at datacenter_tenants at DB |
| 3849 | else: #result=0 |
| 3850 | datacenter_tenants_dict = {} |
| 3851 | #insert at table datacenter_tenants |
| 3852 | else: #if vim_tenant_id==None: |
| 3853 | #create tenant at VIM if not provided |
| 3854 | try: |
| 3855 | _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, |
| 3856 | vim_passwd=vim_password) |
| 3857 | datacenter_name = myvim["name"] |
| 3858 | vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name) |
| 3859 | except vimconn.vimconnException as e: |
| 3860 | raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3861 | datacenter_tenants_dict = {} |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3862 | datacenter_tenants_dict["created"]="true" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3863 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3864 | #fill datacenter_tenants table |
| 3865 | if not vim_tenant_id_exist_atdb: |
| 3866 | datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id |
| 3867 | datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name |
| 3868 | datacenter_tenants_dict["user"] = vim_username |
| 3869 | datacenter_tenants_dict["passwd"] = vim_password |
| 3870 | datacenter_tenants_dict["datacenter_id"] = datacenter_id |
| 3871 | if config: |
| 3872 | datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256) |
| 3873 | id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True) |
| 3874 | datacenter_tenants_dict["uuid"] = id_ |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3875 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3876 | #fill tenants_datacenters table |
| 3877 | datacenter_tenant_id = datacenter_tenants_dict["uuid"] |
| 3878 | tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id |
| 3879 | mydb.new_row('tenants_datacenters', tenants_datacenter_dict) |
| 3880 | # create thread |
| 3881 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data |
| 3882 | datacenter_name = myvim["name"] |
| 3883 | thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid']) |
| 3884 | new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id, |
| 3885 | db=db, db_lock=db_lock, ovim=ovim) |
| 3886 | new_thread.start() |
| 3887 | thread_id = datacenter_tenants_dict["uuid"] |
| 3888 | vim_threads["running"][thread_id] = new_thread |
| 3889 | return datacenter_id |
| 3890 | except vimconn.vimconnException as e: |
| 3891 | raise NfvoException(str(e), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3892 | |
| tierno | 9931490 | 2017-04-26 13:23:09 +0200 | [diff] [blame] | 3893 | |
| 3894 | def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None, |
| 3895 | vim_username=None, vim_password=None, config=None): |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3896 | #Obtain the data of this datacenter_tenant_id |
| 3897 | vim_data = mydb.get_rows( |
| 3898 | SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user", |
| 3899 | "datacenter_tenants.passwd", "datacenter_tenants.config"), |
| 3900 | FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id", |
| 3901 | WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant, |
| 3902 | "tenants_datacenters.datacenter_id": datacenter_id}) |
| 3903 | |
| 3904 | logger.debug(str(vim_data)) |
| 3905 | if len(vim_data) < 1: |
| 3906 | raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict) |
| 3907 | |
| 3908 | v = vim_data[0] |
| 3909 | if v['config']: |
| 3910 | v['config'] = yaml.load(v['config']) |
| 3911 | |
| 3912 | if vim_tenant_id: |
| 3913 | v['vim_tenant_id'] = vim_tenant_id |
| 3914 | if vim_tenant_name: |
| 3915 | v['vim_tenant_name'] = vim_tenant_name |
| 3916 | if vim_username: |
| 3917 | v['user'] = vim_username |
| 3918 | if vim_password: |
| 3919 | v['passwd'] = vim_password |
| 3920 | if config: |
| 3921 | if not v['config']: |
| 3922 | v['config'] = {} |
| 3923 | v['config'].update(config) |
| 3924 | |
| 3925 | logger.debug(str(v)) |
| 3926 | deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id']) |
| 3927 | associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'], |
| 3928 | vim_username=v['user'], vim_password=v['passwd'], config=v['config']) |
| 3929 | |
| 3930 | return datacenter_id |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3931 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3932 | def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3933 | #get nfvo_tenant info |
| 3934 | if not tenant_id or tenant_id=="any": |
| 3935 | tenant_uuid = None |
| 3936 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3937 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3938 | tenant_uuid = tenant_dict['uuid'] |
| 3939 | |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3940 | datacenter_id = get_datacenter_uuid(mydb, tenant_uuid, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3941 | #check that this association exist before |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3942 | tenants_datacenter_dict={"datacenter_id": datacenter_id } |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3943 | if tenant_uuid: |
| 3944 | tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3945 | tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 3946 | if len(tenant_datacenter_list)==0 and tenant_uuid: |
| 3947 | 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] | 3948 | |
| 3949 | #delete this association |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3950 | mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3951 | |
| 3952 | #get vim_tenant info and deletes |
| 3953 | warning='' |
| 3954 | for tenant_datacenter_item in tenant_datacenter_list: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3955 | vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 3956 | #try to delete vim:tenant |
| 3957 | try: |
| 3958 | mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 3959 | if vim_tenant_dict['created']=='true': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3960 | #delete tenant at VIM if created by NFVO |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3961 | try: |
| tierno | 0ea2a7e | 2017-10-18 00:06:26 +0200 | [diff] [blame] | 3962 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3963 | myvim.delete_tenant(vim_tenant_dict['vim_tenant_id']) |
| 3964 | except vimconn.vimconnException as e: |
| 3965 | warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e)) |
| 3966 | logger.warn(warning) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3967 | except db_base_Exception as e: |
| 3968 | logger.error("Cannot delete datacenter_tenants " + str(e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3969 | 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] | 3970 | thread_id = tenant_datacenter_item["datacenter_tenant_id"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3971 | thread = vim_threads["running"][thread_id] |
| tierno | 868220c | 2017-09-26 00:11:05 +0200 | [diff] [blame] | 3972 | thread.insert_task("exit") |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3973 | vim_threads["deleting"][thread_id] = thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3974 | return "datacenter {} detached. {}".format(datacenter_id, warning) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3975 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3976 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3977 | def datacenter_action(mydb, tenant_id, datacenter, action_dict): |
| 3978 | #DEPRECATED |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3979 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3980 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3981 | |
| 3982 | if 'net-update' in action_dict: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3983 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3984 | 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] | 3985 | #print content |
| 3986 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3987 | #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e)) |
| 3988 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3989 | #update nets Change from VIM format to NFVO format |
| 3990 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3991 | for net in nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3992 | net_nfvo={'datacenter_id': datacenter_id} |
| 3993 | net_nfvo['name'] = net['name'] |
| 3994 | #net_nfvo['description']= net['name'] |
| 3995 | net_nfvo['vim_net_id'] = net['id'] |
| 3996 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 3997 | net_nfvo['shared'] = net['shared'] |
| 3998 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| 3999 | net_list.append(net_nfvo) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4000 | inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list) |
| 4001 | logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted) |
| 4002 | return inserted |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4003 | elif 'net-edit' in action_dict: |
| 4004 | net = action_dict['net-edit'].pop('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4005 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4006 | result = mydb.update_rows('datacenter_nets', action_dict['net-edit'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4007 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4008 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4009 | elif 'net-delete' in action_dict: |
| 4010 | net = action_dict['net-deelte'].get('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4011 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4012 | result = mydb.delete_row(FROM='datacenter_nets', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4013 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4014 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4015 | |
| 4016 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4017 | raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4018 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4019 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4020 | def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): |
| 4021 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4022 | datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4023 | |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4024 | what = 'uuid' if utils.check_valid_uuid(netmap) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4025 | result = mydb.update_rows('datacenter_nets', action_dict['netmap'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4026 | WHERE={'datacenter_id':datacenter_id, what: netmap}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4027 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4028 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4029 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4030 | def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): |
| 4031 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4032 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4033 | filter_dict={} |
| 4034 | if action_dict: |
| 4035 | action_dict = action_dict["netmap"] |
| 4036 | if 'vim_id' in action_dict: |
| 4037 | filter_dict["id"] = action_dict['vim_id'] |
| 4038 | if 'vim_name' in action_dict: |
| 4039 | filter_dict["name"] = action_dict['vim_name'] |
| 4040 | else: |
| 4041 | filter_dict["shared"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4042 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4043 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4044 | vim_nets = myvim.get_network_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4045 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4046 | #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e)) |
| 4047 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| 4048 | if len(vim_nets)>1 and action_dict: |
| 4049 | raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict) |
| 4050 | elif len(vim_nets)==0: # and action_dict: |
| 4051 | 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] | 4052 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4053 | for net in vim_nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4054 | net_nfvo={'datacenter_id': datacenter_id} |
| 4055 | if action_dict and "name" in action_dict: |
| 4056 | net_nfvo['name'] = action_dict['name'] |
| 4057 | else: |
| 4058 | net_nfvo['name'] = net['name'] |
| 4059 | #net_nfvo['description']= net['name'] |
| 4060 | net_nfvo['vim_net_id'] = net['id'] |
| 4061 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 4062 | net_nfvo['shared'] = net['shared'] |
| 4063 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4064 | try: |
| 4065 | net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4066 | net_nfvo["status"] = "OK" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4067 | net_nfvo["uuid"] = net_id |
| 4068 | except db_base_Exception as e: |
| 4069 | if action_dict: |
| 4070 | raise |
| 4071 | else: |
| 4072 | net_nfvo["status"] = "FAIL: " + str(e) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4073 | net_list.append(net_nfvo) |
| 4074 | return net_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4075 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4076 | def get_sdn_net_id(mydb, tenant_id, datacenter, network_id): |
| 4077 | # obtain all network data |
| 4078 | try: |
| 4079 | if utils.check_valid_uuid(network_id): |
| 4080 | filter_dict = {"id": network_id} |
| 4081 | else: |
| 4082 | filter_dict = {"name": network_id} |
| 4083 | |
| 4084 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 4085 | network = myvim.get_network_list(filter_dict=filter_dict) |
| 4086 | except vimconn.vimconnException as e: |
| tierno | f1ba57e | 2017-09-07 12:23:19 +0200 | [diff] [blame] | 4087 | 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] | 4088 | |
| 4089 | # ensure the network is defined |
| 4090 | if len(network) == 0: |
| 4091 | raise NfvoException("Network {} is not present in the system".format(network_id), |
| 4092 | HTTP_Bad_Request) |
| 4093 | |
| 4094 | # ensure there is only one network with the provided name |
| 4095 | if len(network) > 1: |
| 4096 | raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request) |
| 4097 | |
| 4098 | # ensure it is a dataplane network |
| 4099 | if network[0]['type'] != 'data': |
| 4100 | return None |
| 4101 | |
| 4102 | # ensure we use the id |
| 4103 | network_id = network[0]['id'] |
| 4104 | |
| 4105 | # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id |
| 4106 | # and with instance_scenario_id==NULL |
| 4107 | #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None} |
| 4108 | search_dict = {'vim_net_id': network_id} |
| 4109 | |
| 4110 | try: |
| 4111 | #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id'] |
| 4112 | result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict) |
| 4113 | except db_base_Exception as e: |
| 4114 | raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format( |
| 4115 | network_id) + str(e), HTTP_Internal_Server_Error) |
| 4116 | |
| 4117 | sdn_net_counter = 0 |
| 4118 | for net in result: |
| 4119 | if net['sdn_net_id'] != None: |
| 4120 | sdn_net_counter+=1 |
| 4121 | sdn_net_id = net['sdn_net_id'] |
| 4122 | |
| 4123 | if sdn_net_counter == 0: |
| 4124 | return None |
| 4125 | elif sdn_net_counter == 1: |
| 4126 | return sdn_net_id |
| 4127 | else: |
| 4128 | raise NfvoException("More than one SDN network is associated to vim network {}".format( |
| 4129 | network_id), HTTP_Internal_Server_Error) |
| 4130 | |
| 4131 | def get_sdn_controller_id(mydb, datacenter): |
| 4132 | # Obtain sdn controller id |
| 4133 | config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}') |
| 4134 | if not config: |
| 4135 | return None |
| 4136 | |
| 4137 | return yaml.load(config).get('sdn-controller') |
| 4138 | |
| 4139 | def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor): |
| 4140 | try: |
| 4141 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 4142 | if not sdn_network_id: |
| 4143 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error) |
| 4144 | |
| 4145 | #Obtain sdn controller id |
| 4146 | controller_id = get_sdn_controller_id(mydb, datacenter) |
| 4147 | if not controller_id: |
| 4148 | raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error) |
| 4149 | |
| 4150 | #Obtain sdn controller info |
| 4151 | sdn_controller = ovim.show_of_controller(controller_id) |
| 4152 | |
| 4153 | port_data = { |
| 4154 | 'name': 'external_port', |
| 4155 | 'net_id': sdn_network_id, |
| 4156 | 'ofc_id': controller_id, |
| 4157 | 'switch_dpid': sdn_controller['dpid'], |
| 4158 | 'switch_port': descriptor['port'] |
| 4159 | } |
| 4160 | |
| 4161 | if 'vlan' in descriptor: |
| 4162 | port_data['vlan'] = descriptor['vlan'] |
| 4163 | if 'mac' in descriptor: |
| 4164 | port_data['mac'] = descriptor['mac'] |
| 4165 | |
| 4166 | result = ovim.new_port(port_data) |
| 4167 | except ovimException as e: |
| 4168 | raise NfvoException("ovimException attaching SDN network {} to vim network {}".format( |
| 4169 | sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error) |
| 4170 | except db_base_Exception as e: |
| 4171 | raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format( |
| 4172 | network_id) + str(e), HTTP_Internal_Server_Error) |
| 4173 | |
| 4174 | return 'Port uuid: '+ result |
| 4175 | |
| 4176 | def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None): |
| 4177 | if port_id: |
| 4178 | filter = {'uuid': port_id} |
| 4179 | else: |
| 4180 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) |
| 4181 | if not sdn_network_id: |
| 4182 | raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), |
| 4183 | HTTP_Internal_Server_Error) |
| 4184 | #in case no port_id is specified only ports marked as 'external_port' will be detached |
| 4185 | filter = {'name': 'external_port', 'net_id': sdn_network_id} |
| 4186 | |
| 4187 | try: |
| 4188 | port_list = ovim.get_ports(columns={'uuid'}, filter=filter) |
| 4189 | except ovimException as e: |
| 4190 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| 4191 | HTTP_Internal_Server_Error) |
| 4192 | |
| 4193 | if len(port_list) == 0: |
| 4194 | raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id), |
| 4195 | HTTP_Bad_Request) |
| 4196 | |
| 4197 | port_uuid_list = [] |
| 4198 | for port in port_list: |
| 4199 | try: |
| 4200 | port_uuid_list.append(port['uuid']) |
| 4201 | ovim.delete_port(port['uuid']) |
| 4202 | except ovimException as e: |
| 4203 | raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error) |
| 4204 | |
| 4205 | return 'Detached ports uuid: {}'.format(','.join(port_uuid_list)) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4206 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4207 | def vim_action_get(mydb, tenant_id, datacenter, item, name): |
| 4208 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4209 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4210 | filter_dict={} |
| 4211 | if name: |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 4212 | if utils.check_valid_uuid(name): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4213 | filter_dict["id"] = name |
| 4214 | else: |
| 4215 | filter_dict["name"] = name |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4216 | try: |
| 4217 | if item=="networks": |
| 4218 | #filter_dict['tenant_id'] = myvim['tenant_id'] |
| 4219 | content = myvim.get_network_list(filter_dict=filter_dict) |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4220 | |
| 4221 | if len(content) == 0: |
| 4222 | raise NfvoException("Network {} is not present in the system. ".format(name), |
| 4223 | HTTP_Bad_Request) |
| 4224 | |
| 4225 | #Update the networks with the attached ports |
| 4226 | for net in content: |
| 4227 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id']) |
| 4228 | if sdn_network_id != None: |
| 4229 | try: |
| 4230 | #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id}) |
| 4231 | port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id}) |
| 4232 | except ovimException as e: |
| 4233 | raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error) |
| 4234 | #Remove field name and if port name is external_port save it as 'type' |
| 4235 | for port in port_list: |
| 4236 | if port['name'] == 'external_port': |
| 4237 | port['type'] = "External" |
| 4238 | del port['name'] |
| 4239 | net['sdn_network_id'] = sdn_network_id |
| 4240 | net['sdn_attached_ports'] = port_list |
| 4241 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4242 | elif item=="tenants": |
| 4243 | content = myvim.get_tenant_list(filter_dict=filter_dict) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 4244 | elif item == "images": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4245 | |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 4246 | content = myvim.get_image_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4247 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4248 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 4249 | 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] | 4250 | if name and len(content)==1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4251 | return {item[:-1]: content[0]} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4252 | elif name and len(content)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4253 | 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] | 4254 | datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4255 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4256 | return {item: content} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4257 | except vimconn.vimconnException as e: |
| 4258 | 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] | 4259 | 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] | 4260 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4261 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4262 | def vim_action_delete(mydb, tenant_id, datacenter, item, name): |
| 4263 | #get datacenter info |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 4264 | if tenant_id == "any": |
| 4265 | tenant_id=None |
| 4266 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4267 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 4268 | #get uuid name |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4269 | content = vim_action_get(mydb, tenant_id, datacenter, item, name) |
| 4270 | logger.debug("vim_action_delete vim response: " + str(content)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 4271 | items = content.values()[0] |
| 4272 | if type(items)==list and len(items)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4273 | raise NfvoException("Not found " + item, HTTP_Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 4274 | elif type(items)==list and len(items)>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4275 | 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] | 4276 | else: # it is a dict |
| 4277 | item_id = items["id"] |
| 4278 | item_name = str(items.get("name")) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4279 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4280 | try: |
| 4281 | if item=="networks": |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4282 | # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it |
| 4283 | sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id) |
| 4284 | if sdn_network_id != None: |
| 4285 | #Delete any port attachment to this network |
| 4286 | try: |
| 4287 | port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id}) |
| 4288 | except ovimException as e: |
| 4289 | raise NfvoException( |
| 4290 | "ovimException obtaining external ports for net {}. ".format(network_id) + str(e), |
| 4291 | HTTP_Internal_Server_Error) |
| 4292 | |
| 4293 | # By calling one by one all ports to be detached we ensure that not only the external_ports get detached |
| 4294 | for port in port_list: |
| 4295 | vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid']) |
| 4296 | |
| 4297 | #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id |
| 4298 | try: |
| 4299 | mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id}) |
| 4300 | except db_base_Exception as e: |
| 4301 | raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) + |
| 4302 | str(e), HTTP_Internal_Server_Error) |
| 4303 | |
| 4304 | #Delete the SDN network |
| 4305 | try: |
| 4306 | ovim.delete_network(sdn_network_id) |
| 4307 | except ovimException as e: |
| 4308 | logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True) |
| 4309 | raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), |
| 4310 | HTTP_Internal_Server_Error) |
| 4311 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4312 | content = myvim.delete_network(item_id) |
| 4313 | elif item=="tenants": |
| 4314 | content = myvim.delete_tenant(item_id) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 4315 | elif item == "images": |
| 4316 | content = myvim.delete_image(item_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4317 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4318 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4319 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4320 | #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e))) |
| 4321 | 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] | 4322 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4323 | return "{} {} {} deleted".format(item[:-1], item_id,item_name) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4324 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 4325 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4326 | def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): |
| 4327 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4328 | logger.debug("vim_action_create descriptor %s", str(descriptor)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 4329 | if tenant_id == "any": |
| 4330 | tenant_id=None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 4331 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4332 | try: |
| 4333 | if item=="networks": |
| 4334 | net = descriptor["network"] |
| 4335 | net_name = net.pop("name") |
| 4336 | net_type = net.pop("type", "bridge") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 4337 | net_public = net.pop("shared", False) |
| 4338 | net_ipprofile = net.pop("ip_profile", None) |
| tierno | a7d34d0 | 2017-02-23 14:42:07 +0100 | [diff] [blame] | 4339 | net_vlan = net.pop("vlan", None) |
| 4340 | 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] | 4341 | |
| 4342 | #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network |
| 4343 | 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] | 4344 | #obtain datacenter_tenant_id |
| 4345 | datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',), |
| 4346 | FROM='datacenter_tenants', |
| 4347 | WHERE={'datacenter_id': datacenter})[0]['uuid'] |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4348 | try: |
| 4349 | sdn_network = {} |
| 4350 | sdn_network['vlan'] = net_vlan |
| 4351 | sdn_network['type'] = net_type |
| 4352 | sdn_network['name'] = net_name |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 4353 | sdn_network['region'] = datacenter_tenant_id |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4354 | ovim_content = ovim.new_network(sdn_network) |
| 4355 | except ovimException as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 4356 | logger.error("ovimException creating SDN network={} ".format( |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4357 | sdn_network) + str(e), exc_info=True) |
| 4358 | raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e), |
| 4359 | HTTP_Internal_Server_Error) |
| 4360 | |
| 4361 | # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id |
| 4362 | # use instance_scenario_id=None to distinguish from real instaces of nets |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 4363 | correspondence = {'instance_scenario_id': None, |
| 4364 | 'sdn_net_id': ovim_content, |
| 4365 | 'vim_net_id': content, |
| 4366 | 'datacenter_tenant_id': datacenter_tenant_id |
| 4367 | } |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4368 | try: |
| 4369 | mydb.new_row('instance_nets', correspondence, add_uuid=True) |
| 4370 | except db_base_Exception as e: |
| tierno | 00e3df7 | 2017-11-29 17:20:13 +0100 | [diff] [blame] | 4371 | raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format( |
| 4372 | correspondence, e), HTTP_Internal_Server_Error) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4373 | elif item=="tenants": |
| 4374 | tenant = descriptor["tenant"] |
| 4375 | content = myvim.new_tenant(tenant["name"], tenant.get("description")) |
| 4376 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 4377 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 4378 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 4379 | 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] | 4380 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 4381 | return vim_action_get(mydb, tenant_id, datacenter, item, content) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4382 | |
| 4383 | def sdn_controller_create(mydb, tenant_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4384 | data = ovim.new_of_controller(sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4385 | logger.debug('New SDN controller created with uuid {}'.format(data)) |
| 4386 | return data |
| 4387 | |
| 4388 | def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4389 | data = ovim.edit_of_controller(controller_id, sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4390 | msg = 'SDN controller {} updated'.format(data) |
| 4391 | logger.debug(msg) |
| 4392 | return msg |
| 4393 | |
| 4394 | def sdn_controller_list(mydb, tenant_id, controller_id=None): |
| 4395 | if controller_id == None: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4396 | data = ovim.get_of_controllers() |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4397 | else: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4398 | data = ovim.show_of_controller(controller_id) |
| 4399 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4400 | msg = 'SDN controller list:\n {}'.format(data) |
| 4401 | logger.debug(msg) |
| 4402 | return data |
| 4403 | |
| 4404 | def sdn_controller_delete(mydb, tenant_id, controller_id): |
| 4405 | select_ = ('uuid', 'config') |
| 4406 | datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_) |
| 4407 | for datacenter in datacenters: |
| 4408 | if datacenter['config']: |
| 4409 | config = yaml.load(datacenter['config']) |
| 4410 | if 'sdn-controller' in config and config['sdn-controller'] == controller_id: |
| 4411 | raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict) |
| 4412 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4413 | data = ovim.delete_of_controller(controller_id) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4414 | msg = 'SDN controller {} deleted'.format(data) |
| 4415 | logger.debug(msg) |
| 4416 | return msg |
| 4417 | |
| 4418 | def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping): |
| 4419 | controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id}) |
| 4420 | if len(controller) < 1: |
| 4421 | raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found) |
| 4422 | |
| 4423 | try: |
| 4424 | sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"] |
| 4425 | except: |
| 4426 | raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request) |
| 4427 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4428 | sdn_controller = ovim.show_of_controller(sdn_controller_id) |
| 4429 | switch_dpid = sdn_controller["dpid"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4430 | |
| 4431 | maps = list() |
| 4432 | for compute_node in sdn_port_mapping: |
| 4433 | #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid} |
| 4434 | element = dict() |
| 4435 | element["compute_node"] = compute_node["compute_node"] |
| 4436 | for port in compute_node["ports"]: |
| 4437 | element["pci"] = port.get("pci") |
| 4438 | element["switch_port"] = port.get("switch_port") |
| 4439 | element["switch_mac"] = port.get("switch_mac") |
| 4440 | if not element["pci"] or not (element["switch_port"] or element["switch_mac"]): |
| 4441 | raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'" |
| 4442 | " or 'switch_mac'", HTTP_Bad_Request) |
| 4443 | maps.append(dict(element)) |
| 4444 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4445 | 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] | 4446 | |
| 4447 | def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 4448 | maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id}) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4449 | |
| 4450 | result = { |
| 4451 | "sdn-controller": None, |
| 4452 | "datacenter-id": datacenter_id, |
| 4453 | "dpid": None, |
| 4454 | "ports_mapping": list() |
| 4455 | } |
| 4456 | |
| 4457 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id) |
| 4458 | if datacenter['config']: |
| 4459 | config = yaml.load(datacenter['config']) |
| 4460 | if 'sdn-controller' in config: |
| 4461 | controller_id = config['sdn-controller'] |
| 4462 | sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id) |
| 4463 | result["sdn-controller"] = controller_id |
| 4464 | result["dpid"] = sdn_controller["dpid"] |
| 4465 | |
| Pablo Montes Moreno | 6aa0b2b | 2017-05-23 18:33:12 +0200 | [diff] [blame] | 4466 | if result["sdn-controller"] == None: |
| 4467 | raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request) |
| 4468 | if result["dpid"] == None: |
| 4469 | raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]), |
| 4470 | HTTP_Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 4471 | |
| 4472 | if len(maps) == 0: |
| 4473 | return result |
| 4474 | |
| 4475 | ports_correspondence_dict = dict() |
| 4476 | for link in maps: |
| 4477 | if result["sdn-controller"] != link["ofc_id"]: |
| 4478 | raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 4479 | if result["dpid"] != link["switch_dpid"]: |
| 4480 | raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 4481 | element = dict() |
| 4482 | element["pci"] = link["pci"] |
| 4483 | if link["switch_port"]: |
| 4484 | element["switch_port"] = link["switch_port"] |
| 4485 | if link["switch_mac"]: |
| 4486 | element["switch_mac"] = link["switch_mac"] |
| 4487 | |
| 4488 | if not link["compute_node"] in ports_correspondence_dict: |
| 4489 | content = dict() |
| 4490 | content["compute_node"] = link["compute_node"] |
| 4491 | content["ports"] = list() |
| 4492 | ports_correspondence_dict[link["compute_node"]] = content |
| 4493 | |
| 4494 | ports_correspondence_dict[link["compute_node"]]["ports"].append(element) |
| 4495 | |
| 4496 | for key in sorted(ports_correspondence_dict): |
| 4497 | result["ports_mapping"].append(ports_correspondence_dict[key]) |
| 4498 | |
| 4499 | return result |
| 4500 | |
| 4501 | def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id): |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame] | 4502 | return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id}) |
| gcalvino | e580c7d | 2017-09-22 14:09:51 +0200 | [diff] [blame] | 4503 | |
| 4504 | def create_RO_keypair(tenant_id): |
| 4505 | """ |
| 4506 | Creates a public / private keys for a RO tenant and returns their values |
| 4507 | Params: |
| 4508 | tenant_id: ID of the tenant |
| 4509 | Return: |
| 4510 | public_key: Public key for the RO tenant |
| 4511 | private_key: Encrypted private key for RO tenant |
| 4512 | """ |
| 4513 | |
| 4514 | bits = 2048 |
| 4515 | key = RSA.generate(bits) |
| 4516 | try: |
| 4517 | public_key = key.publickey().exportKey('OpenSSH') |
| 4518 | if isinstance(public_key, ValueError): |
| 4519 | raise NfvoException("Unable to create public key: {}".format(public_key), HTTP_Internal_Server_Error) |
| 4520 | private_key = key.exportKey(passphrase=tenant_id, pkcs=8) |
| 4521 | except (ValueError, NameError) as e: |
| 4522 | raise NfvoException("Unable to create private key: {}".format(e), HTTP_Internal_Server_Error) |
| 4523 | return public_key, private_key |
| 4524 | |
| 4525 | def decrypt_key (key, tenant_id): |
| 4526 | """ |
| 4527 | Decrypts an encrypted RSA key |
| 4528 | Params: |
| 4529 | key: Private key to be decrypted |
| 4530 | tenant_id: ID of the tenant |
| 4531 | Return: |
| 4532 | unencrypted_key: Unencrypted private key for RO tenant |
| 4533 | """ |
| 4534 | try: |
| 4535 | key = RSA.importKey(key,tenant_id) |
| 4536 | unencrypted_key = key.exportKey('PEM') |
| 4537 | if isinstance(unencrypted_key, ValueError): |
| 4538 | raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), HTTP_Internal_Server_Error) |
| 4539 | except ValueError as e: |
| 4540 | raise NfvoException("Unable to decrypt the private key: {}".format(e), HTTP_Internal_Server_Error) |
| 4541 | return unencrypted_key |