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