| 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 | |
| 30 | import imp |
| 31 | #import json |
| 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 | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 41 | from db_base import db_base_Exception |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 42 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 43 | import nfvo_db |
| 44 | from threading import Lock |
| 45 | from time import time |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame^] | 46 | import ovim as ovim_module |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 47 | |
| 48 | global global_config |
| 49 | global vimconn_imported |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 50 | global logger |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 51 | global default_volume_size |
| 52 | default_volume_size = '5' #size in GB |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 53 | global ovim |
| 54 | ovim = None |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 55 | global_config = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 56 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 57 | vimconn_imported = {} # dictionary with VIM type as key, loaded module as value |
| 58 | vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 59 | vim_persistent_info = {} |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 60 | logger = logging.getLogger('openmano.nfvo') |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 61 | task_lock = Lock() |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 62 | global_instance_tasks = {} |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 63 | last_task_id = 0.0 |
| 64 | db=None |
| 65 | db_lock=Lock() |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 66 | |
| 67 | class NfvoException(Exception): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 68 | def __init__(self, message, http_code): |
| 69 | self.http_code = http_code |
| 70 | Exception.__init__(self, message) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 71 | |
| 72 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 73 | def get_task_id(): |
| 74 | global last_task_id |
| 75 | task_id = time() |
| 76 | if task_id <= last_task_id: |
| 77 | task_id = last_task_id + 0.000001 |
| 78 | last_task_id = task_id |
| 79 | return "TASK.{:.6f}".format(task_id) |
| 80 | |
| 81 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 82 | def new_task(name, params, depends=None): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 83 | task_id = get_task_id() |
| 84 | task = {"status": "enqueued", "id": task_id, "name": name, "params": params} |
| 85 | if depends: |
| 86 | task["depends"] = depends |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 87 | return task |
| 88 | |
| 89 | |
| 90 | def is_task_id(id): |
| 91 | return True if id[:5] == "TASK." else False |
| 92 | |
| 93 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 94 | def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id): |
| 95 | name = datacenter_name[:16] |
| 96 | if name not in vim_threads["names"]: |
| 97 | vim_threads["names"].append(name) |
| 98 | return name |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 99 | name = datacenter_name[:16] + "." + tenant_name[:16] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 100 | if name not in vim_threads["names"]: |
| 101 | vim_threads["names"].append(name) |
| 102 | return name |
| 103 | name = datacenter_id + "-" + tenant_id |
| 104 | vim_threads["names"].append(name) |
| 105 | return name |
| 106 | |
| 107 | |
| 108 | def start_service(mydb): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 109 | global db, global_config |
| 110 | db = nfvo_db.nfvo_db() |
| 111 | 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] | 112 | global ovim |
| 113 | |
| 114 | # Initialize openvim for SDN control |
| 115 | # TODO: Avoid static configuration by adding new parameters to openmanod.cfg |
| 116 | # TODO: review ovim.py to delete not needed configuration |
| 117 | ovim_configuration = { |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame^] | 118 | 'logger_name': 'openmano.ovim', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 119 | 'network_vlan_range_start': 1000, |
| 120 | 'network_vlan_range_end': 4096, |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame^] | 121 | 'db_name': global_config["db_ovim_name"], |
| 122 | 'db_host': global_config["db_ovim_host"], |
| 123 | 'db_user': global_config["db_ovim_user"], |
| 124 | 'db_passwd': global_config["db_ovim_passwd"], |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 125 | 'bridge_ifaces': {}, |
| 126 | 'mode': 'normal', |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 127 | 'network_type': 'bridge', |
| 128 | #TODO: log_level_of should not be needed. To be modified in ovim |
| 129 | 'log_level_of': 'DEBUG' |
| 130 | } |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame^] | 131 | ovim = ovim_module.ovim(ovim_configuration) |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 132 | ovim.start_service() |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 133 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 134 | 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' |
| 135 | select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name', |
| 136 | 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id', |
| 137 | 'user','passwd', 'dt.config as dt_config', 'nfvo_tenant_id') |
| 138 | try: |
| 139 | vims = mydb.get_rows(FROM=from_, SELECT=select_) |
| 140 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 141 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 142 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 143 | if vim["config"]: |
| 144 | extra.update(yaml.load(vim["config"])) |
| 145 | if vim.get('dt_config'): |
| 146 | extra.update(yaml.load(vim["dt_config"])) |
| 147 | if vim["type"] not in vimconn_imported: |
| 148 | module_info=None |
| 149 | try: |
| 150 | module = "vimconn_" + vim["type"] |
| 151 | module_info = imp.find_module(module) |
| 152 | vim_conn = imp.load_module(vim["type"], *module_info) |
| 153 | vimconn_imported[vim["type"]] = vim_conn |
| 154 | except (IOError, ImportError) as e: |
| 155 | if module_info and module_info[0]: |
| 156 | file.close(module_info[0]) |
| 157 | raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 158 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 159 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 160 | thread_id = vim['datacenter_tenant_id'] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 161 | vim_persistent_info[thread_id] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 162 | try: |
| 163 | #if not tenant: |
| 164 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 165 | myvim = vimconn_imported[ vim["type"] ].vimconnector( |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 166 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| 167 | tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'], |
| 168 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| 169 | user=vim['user'], passwd=vim['passwd'], |
| 170 | config=extra, persistent_info=vim_persistent_info[thread_id] |
| 171 | ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 172 | except Exception as e: |
| 173 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error) |
| 174 | thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], vim['vim_tenant_id']) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 175 | 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] | 176 | vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 177 | new_thread.start() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 178 | vim_threads["running"][thread_id] = new_thread |
| 179 | except db_base_Exception as e: |
| 180 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| 181 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 182 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 183 | def stop_service(): |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 184 | global ovim, global_config |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 185 | if ovim: |
| 186 | ovim.stop_service() |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 187 | for thread_id,thread in vim_threads["running"].items(): |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 188 | thread.insert_task(new_task("exit", None)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 189 | vim_threads["deleting"][thread_id] = thread |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 190 | vim_threads["running"] = {} |
| tierno | c565179 | 2017-03-27 10:50:43 +0200 | [diff] [blame] | 191 | if global_config and global_config.get("console_thread"): |
| 192 | for thread in global_config["console_thread"]: |
| 193 | thread.terminate = True |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 194 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 195 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 196 | def get_flavorlist(mydb, vnf_id, nfvo_tenant=None): |
| 197 | '''Obtain flavorList |
| 198 | return result, content: |
| 199 | <0, error_text upon error |
| 200 | nb_records, flavor_list on success |
| 201 | ''' |
| 202 | WHERE_dict={} |
| 203 | WHERE_dict['vnf_id'] = vnf_id |
| 204 | if nfvo_tenant is not None: |
| 205 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 206 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 207 | #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict ) |
| 208 | #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] | 209 | flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict ) |
| 210 | #print "get_flavor_list result:", result |
| 211 | #print "get_flavor_list content:", content |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 212 | flavorList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 213 | for flavor in flavors: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 214 | flavorList.append(flavor['flavor_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 215 | return flavorList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 216 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 217 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 218 | def get_imagelist(mydb, vnf_id, nfvo_tenant=None): |
| 219 | '''Obtain imageList |
| 220 | return result, content: |
| 221 | <0, error_text upon error |
| 222 | nb_records, flavor_list on success |
| 223 | ''' |
| 224 | WHERE_dict={} |
| 225 | WHERE_dict['vnf_id'] = vnf_id |
| 226 | if nfvo_tenant is not None: |
| 227 | WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 228 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 229 | #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] | 230 | 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] | 231 | imageList=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 232 | for image in images: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 233 | imageList.append(image['image_id']) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 234 | return imageList |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 235 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 236 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 237 | def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None, |
| 238 | vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 239 | '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 240 | return dictionary with {datacenter_id: vim_class, ... }. vim_class contain: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 241 | '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] | 242 | raise exception upon error |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 243 | ''' |
| 244 | WHERE_dict={} |
| 245 | if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant |
| 246 | if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 247 | 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] | 248 | if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name |
| 249 | 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] | 250 | if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name |
| 251 | 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] | 252 | 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] | 253 | 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] | 254 | '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] | 255 | 'user','passwd', 'dt.config as dt_config') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 256 | else: |
| 257 | from_ = 'datacenters as d' |
| 258 | 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] | 259 | try: |
| 260 | vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict ) |
| 261 | vim_dict={} |
| 262 | for vim in vims: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 263 | extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), |
| 264 | 'datacenter_id': vim.get('datacenter_id')} |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 265 | if vim["config"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 266 | extra.update(yaml.load(vim["config"])) |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 267 | if vim.get('dt_config'): |
| 268 | extra.update(yaml.load(vim["dt_config"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 269 | if vim["type"] not in vimconn_imported: |
| 270 | module_info=None |
| 271 | try: |
| 272 | module = "vimconn_" + vim["type"] |
| 273 | module_info = imp.find_module(module) |
| 274 | vim_conn = imp.load_module(vim["type"], *module_info) |
| 275 | vimconn_imported[vim["type"]] = vim_conn |
| 276 | except (IOError, ImportError) as e: |
| 277 | if module_info and module_info[0]: |
| 278 | file.close(module_info[0]) |
| 279 | raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( |
| 280 | vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 281 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 282 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 283 | if 'datacenter_tenant_id' in vim: |
| 284 | thread_id = vim["datacenter_tenant_id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 285 | if thread_id not in vim_persistent_info: |
| 286 | vim_persistent_info[thread_id] = {} |
| 287 | persistent_info = vim_persistent_info[thread_id] |
| 288 | else: |
| 289 | persistent_info = {} |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 290 | #if not tenant: |
| 291 | # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"]) |
| 292 | vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector( |
| 293 | uuid=vim['datacenter_id'], name=vim['datacenter_name'], |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 294 | tenant_id=vim.get('vim_tenant_id',vim_tenant), |
| 295 | tenant_name=vim.get('vim_tenant_name',vim_tenant_name), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 296 | url=vim['vim_url'], url_admin=vim['vim_url_admin'], |
| tierno | 3ae3974 | 2016-09-07 12:17:51 +0200 | [diff] [blame] | 297 | user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd), |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 298 | config=extra, persistent_info=persistent_info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 299 | ) |
| 300 | except Exception as e: |
| 301 | raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error) |
| 302 | return vim_dict |
| 303 | except db_base_Exception as e: |
| 304 | raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 305 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 306 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 307 | def rollback(mydb, vims, rollback_list): |
| 308 | undeleted_items=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 309 | #delete things by reverse order |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 310 | for i in range(len(rollback_list)-1, -1, -1): |
| 311 | item = rollback_list[i] |
| 312 | if item["where"]=="vim": |
| 313 | if item["vim_id"] not in vims: |
| 314 | continue |
| 315 | vim=vims[ item["vim_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 316 | try: |
| 317 | if item["what"]=="image": |
| 318 | vim.delete_image(item["uuid"]) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 319 | 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] | 320 | elif item["what"]=="flavor": |
| 321 | vim.delete_flavor(item["uuid"]) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 322 | 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] | 323 | elif item["what"]=="network": |
| 324 | vim.delete_network(item["uuid"]) |
| 325 | elif item["what"]=="vm": |
| 326 | vim.delete_vminstance(item["uuid"]) |
| 327 | except vimconn.vimconnException as e: |
| 328 | logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e)) |
| 329 | undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"])) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 330 | except db_base_Exception as e: |
| 331 | 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] | 332 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 333 | else: # where==mano |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 334 | try: |
| 335 | if item["what"]=="image": |
| 336 | mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]}) |
| 337 | elif item["what"]=="flavor": |
| 338 | mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]}) |
| 339 | except db_base_Exception as e: |
| 340 | logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e)) |
| 341 | undeleted_items.append("{} '{}'".format(item['what'], item["uuid"])) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 342 | if len(undeleted_items)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 343 | return True," Rollback successful." |
| 344 | else: |
| 345 | return False," Rollback fails to delete: " + str(undeleted_items) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 346 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 347 | |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 348 | def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 349 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 350 | #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 351 | vnfc_interfaces={} |
| 352 | for vnfc in vnf_descriptor["vnf"]["VNFC"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 353 | name_dict = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 354 | #dataplane interfaces |
| 355 | for numa in vnfc.get("numas",() ): |
| 356 | for interface in numa.get("interfaces",()): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 357 | if interface["name"] in name_dict: |
| 358 | raise NfvoException( |
| 359 | "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format( |
| 360 | vnfc["name"], interface["name"]), |
| 361 | HTTP_Bad_Request) |
| 362 | name_dict[ interface["name"] ] = "underlay" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 363 | #bridge interfaces |
| 364 | for interface in vnfc.get("bridge-ifaces",() ): |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 365 | if interface["name"] in name_dict: |
| 366 | raise NfvoException( |
| 367 | "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format( |
| 368 | vnfc["name"], interface["name"]), |
| 369 | HTTP_Bad_Request) |
| 370 | name_dict[ interface["name"] ] = "overlay" |
| 371 | vnfc_interfaces[ vnfc["name"] ] = name_dict |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 372 | # check bood-data info |
| 373 | if "boot-data" in vnfc: |
| 374 | # check that user-data is incompatible with users and config-files |
| 375 | if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"): |
| 376 | raise NfvoException( |
| 377 | "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'", |
| 378 | HTTP_Bad_Request) |
| 379 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 380 | #check if the info in external_connections matches with the one in the vnfcs |
| 381 | name_list=[] |
| 382 | for external_connection in vnf_descriptor["vnf"].get("external-connections",() ): |
| 383 | if external_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 384 | raise NfvoException( |
| 385 | "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format( |
| 386 | external_connection["name"]), |
| 387 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 388 | name_list.append(external_connection["name"]) |
| 389 | if external_connection["VNFC"] not in vnfc_interfaces: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 390 | raise NfvoException( |
| 391 | "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format( |
| 392 | external_connection["name"], external_connection["VNFC"]), |
| 393 | HTTP_Bad_Request) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 394 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 395 | if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 396 | raise NfvoException( |
| 397 | "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 398 | external_connection["name"], |
| 399 | external_connection["local_iface_name"]), |
| 400 | HTTP_Bad_Request ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 401 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 402 | #check if the info in internal_connections matches with the one in the vnfcs |
| 403 | name_list=[] |
| 404 | for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ): |
| 405 | if internal_connection["name"] in name_list: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 406 | raise NfvoException( |
| 407 | "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format( |
| 408 | internal_connection["name"]), |
| 409 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 410 | name_list.append(internal_connection["name"]) |
| 411 | #We should check that internal-connections of type "ptp" have only 2 elements |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 412 | |
| 413 | if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"): |
| 414 | raise NfvoException( |
| 415 | "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format( |
| 416 | internal_connection["name"], |
| 417 | 'ptp' if vnf_descriptor_version==1 else 'e-line', |
| 418 | 'data' if vnf_descriptor_version==1 else "e-lan"), |
| 419 | HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 420 | for port in internal_connection["elements"]: |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 421 | vnf = port["VNFC"] |
| 422 | iface = port["local_iface_name"] |
| 423 | if vnf not in vnfc_interfaces: |
| 424 | raise NfvoException( |
| 425 | "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format( |
| 426 | internal_connection["name"], vnf), |
| 427 | HTTP_Bad_Request) |
| 428 | if iface not in vnfc_interfaces[ vnf ]: |
| 429 | raise NfvoException( |
| 430 | "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format( |
| 431 | internal_connection["name"], iface), |
| 432 | HTTP_Bad_Request) |
| 433 | return -HTTP_Bad_Request, |
| 434 | if vnf_descriptor_version==1 and "type" not in internal_connection: |
| 435 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 436 | internal_connection["type"] = "bridge" |
| 437 | else: |
| 438 | internal_connection["type"] = "data" |
| 439 | if vnf_descriptor_version==2 and "implementation" not in internal_connection: |
| 440 | if vnfc_interfaces[vnf][iface] == "overlay": |
| 441 | internal_connection["implementation"] = "overlay" |
| 442 | else: |
| 443 | internal_connection["implementation"] = "underlay" |
| 444 | if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \ |
| 445 | internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay": |
| 446 | raise NfvoException( |
| 447 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 448 | internal_connection["name"], |
| 449 | iface, 'bridge' if vnf_descriptor_version==1 else 'overlay', |
| 450 | 'data' if vnf_descriptor_version==1 else 'underlay'), |
| 451 | HTTP_Bad_Request) |
| 452 | if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \ |
| 453 | vnfc_interfaces[vnf][iface] == "underlay": |
| 454 | raise NfvoException( |
| 455 | "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format( |
| 456 | internal_connection["name"], iface, |
| 457 | 'data' if vnf_descriptor_version==1 else 'underlay', |
| 458 | 'bridge' if vnf_descriptor_version==1 else 'overlay'), |
| 459 | HTTP_Bad_Request) |
| 460 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 461 | |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 462 | 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] | 463 | #look if image exist |
| 464 | if only_create_at_vim: |
| 465 | image_mano_id = image_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 466 | if return_on_error == None: |
| 467 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 468 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 469 | if image_dict['location']: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 470 | images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']}) |
| 471 | else: |
| 472 | 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] | 473 | if len(images)>=1: |
| 474 | image_mano_id = images[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 475 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 476 | #create image in MANO DB |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 477 | temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None), |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 478 | 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None), |
| 479 | 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 480 | } |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 481 | #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] | 482 | image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True) |
| 483 | rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 484 | #create image at every vim |
| 485 | for vim_id,vim in vims.iteritems(): |
| 486 | image_created="false" |
| 487 | #look at database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 488 | 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] | 489 | #look at VIM if this image exist |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 490 | try: |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 491 | if image_dict['location'] is not None: |
| 492 | image_vim_id = vim.get_image_id_from_path(image_dict['location']) |
| 493 | else: |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 494 | filter_dict = {} |
| 495 | filter_dict['name'] = image_dict['universal_name'] |
| 496 | if image_dict.get('checksum') != None: |
| 497 | filter_dict['checksum'] = image_dict['checksum'] |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 498 | #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 499 | vim_images = vim.get_image_list(filter_dict) |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 500 | #logger.debug('>>>>>>>> VIM images: %s', str(vim_images)) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 501 | if len(vim_images) > 1: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 502 | 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] | 503 | elif len(vim_images) == 0: |
| garciadeblas | 3fa2c05 | 2017-01-05 12:00:08 +0100 | [diff] [blame] | 504 | 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] | 505 | else: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 506 | #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0])) |
| 507 | image_vim_id = vim_images[0]['id'] |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 508 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 509 | except vimconn.vimconnNotFoundException as e: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 510 | #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] | 511 | try: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 512 | #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None |
| 513 | if image_dict['location']: |
| 514 | image_vim_id = vim.new_image(image_dict) |
| 515 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id}) |
| 516 | image_created="true" |
| 517 | else: |
| garciadeblas | b6153a2 | 2017-02-06 15:38:33 +0100 | [diff] [blame] | 518 | #If we reach this point, then the image has image name, and optionally checksum, and could not be found |
| 519 | raise vimconn.vimconnException(str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 520 | except vimconn.vimconnException as e: |
| 521 | if return_on_error: |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 522 | logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 523 | raise |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 524 | image_vim_id = None |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 525 | logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 526 | continue |
| 527 | except vimconn.vimconnException as e: |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 528 | if return_on_error: |
| 529 | logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e)) |
| 530 | raise |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 531 | 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] | 532 | image_vim_id = None |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 533 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 534 | #if we reach here, the image has been created or existed |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 535 | if len(image_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 536 | #add new vim_id at datacenters_images |
| 537 | mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created}) |
| 538 | elif image_db[0]["vim_id"]!=image_vim_id: |
| 539 | #modify existing vim_id at datacenters_images |
| 540 | 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] | 541 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 542 | return image_vim_id if only_create_at_vim else image_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 543 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 544 | |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 545 | 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] | 546 | temp_flavor_dict= {'disk':flavor_dict.get('disk',1), |
| 547 | 'ram':flavor_dict.get('ram'), |
| 548 | 'vcpus':flavor_dict.get('vcpus'), |
| 549 | } |
| 550 | if 'extended' in flavor_dict and flavor_dict['extended']==None: |
| 551 | del flavor_dict['extended'] |
| 552 | if 'extended' in flavor_dict: |
| 553 | temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256) |
| 554 | |
| 555 | #look if flavor exist |
| 556 | if only_create_at_vim: |
| 557 | flavor_mano_id = flavor_dict['uuid'] |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 558 | if return_on_error == None: |
| 559 | return_on_error = True |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 560 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 561 | flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict) |
| 562 | if len(flavors)>=1: |
| 563 | flavor_mano_id = flavors[0]['uuid'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 564 | else: |
| 565 | #create flavor |
| 566 | #create one by one the images of aditional disks |
| 567 | dev_image_list=[] #list of images |
| 568 | if 'extended' in flavor_dict and flavor_dict['extended']!=None: |
| 569 | dev_nb=0 |
| 570 | for device in flavor_dict['extended'].get('devices',[]): |
| garciadeblas | 41f18be | 2016-10-04 09:09:58 +0200 | [diff] [blame] | 571 | if "image" not in device and "image name" not in device: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 572 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 573 | image_dict={} |
| 574 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 575 | image_dict['universal_name']=device.get('image name') |
| 576 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 577 | image_dict['location']=device.get('image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 578 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 579 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 580 | image_metadata_dict = device.get('image metadata', None) |
| 581 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 582 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 583 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 584 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 585 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 586 | #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] | 587 | dev_image_list.append(image_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 588 | dev_nb += 1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 589 | temp_flavor_dict['name'] = flavor_dict['name'] |
| 590 | temp_flavor_dict['description'] = flavor_dict.get('description',None) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 591 | content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True) |
| 592 | flavor_mano_id= content |
| 593 | rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 594 | #create flavor at every vim |
| 595 | if 'uuid' in flavor_dict: |
| 596 | del flavor_dict['uuid'] |
| 597 | flavor_vim_id=None |
| 598 | for vim_id,vim in vims.items(): |
| 599 | flavor_created="false" |
| 600 | #look at database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 601 | 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] | 602 | #look at VIM if this flavor exist SKIPPED |
| 603 | #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location']) |
| 604 | #if res_vim < 0: |
| 605 | # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id |
| 606 | # continue |
| 607 | #elif res_vim==0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 608 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 609 | #Create the flavor in VIM |
| 610 | #Translate images at devices from MANO id to VIM id |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 611 | disk_list = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 612 | if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']: |
| 613 | #make a copy of original devices |
| 614 | devices_original=[] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 615 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 616 | for device in flavor_dict["extended"].get("devices",[]): |
| 617 | dev={} |
| 618 | dev.update(device) |
| 619 | devices_original.append(dev) |
| 620 | if 'image' in device: |
| 621 | del device['image'] |
| 622 | if 'image metadata' in device: |
| 623 | del device['image metadata'] |
| 624 | dev_nb=0 |
| 625 | for index in range(0,len(devices_original)) : |
| 626 | device=devices_original[index] |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 627 | if "image" not in device and "image name" not in device: |
| 628 | if 'size' in device: |
| 629 | disk_list.append({'size': device.get('size', default_volume_size)}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 630 | continue |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 631 | image_dict={} |
| 632 | image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img") |
| 633 | image_dict['universal_name']=device.get('image name') |
| 634 | image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img" |
| 635 | image_dict['location']=device.get('image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 636 | #image_dict['new_location']=device.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 637 | image_dict['checksum']=device.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 638 | image_metadata_dict = device.get('image metadata', None) |
| 639 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 640 | if image_metadata_dict != None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 641 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 642 | image_dict['metadata']=image_metadata_str |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 643 | 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] | 644 | image_dict["uuid"]=image_mano_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 645 | 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] | 646 | |
| 647 | #save disk information (image must be based on and size |
| 648 | disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)}) |
| 649 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 650 | flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id |
| 651 | dev_nb += 1 |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 652 | if len(flavor_db)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 653 | #check that this vim_id exist in VIM, if not create |
| 654 | flavor_vim_id=flavor_db[0]["vim_id"] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 655 | try: |
| 656 | vim.get_flavor(flavor_vim_id) |
| 657 | continue #flavor exist |
| 658 | except vimconn.vimconnException: |
| 659 | pass |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 660 | #create flavor at vim |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 661 | logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"]) |
| 662 | try: |
| tierno | cf157a8 | 2017-01-30 14:07:06 +0100 | [diff] [blame] | 663 | flavor_vim_id = None |
| 664 | flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict) |
| 665 | flavor_create="false" |
| 666 | except vimconn.vimconnException as e: |
| 667 | pass |
| 668 | try: |
| 669 | if not flavor_vim_id: |
| 670 | flavor_vim_id = vim.new_flavor(flavor_dict) |
| 671 | rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id}) |
| 672 | flavor_created="true" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 673 | except vimconn.vimconnException as e: |
| 674 | if return_on_error: |
| 675 | logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 676 | raise |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 677 | logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e)) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 678 | flavor_vim_id = None |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 679 | continue |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 680 | #if reach here the flavor has been create or exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 681 | if len(flavor_db)==0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 682 | #add new vim_id at datacenters_flavors |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 683 | extended_devices_yaml = None |
| 684 | if len(disk_list) > 0: |
| 685 | extended_devices = dict() |
| 686 | extended_devices['disks'] = disk_list |
| 687 | extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256) |
| 688 | mydb.new_row('datacenters_flavors', |
| 689 | {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id, |
| 690 | 'created':flavor_created,'extended': extended_devices_yaml}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 691 | elif flavor_db[0]["vim_id"]!=flavor_vim_id: |
| 692 | #modify existing vim_id at datacenters_flavors |
| 693 | 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] | 694 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 695 | return flavor_vim_id if only_create_at_vim else flavor_mano_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 696 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 697 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 698 | def new_vnf(mydb, tenant_id, vnf_descriptor): |
| 699 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 700 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 701 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 702 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 703 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 704 | vims = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 705 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 706 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 707 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 708 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 709 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 710 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 711 | else: |
| 712 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 713 | # 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] | 714 | if global_config["auto_push_VNF_to_VIMs"]: |
| 715 | vims = get_vim(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 716 | |
| 717 | # Step 4. Review the descriptor and add missing fields |
| 718 | #print vnf_descriptor |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 719 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 720 | vnf_name = vnf_descriptor['vnf']['name'] |
| 721 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 722 | if "physical" in vnf_descriptor['vnf']: |
| 723 | del vnf_descriptor['vnf']['physical'] |
| 724 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 725 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 726 | # 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] | 727 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 728 | 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] | 729 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 730 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 731 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 732 | 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] | 733 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 734 | 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] | 735 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 736 | VNFCitem={} |
| 737 | VNFCitem["name"] = vnfc['name'] |
| 738 | 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] | 739 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 740 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 741 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 742 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 743 | 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] | 744 | myflavorDict["description"] = VNFCitem["description"] |
| 745 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 746 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| 747 | myflavorDict["disk"] = vnfc.get("disk", 1) |
| 748 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 749 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 750 | devices = vnfc.get("devices") |
| 751 | if devices != None: |
| 752 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 753 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 754 | # TODO: |
| 755 | # 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] | 756 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 757 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 758 | # Previous code has been commented |
| 759 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 760 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 761 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 762 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 763 | #else: |
| 764 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 765 | # if result2: |
| 766 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 767 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 768 | # else: |
| 769 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 770 | 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] | 771 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 772 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 773 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 774 | |
| 775 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 776 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 777 | # Step 6.2 New flavors are created in the VIM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 778 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 779 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 780 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 781 | VNFCitem["flavor_id"] = flavor_id |
| 782 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 783 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 784 | logger.debug("Creating new images in the VIM for each VNFC") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 785 | # Step 6.3 New images are created in the VIM |
| 786 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 787 | #This "for" loop might be integrated with the previous one |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 788 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 789 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 790 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 791 | image_dict={} |
| 792 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 793 | image_dict['universal_name']=vnfc.get('image name') |
| 794 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 795 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 796 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 797 | image_dict['checksum']=vnfc.get('image checksum') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 798 | image_metadata_dict = vnfc.get('image metadata', None) |
| 799 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 800 | if image_metadata_dict is not None: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 801 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 802 | image_dict['metadata']=image_metadata_str |
| 803 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 804 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 805 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 806 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 807 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 808 | if vnfc.get("boot-data"): |
| 809 | 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] | 810 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 811 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 812 | # Step 7. Storing the VNF descriptor in the repository |
| 813 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 814 | 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] | 815 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 816 | # Step 8. Adding the VNF to the NFVO DB |
| 817 | vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 818 | return vnf_id |
| 819 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 820 | _, message = rollback(mydb, vims, rollback_list) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 821 | if isinstance(e, db_base_Exception): |
| 822 | error_text = "Exception at database" |
| 823 | elif isinstance(e, KeyError): |
| 824 | error_text = "KeyError exception " |
| 825 | e.http_code = HTTP_Internal_Server_Error |
| 826 | else: |
| 827 | error_text = "Exception at VIM" |
| 828 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 829 | #logger.error("start_scenario %s", error_text) |
| 830 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 831 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 832 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 833 | def new_vnf_v02(mydb, tenant_id, vnf_descriptor): |
| 834 | global global_config |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 835 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 836 | # Step 1. Check the VNF descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 837 | check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 838 | # Step 2. Check tenant exist |
| tierno | d29b1d3 | 2017-01-25 11:02:52 +0100 | [diff] [blame] | 839 | vims = {} |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 840 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 841 | check_tenant(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 842 | if "tenant_id" in vnf_descriptor["vnf"]: |
| 843 | if vnf_descriptor["vnf"]["tenant_id"] != tenant_id: |
| 844 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id), |
| 845 | HTTP_Unauthorized) |
| 846 | else: |
| 847 | vnf_descriptor['vnf']['tenant_id'] = tenant_id |
| 848 | # 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] | 849 | if global_config["auto_push_VNF_to_VIMs"]: |
| 850 | vims = get_vim(mydb, tenant_id) |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 851 | |
| 852 | # Step 4. Review the descriptor and add missing fields |
| 853 | #print vnf_descriptor |
| 854 | #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)") |
| 855 | vnf_name = vnf_descriptor['vnf']['name'] |
| 856 | vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name) |
| 857 | if "physical" in vnf_descriptor['vnf']: |
| 858 | del vnf_descriptor['vnf']['physical'] |
| 859 | #print vnf_descriptor |
| tierno | afed5f1 | 2017-01-26 17:57:43 +0100 | [diff] [blame] | 860 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 861 | # 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] | 862 | logger.debug('BEGIN creation of VNF "%s"' % vnf_name) |
| 863 | 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] | 864 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 865 | #For each VNFC, we add it to the VNFCDict and we create a flavor. |
| 866 | VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database |
| 867 | rollback_list = [] # It will contain the new images created in mano. It is used for rollback |
| 868 | try: |
| 869 | logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC") |
| 870 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 871 | VNFCitem={} |
| 872 | VNFCitem["name"] = vnfc['name'] |
| 873 | 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] | 874 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 875 | #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 876 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 877 | myflavorDict = {} |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 878 | 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] | 879 | myflavorDict["description"] = VNFCitem["description"] |
| 880 | myflavorDict["ram"] = vnfc.get("ram", 0) |
| 881 | myflavorDict["vcpus"] = vnfc.get("vcpus", 0) |
| 882 | myflavorDict["disk"] = vnfc.get("disk", 1) |
| 883 | myflavorDict["extended"] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 884 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 885 | devices = vnfc.get("devices") |
| 886 | if devices != None: |
| 887 | myflavorDict["extended"]["devices"] = devices |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 888 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 889 | # TODO: |
| 890 | # 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] | 891 | # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host |
| 892 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 893 | # Previous code has been commented |
| 894 | #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" : |
| 895 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 200 |
| 896 | #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" : |
| 897 | # myflavorDict["flavor"]['extended']['processor_ranking'] = 300 |
| 898 | #else: |
| 899 | # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList) |
| 900 | # if result2: |
| 901 | # print "Error creating flavor: unknown processor model. Rollback successful." |
| 902 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful." |
| 903 | # else: |
| 904 | # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message |
| 905 | 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] | 906 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 907 | if 'numas' in vnfc and len(vnfc['numas'])>0: |
| 908 | myflavorDict['extended']['numas'] = vnfc['numas'] |
| 909 | |
| 910 | #print myflavorDict |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 911 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 912 | # Step 6.2 New flavors are created in the VIM |
| 913 | flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list) |
| 914 | |
| 915 | #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id) |
| 916 | VNFCitem["flavor_id"] = flavor_id |
| 917 | VNFCDict[vnfc['name']] = VNFCitem |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 918 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 919 | logger.debug("Creating new images in the VIM for each VNFC") |
| 920 | # Step 6.3 New images are created in the VIM |
| 921 | #For each VNFC, we must create the appropriate image. |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 922 | #This "for" loop might be integrated with the previous one |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 923 | #In case this integration is made, the VNFCDict might become a VNFClist. |
| 924 | for vnfc in vnf_descriptor['vnf']['VNFC']: |
| 925 | #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description']) |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 926 | image_dict={} |
| 927 | image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img") |
| 928 | image_dict['universal_name']=vnfc.get('image name') |
| 929 | image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description']) |
| 930 | image_dict['location']=vnfc.get('VNFC image') |
| garciadeblas | 1448045 | 2017-01-10 13:08:07 +0100 | [diff] [blame] | 931 | #image_dict['new_location']=vnfc.get('image location') |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 932 | image_dict['checksum']=vnfc.get('image checksum') |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 933 | image_metadata_dict = vnfc.get('image metadata', None) |
| 934 | image_metadata_str = None |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 935 | if image_metadata_dict is not None: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 936 | image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256) |
| 937 | image_dict['metadata']=image_metadata_str |
| 938 | #print "create_or_use_image", mydb, vims, image_dict, rollback_list |
| 939 | image_id = create_or_use_image(mydb, vims, image_dict, rollback_list) |
| 940 | #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id) |
| 941 | VNFCDict[vnfc['name']]["image_id"] = image_id |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 942 | VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image') |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 943 | if vnfc.get("boot-data"): |
| 944 | 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] | 945 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 946 | # Step 7. Storing the VNF descriptor in the repository |
| 947 | if "descriptor" not in vnf_descriptor["vnf"]: |
| 948 | 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] | 949 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 950 | # Step 8. Adding the VNF to the NFVO DB |
| 951 | vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict) |
| 952 | return vnf_id |
| 953 | except (db_base_Exception, vimconn.vimconnException, KeyError) as e: |
| 954 | _, message = rollback(mydb, vims, rollback_list) |
| 955 | if isinstance(e, db_base_Exception): |
| 956 | error_text = "Exception at database" |
| 957 | elif isinstance(e, KeyError): |
| 958 | error_text = "KeyError exception " |
| 959 | e.http_code = HTTP_Internal_Server_Error |
| 960 | else: |
| 961 | error_text = "Exception at VIM" |
| 962 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 963 | #logger.error("start_scenario %s", error_text) |
| 964 | raise NfvoException(error_text, e.http_code) |
| 965 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 966 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 967 | def get_vnf_id(mydb, tenant_id, vnf_id): |
| 968 | #check valid tenant_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 969 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 970 | #obtain data |
| 971 | where_or = {} |
| 972 | if tenant_id != "any": |
| 973 | where_or["tenant_id"] = tenant_id |
| 974 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 975 | vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") |
| 976 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 977 | vnf_id=vnf["uuid"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 978 | filter_keys = ('uuid','name','description','public', "tenant_id", "created_at") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 979 | 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] | 980 | #change_keys_http2db(filtered_content, http2db_vnf, reverse=True) |
| 981 | data={'vnf' : filtered_content} |
| 982 | #GET VM |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 983 | content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id', |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 984 | SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description', 'boot_data'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 985 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 986 | if len(content)==0: |
| 987 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 988 | # change boot_data into boot-data |
| 989 | for vm in content: |
| 990 | if vm.get("boot_data"): |
| 991 | vm["boot-data"] = yaml.safe_load(vm["boot_data"]) |
| 992 | del vm["boot_data"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 993 | |
| 994 | data['vnf']['VNFC'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 995 | #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] | 996 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 997 | #GET NET |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 998 | 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] | 999 | SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'), |
| 1000 | WHERE={'vnfs.uuid': vnf_id} ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1001 | data['vnf']['nets'] = content |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1002 | |
| 1003 | #GET ip-profile for each net |
| 1004 | for net in data['vnf']['nets']: |
| 1005 | ipprofiles = mydb.get_rows(FROM='ip_profiles', |
| 1006 | SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'), |
| 1007 | WHERE={'net_id': net["uuid"]} ) |
| 1008 | if len(ipprofiles)==1: |
| 1009 | net["ip_profile"] = ipprofiles[0] |
| 1010 | elif len(ipprofiles)>1: |
| 1011 | 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] | 1012 | |
| 1013 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1014 | #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] | 1015 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1016 | #GET External Interfaces |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1017 | 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] | 1018 | SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \ |
| 1019 | '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] | 1020 | WHERE={'vnfs.uuid': vnf_id}, |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1021 | WHERE_NOT={'interfaces.external_name': None} ) |
| 1022 | #print content |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1023 | data['vnf']['external-connections'] = content |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1024 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1025 | return data |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1026 | |
| 1027 | |
| 1028 | def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None): |
| 1029 | # Check tenant exist |
| 1030 | if tenant_id != "any": |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1031 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1032 | # Get the URL of the VIM from the nfvo_tenant and the datacenter |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1033 | vims = get_vim(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1034 | else: |
| 1035 | vims={} |
| 1036 | |
| 1037 | # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided" |
| 1038 | where_or = {} |
| 1039 | if tenant_id != "any": |
| 1040 | where_or["tenant_id"] = tenant_id |
| 1041 | where_or["public"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1042 | 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] | 1043 | vnf_id = vnf["uuid"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1044 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1045 | # "Getting the list of flavors and tenants of the VNF" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1046 | flavorList = get_flavorlist(mydb, vnf_id) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1047 | if len(flavorList)==0: |
| 1048 | 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] | 1049 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1050 | imageList = get_imagelist(mydb, vnf_id) |
| 1051 | if len(imageList)==0: |
| 1052 | 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] | 1053 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1054 | deleted = mydb.delete_row_by_id('vnfs', vnf_id) |
| 1055 | if deleted == 0: |
| 1056 | raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1057 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1058 | undeletedItems = [] |
| 1059 | for flavor in flavorList: |
| 1060 | #check if flavor is used by other vnf |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1061 | try: |
| 1062 | c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} ) |
| 1063 | if len(c) > 0: |
| 1064 | logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor) |
| 1065 | continue |
| 1066 | #flavor not used, must be deleted |
| 1067 | #delelte at VIM |
| 1068 | c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1069 | for flavor_vim in c: |
| 1070 | if flavor_vim["datacenter_id"] not in vims: |
| 1071 | continue |
| 1072 | if flavor_vim['created']=='false': #skip this flavor because not created by openmano |
| 1073 | continue |
| 1074 | myvim=vims[ flavor_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1075 | try: |
| 1076 | myvim.delete_flavor(flavor_vim["vim_id"]) |
| 1077 | except vimconn.vimconnNotFoundException as e: |
| 1078 | logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] ) |
| 1079 | except vimconn.vimconnException as e: |
| 1080 | logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s", |
| 1081 | flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1082 | 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] | 1083 | #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors |
| 1084 | mydb.delete_row_by_id('flavors', flavor) |
| 1085 | except db_base_Exception as e: |
| 1086 | 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] | 1087 | undeletedItems.append("flavor %s" % flavor) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1088 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1089 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1090 | for image in imageList: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1091 | try: |
| 1092 | #check if image is used by other vnf |
| 1093 | c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} ) |
| 1094 | if len(c) > 0: |
| 1095 | logger.debug("Image '%s' not deleted because it is being used by another VNF", image) |
| 1096 | continue |
| 1097 | #image not used, must be deleted |
| 1098 | #delelte at VIM |
| 1099 | c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image}) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1100 | for image_vim in c: |
| 1101 | if image_vim["datacenter_id"] not in vims: |
| 1102 | continue |
| 1103 | if image_vim['created']=='false': #skip this image because not created by openmano |
| 1104 | continue |
| 1105 | myvim=vims[ image_vim["datacenter_id"] ] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1106 | try: |
| 1107 | myvim.delete_image(image_vim["vim_id"]) |
| 1108 | except vimconn.vimconnNotFoundException as e: |
| 1109 | logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] ) |
| 1110 | except vimconn.vimconnException as e: |
| 1111 | logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s", |
| 1112 | image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e)) |
| 1113 | 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] | 1114 | #delete image from Database, using table images and with cascade foreign key also at datacenters_images |
| 1115 | mydb.delete_row_by_id('images', image) |
| 1116 | except db_base_Exception as e: |
| 1117 | 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] | 1118 | undeletedItems.append("image %s" % image) |
| 1119 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1120 | return vnf_id + " " + vnf["name"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1121 | #if undeletedItems: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1122 | # return "delete_vnf. Undeleted: %s" %(undeletedItems) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1123 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1124 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1125 | def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None): |
| 1126 | result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name) |
| 1127 | if result < 0: |
| 1128 | return result, vims |
| 1129 | elif result == 0: |
| 1130 | return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name |
| 1131 | myvim = vims.values()[0] |
| 1132 | result,servers = myvim.get_hosts_info() |
| 1133 | if result < 0: |
| 1134 | return result, servers |
| 1135 | topology = {'name':myvim['name'] , 'servers': servers} |
| 1136 | return result, topology |
| 1137 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1138 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1139 | def get_hosts(mydb, nfvo_tenant_id): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1140 | vims = get_vim(mydb, nfvo_tenant_id) |
| 1141 | if len(vims) == 0: |
| 1142 | raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found) |
| 1143 | elif len(vims)>1: |
| 1144 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 1145 | 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] | 1146 | myvim = vims.values()[0] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1147 | try: |
| 1148 | hosts = myvim.get_hosts() |
| 1149 | 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] | 1150 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1151 | datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] } |
| 1152 | for host in hosts: |
| 1153 | server={'name':host['name'], 'vms':[]} |
| 1154 | for vm in host['instances']: |
| 1155 | #get internal name and model |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1156 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1157 | c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\ |
| 1158 | WHERE={'vim_vm_id':vm['id']} ) |
| 1159 | if len(c) == 0: |
| 1160 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id'])) |
| 1161 | continue |
| 1162 | server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1163 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1164 | except db_base_Exception as e: |
| 1165 | logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e))) |
| 1166 | datacenter['Datacenters'][0]['servers'].append(server) |
| 1167 | #return -400, "en construccion" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1168 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1169 | #print 'datacenters '+ json.dumps(datacenter, indent=4) |
| 1170 | return datacenter |
| 1171 | except vimconn.vimconnException as e: |
| 1172 | 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] | 1173 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1174 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1175 | def new_scenario(mydb, tenant_id, topo): |
| 1176 | |
| 1177 | # result, vims = get_vim(mydb, tenant_id) |
| 1178 | # if result < 0: |
| 1179 | # return result, vims |
| 1180 | #1: parse input |
| 1181 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1182 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1183 | if "tenant_id" in topo: |
| 1184 | if topo["tenant_id"] != tenant_id: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1185 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id), |
| 1186 | HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1187 | else: |
| 1188 | tenant_id=None |
| 1189 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1190 | #1.1: get VNFs and external_networks (other_nets). |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1191 | vnfs={} |
| 1192 | other_nets={} #external_networks, bridge_networks and data_networkds |
| 1193 | nodes = topo['topology']['nodes'] |
| 1194 | for k in nodes.keys(): |
| 1195 | if nodes[k]['type'] == 'VNF': |
| 1196 | vnfs[k] = nodes[k] |
| 1197 | vnfs[k]['ifaces'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1198 | elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1199 | other_nets[k] = nodes[k] |
| 1200 | other_nets[k]['external']=True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1201 | elif nodes[k]['type'] == 'network': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1202 | other_nets[k] = nodes[k] |
| 1203 | other_nets[k]['external']=False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1204 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1205 | |
| 1206 | #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces |
| 1207 | for name,vnf in vnfs.items(): |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1208 | where={} |
| 1209 | where_or={"tenant_id": tenant_id, 'public': "true"} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1210 | error_text = "" |
| 1211 | error_pos = "'topology':'nodes':'" + name + "'" |
| 1212 | if 'vnf_id' in vnf: |
| 1213 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1214 | where['uuid'] = vnf['vnf_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1215 | if 'VNF model' in vnf: |
| 1216 | error_text += " 'VNF model' " + vnf['VNF model'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1217 | where['name'] = vnf['VNF model'] |
| 1218 | if len(where) == 0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1219 | 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] | 1220 | |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1221 | vnf_db = mydb.get_rows(SELECT=('uuid','name','description'), |
| 1222 | FROM='vnfs', |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1223 | WHERE=where, |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1224 | WHERE_OR=where_or, |
| 1225 | WHERE_AND_OR="AND") |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1226 | if len(vnf_db)==0: |
| 1227 | raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| 1228 | elif len(vnf_db)>1: |
| 1229 | 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] | 1230 | vnf['uuid']=vnf_db[0]['uuid'] |
| 1231 | vnf['description']=vnf_db[0]['description'] |
| 1232 | #get external interfaces |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1233 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), |
| 1234 | 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] | 1235 | WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1236 | for ext_iface in ext_ifaces: |
| 1237 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']} |
| 1238 | |
| 1239 | #1.4 get list of connections |
| 1240 | conections = topo['topology']['connections'] |
| 1241 | conections_list = [] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1242 | conections_list_name = [] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1243 | for k in conections.keys(): |
| 1244 | if type(conections[k]['nodes'])==dict: #dict with node:iface pairs |
| 1245 | ifaces_list = conections[k]['nodes'].items() |
| 1246 | elif type(conections[k]['nodes'])==list: #list with dictionary |
| 1247 | ifaces_list=[] |
| 1248 | conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] ) |
| 1249 | for k2 in conection_pair_list: |
| 1250 | ifaces_list += k2 |
| 1251 | |
| 1252 | con_type = conections[k].get("type", "link") |
| 1253 | if con_type != "link": |
| 1254 | if k in other_nets: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1255 | 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] | 1256 | other_nets[k] = {'external': False} |
| 1257 | if conections[k].get("graph"): |
| 1258 | other_nets[k]["graph"] = conections[k]["graph"] |
| 1259 | ifaces_list.append( (k, None) ) |
| 1260 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1261 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1262 | if con_type == "external_network": |
| 1263 | other_nets[k]['external'] = True |
| 1264 | if conections[k].get("model"): |
| 1265 | other_nets[k]["model"] = conections[k]["model"] |
| 1266 | else: |
| 1267 | other_nets[k]["model"] = k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1268 | if con_type == "dataplane_net" or con_type == "bridge_net": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1269 | other_nets[k]["model"] = con_type |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1270 | |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1271 | conections_list_name.append(k) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1272 | 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) |
| 1273 | #print set(ifaces_list) |
| 1274 | #check valid VNF and iface names |
| 1275 | for iface in ifaces_list: |
| 1276 | if iface[0] not in vnfs and iface[0] not in other_nets : |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1277 | raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format( |
| 1278 | str(k), iface[0]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1279 | 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] | 1280 | raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format( |
| 1281 | str(k), iface[0], iface[1]), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1282 | |
| 1283 | #1.5 unify connections from the pair list to a consolidated list |
| 1284 | index=0 |
| 1285 | while index < len(conections_list): |
| 1286 | index2 = index+1 |
| 1287 | while index2 < len(conections_list): |
| 1288 | if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets |
| 1289 | conections_list[index] |= conections_list[index2] |
| 1290 | del conections_list[index2] |
| tierno | efd80c9 | 2016-09-16 14:17:46 +0200 | [diff] [blame] | 1291 | del conections_list_name[index2] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1292 | else: |
| 1293 | index2 += 1 |
| 1294 | conections_list[index] = list(conections_list[index]) # from set to list again |
| 1295 | index += 1 |
| 1296 | #for k in conections_list: |
| 1297 | # print k |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1298 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1299 | |
| 1300 | |
| 1301 | #1.6 Delete non external nets |
| 1302 | # for k in other_nets.keys(): |
| 1303 | # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net': |
| 1304 | # for con in conections_list: |
| 1305 | # delete_indexes=[] |
| 1306 | # for index in range(0,len(con)): |
| 1307 | # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower |
| 1308 | # for index in delete_indexes: |
| 1309 | # del con[index] |
| 1310 | # del other_nets[k] |
| 1311 | #1.7: Check external_ports are present at database table datacenter_nets |
| 1312 | for k,net in other_nets.items(): |
| 1313 | error_pos = "'topology':'nodes':'" + k + "'" |
| 1314 | if net['external']==False: |
| 1315 | if 'name' not in net: |
| 1316 | net['name']=k |
| 1317 | if 'model' not in net: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1318 | raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1319 | if net['model']=='bridge_net': |
| 1320 | net['type']='bridge'; |
| 1321 | elif net['model']=='dataplane_net': |
| 1322 | net['type']='data'; |
| 1323 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1324 | raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1325 | else: #external |
| 1326 | #IF we do not want to check that external network exist at datacenter |
| 1327 | pass |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1328 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1329 | # error_text = "" |
| 1330 | # WHERE_={} |
| 1331 | # if 'net_id' in net: |
| 1332 | # error_text += " 'net_id' " + net['net_id'] |
| 1333 | # WHERE_['uuid'] = net['net_id'] |
| 1334 | # if 'model' in net: |
| 1335 | # error_text += " 'model' " + net['model'] |
| 1336 | # WHERE_['name'] = net['model'] |
| 1337 | # if len(WHERE_) == 0: |
| 1338 | # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos |
| 1339 | # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'), |
| 1340 | # FROM='datacenter_nets', WHERE=WHERE_ ) |
| 1341 | # if r<0: |
| 1342 | # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db |
| 1343 | # elif r==0: |
| 1344 | # print "nfvo.new_scenario Error" +error_text+ " is not present at database" |
| 1345 | # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos |
| 1346 | # elif r>1: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1347 | # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database" |
| 1348 | # 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] | 1349 | # other_nets[k].update(net_db[0]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1350 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1351 | net_list={} |
| 1352 | net_nb=0 #Number of nets |
| 1353 | for con in conections_list: |
| 1354 | #check if this is connected to a external net |
| 1355 | other_net_index=-1 |
| 1356 | #print |
| 1357 | #print "con", con |
| 1358 | for index in range(0,len(con)): |
| 1359 | #check if this is connected to a external net |
| 1360 | for net_key in other_nets.keys(): |
| 1361 | if con[index][0]==net_key: |
| 1362 | if other_net_index>=0: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1363 | 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] | 1364 | #print "nfvo.new_scenario " + error_text |
| 1365 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1366 | else: |
| 1367 | other_net_index = index |
| 1368 | net_target = net_key |
| 1369 | break |
| 1370 | #print "other_net_index", other_net_index |
| 1371 | try: |
| 1372 | if other_net_index>=0: |
| 1373 | del con[other_net_index] |
| 1374 | #IF we do not want to check that external network exist at datacenter |
| 1375 | if other_nets[net_target]['external'] : |
| 1376 | if "name" not in other_nets[net_target]: |
| 1377 | other_nets[net_target]['name'] = other_nets[net_target]['model'] |
| 1378 | if other_nets[net_target]["type"] == "external_network": |
| 1379 | if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data": |
| 1380 | other_nets[net_target]["type"] = "data" |
| 1381 | else: |
| 1382 | other_nets[net_target]["type"] = "bridge" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1383 | #ELSE |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1384 | # if other_nets[net_target]['external'] : |
| 1385 | # 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 |
| 1386 | # if type_=='data' and other_nets[net_target]['type']=="ptp": |
| 1387 | # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target) |
| 1388 | # print "nfvo.new_scenario " + error_text |
| 1389 | # return -HTTP_Bad_Request, error_text |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1390 | #ENDIF |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1391 | for iface in con: |
| 1392 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 1393 | else: |
| 1394 | #create a net |
| 1395 | net_type_bridge=False |
| 1396 | net_type_data=False |
| 1397 | net_target = "__-__net"+str(net_nb) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1398 | 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] | 1399 | 'description':"net-%s in scenario %s" %(net_nb,topo['name']), |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1400 | 'external':False} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1401 | for iface in con: |
| 1402 | vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target |
| 1403 | iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type'] |
| 1404 | if iface_type=='mgmt' or iface_type=='bridge': |
| 1405 | net_type_bridge = True |
| 1406 | else: |
| 1407 | net_type_data = True |
| 1408 | if net_type_bridge and net_type_data: |
| 1409 | 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] | 1410 | #print "nfvo.new_scenario " + error_text |
| 1411 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1412 | elif net_type_bridge: |
| 1413 | type_='bridge' |
| 1414 | else: |
| 1415 | type_='data' if len(con)>2 else 'ptp' |
| 1416 | net_list[net_target]['type'] = type_ |
| 1417 | net_nb+=1 |
| 1418 | except Exception: |
| 1419 | 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] | 1420 | #print "nfvo.new_scenario " + error_text |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1421 | #raise e |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1422 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1423 | |
| 1424 | #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] | 1425 | #1.8.1 obtain management net |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1426 | mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'), |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1427 | FROM='datacenter_nets', WHERE={'name':'mgmt'} ) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1428 | #1.8.2 check all interfaces from all vnfs |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1429 | if len(mgmt_net)>0: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1430 | add_mgmt_net = False |
| 1431 | for vnf in vnfs.values(): |
| 1432 | for iface in vnf['ifaces'].values(): |
| 1433 | if iface['type']=='mgmt' and 'net_key' not in iface: |
| 1434 | #iface not connected |
| 1435 | iface['net_key'] = 'mgmt' |
| 1436 | add_mgmt_net = True |
| 1437 | if add_mgmt_net and 'mgmt' not in net_list: |
| 1438 | net_list['mgmt']=mgmt_net[0] |
| 1439 | net_list['mgmt']['external']=True |
| 1440 | net_list['mgmt']['graph']={'visible':False} |
| 1441 | |
| 1442 | net_list.update(other_nets) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1443 | #print |
| 1444 | #print 'net_list', net_list |
| 1445 | #print |
| 1446 | #print 'vnfs', vnfs |
| 1447 | #print |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1448 | |
| 1449 | #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1450 | c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list, |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1451 | 'tenant_id':tenant_id, 'name':topo['name'], |
| 1452 | 'description':topo.get('description',topo['name']), |
| 1453 | 'public': topo.get('public', False) |
| 1454 | }) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1455 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1456 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1457 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1458 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1459 | def new_scenario_v02(mydb, tenant_id, scenario_dict, version): |
| 1460 | """ This creates a new scenario for version 0.2 and 0.3""" |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1461 | scenario = scenario_dict["scenario"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1462 | if tenant_id != "any": |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1463 | check_tenant(mydb, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1464 | if "tenant_id" in scenario: |
| 1465 | if scenario["tenant_id"] != tenant_id: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1466 | # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1467 | raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format( |
| 1468 | scenario["tenant_id"], tenant_id), HTTP_Unauthorized) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1469 | else: |
| 1470 | tenant_id=None |
| 1471 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1472 | # 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] | 1473 | for name,vnf in scenario["vnfs"].iteritems(): |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1474 | where={} |
| 1475 | where_or={"tenant_id": tenant_id, 'public': "true"} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1476 | error_text = "" |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 1477 | error_pos = "'scenario':'vnfs':'" + name + "'" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1478 | if 'vnf_id' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1479 | error_text += " 'vnf_id' " + vnf['vnf_id'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1480 | where['uuid'] = vnf['vnf_id'] |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1481 | if 'vnf_name' in vnf: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1482 | error_text += " 'vnf_name' " + vnf['vnf_name'] |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1483 | where['name'] = vnf['vnf_name'] |
| 1484 | if len(where) == 0: |
| garciadeblas | 71781ea | 2016-09-19 14:41:59 +0200 | [diff] [blame] | 1485 | 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] | 1486 | vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'), |
| tierno | cea279c | 2016-07-18 12:36:49 +0200 | [diff] [blame] | 1487 | FROM='vnfs', |
| 1488 | WHERE=where, |
| 1489 | WHERE_OR=where_or, |
| 1490 | WHERE_AND_OR="AND") |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1491 | if len(vnf_db) == 0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1492 | raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found) |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1493 | elif len(vnf_db) > 1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1494 | 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] | 1495 | vnf['uuid'] = vnf_db[0]['uuid'] |
| 1496 | vnf['description'] = vnf_db[0]['description'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1497 | vnf['ifaces'] = {} |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1498 | # get external interfaces |
| 1499 | ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'), |
| 1500 | FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id', |
| 1501 | WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name': None} ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1502 | for ext_iface in ext_ifaces: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1503 | vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']} |
| 1504 | # 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] | 1505 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1506 | # 2: Insert net_key and ip_address at every vnf interface |
| 1507 | for net_name, net in scenario["networks"].items(): |
| 1508 | net_type_bridge = False |
| 1509 | net_type_data = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1510 | for iface_dict in net["interfaces"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1511 | if version == "0.2": |
| 1512 | temp_dict = iface_dict |
| 1513 | ip_address = None |
| 1514 | elif version == "0.3": |
| 1515 | temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]} |
| 1516 | ip_address = iface_dict.get('ip_address', None) |
| 1517 | for vnf, iface in temp_dict.items(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1518 | if vnf not in scenario["vnfs"]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1519 | error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format( |
| 1520 | net_name, vnf) |
| 1521 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1522 | raise NfvoException(error_text, HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1523 | if iface not in scenario["vnfs"][vnf]['ifaces']: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1524 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\ |
| 1525 | .format(net_name, iface) |
| 1526 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1527 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1528 | if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1529 | error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\ |
| 1530 | "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key']) |
| 1531 | # logger.debug("nfvo.new_scenario_v02 " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1532 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1533 | scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1534 | scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1535 | iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type'] |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1536 | if iface_type == 'mgmt' or iface_type == 'bridge': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1537 | net_type_bridge = True |
| 1538 | else: |
| 1539 | net_type_data = True |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1540 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1541 | if net_type_bridge and net_type_data: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1542 | error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\ |
| 1543 | .format(net_name) |
| 1544 | # logger.debug("nfvo.new_scenario " + error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1545 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1546 | elif net_type_bridge: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1547 | type_ = 'bridge' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1548 | else: |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1549 | type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp' |
| 1550 | |
| 1551 | if net.get("implementation"): # for v0.3 |
| 1552 | if type_ == "bridge" and net["implementation"] == "underlay": |
| 1553 | error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\ |
| 1554 | "'network':'{}'".format(net_name) |
| 1555 | # logger.debug(error_text) |
| 1556 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 1557 | elif type_ != "bridge" and net["implementation"] == "overlay": |
| 1558 | error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\ |
| 1559 | "'network':'{}'".format(net_name) |
| 1560 | # logger.debug(error_text) |
| 1561 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 1562 | net.pop("implementation") |
| 1563 | if "type" in net and version == "0.3": # for v0.3 |
| 1564 | if type_ == "data" and net["type"] == "e-line": |
| 1565 | error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\ |
| 1566 | "'e-line' at 'network':'{}'".format(net_name) |
| 1567 | # logger.debug(error_text) |
| 1568 | raise NfvoException(error_text, HTTP_Bad_Request) |
| 1569 | elif type_ == "ptp" and net["type"] == "e-lan": |
| 1570 | type_ = "data" |
| 1571 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1572 | net['type'] = type_ |
| 1573 | net['name'] = net_name |
| 1574 | net['external'] = net.get('external', False) |
| 1575 | |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1576 | # 3: insert at database |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1577 | scenario["nets"] = scenario["networks"] |
| 1578 | scenario['tenant_id'] = tenant_id |
| tierno | 5bb59dc | 2017-02-13 14:53:54 +0100 | [diff] [blame] | 1579 | scenario_id = mydb.new_scenario(scenario) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1580 | return scenario_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1581 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1582 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1583 | def edit_scenario(mydb, tenant_id, scenario_id, data): |
| 1584 | data["uuid"] = scenario_id |
| 1585 | data["tenant_id"] = tenant_id |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1586 | c = mydb.edit_scenario( data ) |
| 1587 | return c |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1588 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1589 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1590 | 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] | 1591 | #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] | 1592 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant) |
| 1593 | vims = {datacenter_id: myvim} |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1594 | myvim_tenant = myvim['tenant_id'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1595 | datacenter_name = myvim['name'] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 1596 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1597 | rollbackList=[] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1598 | try: |
| 1599 | #print "Checking that the scenario_id exists and getting the scenario dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1600 | scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 1601 | scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] } |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1602 | scenarioDict['datacenter_id'] = datacenter_id |
| 1603 | #print '================scenarioDict=======================' |
| 1604 | #print json.dumps(scenarioDict, indent=4) |
| 1605 | #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] | 1606 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1607 | logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs'])) |
| 1608 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1609 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1610 | auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id |
| 1611 | auxNetDict['scenario'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1612 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1613 | logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM") |
| 1614 | for sce_net in scenarioDict['nets']: |
| 1615 | #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1616 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1617 | myNetName = "%s.%s" % (instance_scenario_name, sce_net['name']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1618 | myNetName = myNetName[0:255] #limit length |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1619 | myNetType = sce_net['type'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1620 | myNetDict = {} |
| 1621 | myNetDict["name"] = myNetName |
| 1622 | myNetDict["type"] = myNetType |
| 1623 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1624 | myNetIPProfile = sce_net.get('ip_profile', None) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1625 | #TODO: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1626 | #We should use the dictionary as input parameter for new_network |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1627 | #print myNetDict |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1628 | if not sce_net["external"]: |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1629 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1630 | #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id) |
| 1631 | sce_net['vim_id'] = network_id |
| 1632 | auxNetDict['scenario'][sce_net['uuid']] = network_id |
| 1633 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 1634 | sce_net["created"] = True |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1635 | else: |
| 1636 | if sce_net['vim_id'] == None: |
| 1637 | error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name']) |
| 1638 | _, message = rollback(mydb, vims, rollbackList) |
| 1639 | logger.error("nfvo.start_scenario: %s", error_text) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1640 | raise NfvoException(error_text, HTTP_Bad_Request) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1641 | logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id']) |
| 1642 | auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1643 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1644 | logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM") |
| 1645 | #For each vnf net, we create it and we add it to instanceNetlist. |
| 1646 | for sce_vnf in scenarioDict['vnfs']: |
| 1647 | for net in sce_vnf['nets']: |
| 1648 | #print "Net name: %s. Description: %s" % (net["name"], net["description"]) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1649 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1650 | myNetName = "%s.%s" % (instance_scenario_name,net['name']) |
| 1651 | myNetName = myNetName[0:255] #limit length |
| 1652 | myNetType = net['type'] |
| 1653 | myNetDict = {} |
| 1654 | myNetDict["name"] = myNetName |
| 1655 | myNetDict["type"] = myNetType |
| 1656 | myNetDict["tenant_id"] = myvim_tenant |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1657 | myNetIPProfile = net.get('ip_profile', None) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1658 | #print myNetDict |
| 1659 | #TODO: |
| 1660 | #We should use the dictionary as input parameter for new_network |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1661 | network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1662 | #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id) |
| 1663 | net['vim_id'] = network_id |
| 1664 | if sce_vnf['uuid'] not in auxNetDict: |
| 1665 | auxNetDict[sce_vnf['uuid']] = {} |
| 1666 | auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id |
| 1667 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 1668 | net["created"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1669 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1670 | #print "auxNetDict:" |
| 1671 | #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1672 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1673 | logger.debug("start_scenario 3. Creating new vm instances in the VIM") |
| 1674 | #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| 1675 | i = 0 |
| 1676 | for sce_vnf in scenarioDict['vnfs']: |
| 1677 | for vm in sce_vnf['vms']: |
| 1678 | i += 1 |
| 1679 | myVMDict = {} |
| 1680 | #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name']) |
| tierno | ae65a48 | 2016-11-24 16:20:05 +0100 | [diff] [blame] | 1681 | myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i)) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1682 | #myVMDict['description'] = vm['description'] |
| 1683 | myVMDict['description'] = myVMDict['name'][0:99] |
| 1684 | if not startvms: |
| 1685 | myVMDict['start'] = "no" |
| 1686 | myVMDict['name'] = myVMDict['name'][0:255] #limit name length |
| 1687 | #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1688 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1689 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1690 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1691 | image_id = create_or_use_image(mydb, vims, image_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1692 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1693 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1694 | #create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1695 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1696 | if flavor_dict['extended']!=None: |
| 1697 | flavor_dict['extended']= yaml.load(flavor_dict['extended']) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1698 | flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1699 | vm['vim_flavor_id'] = flavor_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1700 | |
| 1701 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1702 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 1703 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 1704 | myVMDict['networks'] = [] |
| 1705 | for iface in vm['interfaces']: |
| 1706 | netDict = {} |
| 1707 | if iface['type']=="data": |
| 1708 | netDict['type'] = iface['model'] |
| 1709 | elif "model" in iface and iface["model"]!=None: |
| 1710 | netDict['model']=iface['model'] |
| 1711 | #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model |
| 1712 | #discover type of interface looking at flavor |
| 1713 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 1714 | for flavor_iface in numa.get('interfaces',[]): |
| 1715 | if flavor_iface.get('name') == iface['internal_name']: |
| 1716 | if flavor_iface['dedicated'] == 'yes': |
| 1717 | netDict['type']="PF" #passthrough |
| 1718 | elif flavor_iface['dedicated'] == 'no': |
| 1719 | netDict['type']="VF" #siov |
| 1720 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 1721 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 1722 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 1723 | break; |
| 1724 | netDict["use"]=iface['type'] |
| 1725 | if netDict["use"]=="data" and not netDict.get("type"): |
| 1726 | #print "netDict", netDict |
| 1727 | #print "iface", iface |
| 1728 | 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']) |
| 1729 | if flavor_dict.get('extended')==None: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1730 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 1731 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1732 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1733 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1734 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 1735 | netDict["type"]="virtual" |
| 1736 | if "vpci" in iface and iface["vpci"] is not None: |
| 1737 | netDict['vpci'] = iface['vpci'] |
| 1738 | if "mac" in iface and iface["mac"] is not None: |
| 1739 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 1740 | if "port-security" in iface and iface["port-security"] is not None: |
| 1741 | netDict['port_security'] = iface['port-security'] |
| 1742 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 1743 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1744 | netDict['name'] = iface['internal_name'] |
| 1745 | if iface['net_id'] is None: |
| 1746 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1747 | #print iface |
| 1748 | #print vnf_iface |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1749 | if vnf_iface['interface_id']==iface['uuid']: |
| 1750 | netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ] |
| 1751 | break |
| 1752 | else: |
| 1753 | netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| 1754 | #skip bridge ifaces not connected to any net |
| 1755 | #if 'net_id' not in netDict or netDict['net_id']==None: |
| 1756 | # continue |
| 1757 | myVMDict['networks'].append(netDict) |
| 1758 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 1759 | #print myVMDict['name'] |
| 1760 | #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 1761 | #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 1762 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 1763 | vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None), |
| 1764 | myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks']) |
| 1765 | #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id) |
| 1766 | vm['vim_id'] = vm_id |
| 1767 | rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id}) |
| 1768 | #put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 1769 | for net in myVMDict['networks']: |
| 1770 | if "vim_id" in net: |
| 1771 | for iface in vm['interfaces']: |
| 1772 | if net["name"]==iface["internal_name"]: |
| 1773 | iface["vim_id"]=net["vim_id"] |
| 1774 | break |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1775 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1776 | logger.debug("start scenario Deployment done") |
| 1777 | #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) |
| 1778 | #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] | 1779 | instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict) |
| 1780 | return mydb.get_instance_scenario(instance_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1781 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1782 | except (db_base_Exception, vimconn.vimconnException) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1783 | _, message = rollback(mydb, vims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 1784 | if isinstance(e, db_base_Exception): |
| 1785 | error_text = "Exception at database" |
| 1786 | else: |
| 1787 | error_text = "Exception at VIM" |
| 1788 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 1789 | #logger.error("start_scenario %s", error_text) |
| 1790 | raise NfvoException(error_text, e.http_code) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1791 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1792 | |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1793 | def unify_cloud_config(cloud_config_preserve, cloud_config): |
| 1794 | ''' join the cloud config information into cloud_config_preserve. |
| 1795 | In case of conflict cloud_config_preserve preserves |
| 1796 | None is admited |
| 1797 | ''' |
| 1798 | if not cloud_config_preserve and not cloud_config: |
| 1799 | return None |
| 1800 | |
| 1801 | new_cloud_config = {"key-pairs":[], "users":[]} |
| 1802 | # key-pairs |
| 1803 | if cloud_config_preserve: |
| 1804 | for key in cloud_config_preserve.get("key-pairs", () ): |
| 1805 | if key not in new_cloud_config["key-pairs"]: |
| 1806 | new_cloud_config["key-pairs"].append(key) |
| 1807 | if cloud_config: |
| 1808 | for key in cloud_config.get("key-pairs", () ): |
| 1809 | if key not in new_cloud_config["key-pairs"]: |
| 1810 | new_cloud_config["key-pairs"].append(key) |
| 1811 | if not new_cloud_config["key-pairs"]: |
| 1812 | del new_cloud_config["key-pairs"] |
| 1813 | |
| 1814 | # users |
| 1815 | if cloud_config: |
| 1816 | new_cloud_config["users"] += cloud_config.get("users", () ) |
| 1817 | if cloud_config_preserve: |
| 1818 | new_cloud_config["users"] += cloud_config_preserve.get("users", () ) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 1819 | index_to_delete = [] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1820 | users = new_cloud_config.get("users", []) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 1821 | for index0 in range(0,len(users)): |
| 1822 | if index0 in index_to_delete: |
| 1823 | continue |
| 1824 | for index1 in range(index0+1,len(users)): |
| 1825 | if index1 in index_to_delete: |
| 1826 | continue |
| 1827 | if users[index0]["name"] == users[index1]["name"]: |
| 1828 | index_to_delete.append(index1) |
| 1829 | for key in users[index1].get("key-pairs",()): |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1830 | if "key-pairs" not in users[index0]: |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 1831 | users[index0]["key-pairs"] = [key] |
| 1832 | elif key not in users[index0]["key-pairs"]: |
| 1833 | users[index0]["key-pairs"].append(key) |
| 1834 | index_to_delete.sort(reverse=True) |
| 1835 | for index in index_to_delete: |
| 1836 | del users[index] |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 1837 | if not new_cloud_config["users"]: |
| 1838 | del new_cloud_config["users"] |
| 1839 | |
| 1840 | #boot-data-drive |
| 1841 | if cloud_config and cloud_config.get("boot-data-drive") != None: |
| 1842 | new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"] |
| 1843 | if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None: |
| 1844 | new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"] |
| 1845 | |
| 1846 | # user-data |
| 1847 | if cloud_config and cloud_config.get("user-data") != None: |
| 1848 | new_cloud_config["user-data"] = cloud_config["user-data"] |
| 1849 | if cloud_config_preserve and cloud_config_preserve.get("user-data") != None: |
| 1850 | new_cloud_config["user-data"] = cloud_config_preserve["user-data"] |
| 1851 | |
| 1852 | # config files |
| 1853 | new_cloud_config["config-files"] = [] |
| 1854 | if cloud_config and cloud_config.get("config-files") != None: |
| 1855 | new_cloud_config["config-files"] += cloud_config["config-files"] |
| 1856 | if cloud_config_preserve: |
| 1857 | for file in cloud_config_preserve.get("config-files", ()): |
| 1858 | for index in range(0, len(new_cloud_config["config-files"])): |
| 1859 | if new_cloud_config["config-files"][index]["dest"] == file["dest"]: |
| 1860 | new_cloud_config["config-files"][index] = file |
| 1861 | break |
| 1862 | else: |
| 1863 | new_cloud_config["config-files"].append(file) |
| 1864 | if not new_cloud_config["config-files"]: |
| 1865 | del new_cloud_config["config-files"] |
| 1866 | return new_cloud_config |
| 1867 | |
| 1868 | |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1869 | 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] | 1870 | datacenter_id = None |
| 1871 | datacenter_name = None |
| 1872 | thread = None |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1873 | try: |
| 1874 | if datacenter_tenant_id: |
| 1875 | thread_id = datacenter_tenant_id |
| 1876 | thread = vim_threads["running"].get(datacenter_tenant_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1877 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1878 | where_={"td.nfvo_tenant_id": tenant_id} |
| 1879 | if datacenter_id_name: |
| 1880 | if utils.check_valid_uuid(datacenter_id_name): |
| 1881 | datacenter_id = datacenter_id_name |
| 1882 | where_["dt.datacenter_id"] = datacenter_id |
| 1883 | else: |
| 1884 | datacenter_name = datacenter_id_name |
| 1885 | where_["d.name"] = datacenter_name |
| 1886 | if datacenter_tenant_id: |
| 1887 | where_["dt.uuid"] = datacenter_tenant_id |
| 1888 | datacenters = mydb.get_rows( |
| 1889 | SELECT=("dt.uuid as datacenter_tenant_id",), |
| 1890 | FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id " |
| 1891 | "join datacenters as d on d.uuid=dt.datacenter_id", |
| 1892 | WHERE=where_) |
| 1893 | if len(datacenters) > 1: |
| 1894 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 1895 | elif datacenters: |
| 1896 | thread_id = datacenters[0]["datacenter_tenant_id"] |
| 1897 | thread = vim_threads["running"].get(thread_id) |
| 1898 | if not thread: |
| 1899 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 1900 | return thread_id, thread |
| 1901 | except db_base_Exception as e: |
| 1902 | raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 1903 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 1904 | 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] | 1905 | datacenter_id = None |
| 1906 | datacenter_name = None |
| 1907 | if datacenter_id_name: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1908 | if utils.check_valid_uuid(datacenter_id_name): |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1909 | datacenter_id = datacenter_id_name |
| 1910 | else: |
| 1911 | datacenter_name = datacenter_id_name |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 1912 | vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1913 | if len(vims) == 0: |
| 1914 | raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found) |
| 1915 | elif len(vims)>1: |
| 1916 | #print "nfvo.datacenter_action() error. Several datacenters found" |
| 1917 | raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict) |
| 1918 | return vims.keys()[0], vims.values()[0] |
| 1919 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1920 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1921 | def update(d, u): |
| 1922 | '''Takes dict d and updates it with the values in dict u.''' |
| 1923 | '''It merges all depth levels''' |
| 1924 | for k, v in u.iteritems(): |
| 1925 | if isinstance(v, collections.Mapping): |
| 1926 | r = update(d.get(k, {}), v) |
| 1927 | d[k] = r |
| 1928 | else: |
| 1929 | d[k] = u[k] |
| 1930 | return d |
| 1931 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1932 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1933 | def create_instance(mydb, tenant_id, instance_dict): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1934 | # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id" |
| 1935 | # logger.debug("Creating instance...") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1936 | scenario = instance_dict["scenario"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1937 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1938 | #find main datacenter |
| 1939 | myvims = {} |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1940 | myvim_threads_id = {} |
| 1941 | instance_tasks={} |
| 1942 | tasks_to_launch={} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1943 | datacenter = instance_dict.get("datacenter") |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1944 | default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| 1945 | myvims[default_datacenter_id] = vim |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1946 | myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id) |
| 1947 | tasks_to_launch[myvim_threads_id[default_datacenter_id]] = [] |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 1948 | #myvim_tenant = myvim['tenant_id'] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1949 | # default_datacenter_name = vim['name'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1950 | rollbackList=[] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1951 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 1952 | #print "Checking that the scenario exists and getting the scenario dictionary" |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1953 | scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1954 | |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 1955 | #logger.debug(">>>>>>> Dictionaries before merging") |
| 1956 | #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256))) |
| 1957 | #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] | 1958 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1959 | scenarioDict['datacenter_id'] = default_datacenter_id |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 1960 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1961 | auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id |
| 1962 | auxNetDict['scenario'] = {} |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1963 | |
| 1964 | 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] | 1965 | instance_name = instance_dict["name"] |
| 1966 | instance_description = instance_dict.get("description") |
| 1967 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1968 | # 0 check correct parameters |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1969 | for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems(): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1970 | found = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1971 | for scenario_net in scenarioDict['nets']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1972 | if net_name == scenario_net["name"]: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1973 | found = True |
| 1974 | break |
| 1975 | if not found: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1976 | raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), HTTP_Bad_Request) |
| 1977 | if "sites" not in net_instance_desc: |
| 1978 | net_instance_desc["sites"] = [ {} ] |
| 1979 | site_without_datacenter_field = False |
| 1980 | for site in net_instance_desc["sites"]: |
| 1981 | if site.get("datacenter"): |
| 1982 | if site["datacenter"] not in myvims: |
| 1983 | #Add this datacenter to myvims |
| 1984 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"]) |
| 1985 | myvims[d] = v |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 1986 | myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, site["datacenter"]) |
| 1987 | tasks_to_launch[myvim_threads_id[d]] = [] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1988 | site["datacenter"] = d #change name to id |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1989 | else: |
| 1990 | if site_without_datacenter_field: |
| 1991 | raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name), HTTP_Bad_Request) |
| 1992 | site_without_datacenter_field = True |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 1993 | site["datacenter"] = default_datacenter_id #change name to id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 1994 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1995 | for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems(): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1996 | found=False |
| 1997 | for scenario_vnf in scenarioDict['vnfs']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 1998 | if vnf_name == scenario_vnf['name']: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1999 | found = True |
| 2000 | break |
| 2001 | if not found: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2002 | raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request) |
| 2003 | if "datacenter" in vnf_instance_desc: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2004 | # Add this datacenter to myvims |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2005 | if vnf_instance_desc["datacenter"] not in myvims: |
| 2006 | d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| 2007 | myvims[d] = v |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2008 | myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"]) |
| 2009 | tasks_to_launch[myvim_threads_id[d]] = [] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2010 | scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"] |
| garciadeblas | 3083338 | 2017-01-09 09:46:31 +0100 | [diff] [blame] | 2011 | |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 2012 | #0.1 parse cloud-config parameters |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2013 | 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] | 2014 | |
| 2015 | #0.2 merge instance information into scenario |
| 2016 | #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict) |
| 2017 | #However, this is not possible yet. |
| 2018 | for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems(): |
| 2019 | for scenario_net in scenarioDict['nets']: |
| 2020 | if net_name == scenario_net["name"]: |
| 2021 | if 'ip-profile' in net_instance_desc: |
| 2022 | ipprofile = net_instance_desc['ip-profile'] |
| 2023 | ipprofile['subnet_address'] = ipprofile.pop('subnet-address',None) |
| 2024 | ipprofile['ip_version'] = ipprofile.pop('ip-version','IPv4') |
| 2025 | ipprofile['gateway_address'] = ipprofile.pop('gateway-address',None) |
| 2026 | ipprofile['dns_address'] = ipprofile.pop('dns-address',None) |
| 2027 | if 'dhcp' in ipprofile: |
| 2028 | ipprofile['dhcp_start_address'] = ipprofile['dhcp'].get('start-address',None) |
| 2029 | ipprofile['dhcp_enabled'] = ipprofile['dhcp'].get('enabled',True) |
| 2030 | ipprofile['dhcp_count'] = ipprofile['dhcp'].get('count',None) |
| 2031 | del ipprofile['dhcp'] |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame] | 2032 | if 'ip_profile' not in scenario_net: |
| 2033 | scenario_net['ip_profile'] = ipprofile |
| 2034 | else: |
| 2035 | update(scenario_net['ip_profile'],ipprofile) |
| tierno | e6c58ce | 2016-09-14 16:02:49 +0200 | [diff] [blame] | 2036 | for interface in net_instance_desc.get('interfaces', () ): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 2037 | if 'ip_address' in interface: |
| 2038 | for vnf in scenarioDict['vnfs']: |
| 2039 | if interface['vnf'] == vnf['name']: |
| 2040 | for vnf_interface in vnf['interfaces']: |
| 2041 | if interface['vnf_interface'] == vnf_interface['external_name']: |
| 2042 | vnf_interface['ip_address']=interface['ip_address'] |
| 2043 | |
| garciadeblas | bb6a1ed | 2016-09-30 14:02:09 +0000 | [diff] [blame] | 2044 | #logger.debug(">>>>>>>> Merged dictionary") |
| tierno | 4319dad | 2016-09-05 12:11:11 +0200 | [diff] [blame] | 2045 | 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] | 2046 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2047 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2048 | # 1. Creating new nets (sce_nets) in the VIM" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2049 | for sce_net in scenarioDict['nets']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2050 | sce_net["vim_id_sites"]={} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2051 | descriptor_net = instance_dict.get("networks",{}).get(sce_net["name"],{}) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2052 | net_name = descriptor_net.get("vim-network-name") |
| 2053 | auxNetDict['scenario'][sce_net['uuid']] = {} |
| 2054 | |
| 2055 | sites = descriptor_net.get("sites", [ {} ]) |
| 2056 | for site in sites: |
| 2057 | if site.get("datacenter"): |
| 2058 | vim = myvims[ site["datacenter"] ] |
| 2059 | datacenter_id = site["datacenter"] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2060 | myvim_thread_id = myvim_threads_id[ site["datacenter"] ] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2061 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2062 | vim = myvims[ default_datacenter_id ] |
| 2063 | datacenter_id = default_datacenter_id |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2064 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2065 | net_type = sce_net['type'] |
| 2066 | lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True |
| 2067 | if sce_net["external"]: |
| 2068 | if not net_name: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2069 | net_name = sce_net["name"] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2070 | if "netmap-use" in site or "netmap-create" in site: |
| 2071 | create_network = False |
| 2072 | lookfor_network = False |
| 2073 | if "netmap-use" in site: |
| 2074 | lookfor_network = True |
| 2075 | if utils.check_valid_uuid(site["netmap-use"]): |
| 2076 | filter_text = "scenario id '%s'" % site["netmap-use"] |
| 2077 | lookfor_filter["id"] = site["netmap-use"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2078 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2079 | filter_text = "scenario name '%s'" % site["netmap-use"] |
| 2080 | lookfor_filter["name"] = site["netmap-use"] |
| 2081 | if "netmap-create" in site: |
| 2082 | create_network = True |
| 2083 | net_vim_name = net_name |
| 2084 | if site["netmap-create"]: |
| 2085 | net_vim_name = site["netmap-create"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2086 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2087 | elif sce_net['vim_id'] != None: |
| 2088 | #there is a netmap at datacenter_nets database #TODO REVISE!!!! |
| 2089 | create_network = False |
| 2090 | lookfor_network = True |
| 2091 | lookfor_filter["id"] = sce_net['vim_id'] |
| 2092 | filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"]) |
| 2093 | #look for network at datacenter and return error |
| 2094 | else: |
| 2095 | #There is not a netmap, look at datacenter for a net with this name and create if not found |
| 2096 | create_network = True |
| 2097 | lookfor_network = True |
| 2098 | lookfor_filter["name"] = sce_net["name"] |
| 2099 | net_vim_name = sce_net["name"] |
| 2100 | filter_text = "scenario name '%s'" % sce_net["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2101 | else: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2102 | if not net_name: |
| 2103 | net_name = "%s.%s" %(instance_name, sce_net["name"]) |
| 2104 | net_name = net_name[:255] #limit length |
| 2105 | net_vim_name = net_name |
| 2106 | create_network = True |
| 2107 | lookfor_network = False |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2108 | |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2109 | if lookfor_network: |
| 2110 | vim_nets = vim.get_network_list(filter_dict=lookfor_filter) |
| 2111 | if len(vim_nets) > 1: |
| 2112 | raise NfvoException("More than one candidate VIM network found for " + filter_text, HTTP_Bad_Request ) |
| 2113 | elif len(vim_nets) == 0: |
| 2114 | if not create_network: |
| 2115 | raise NfvoException("No candidate VIM network found for " + filter_text, HTTP_Bad_Request ) |
| 2116 | else: |
| 2117 | sce_net["vim_id_sites"][datacenter_id] = vim_nets[0]['id'] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2118 | auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = vim_nets[0]['id'] |
| 2119 | create_network = False |
| 2120 | if create_network: |
| 2121 | #if network is not external |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2122 | 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] | 2123 | task_id = task["id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2124 | instance_tasks[task_id] = task |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2125 | tasks_to_launch[myvim_thread_id].append(task) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2126 | #network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None)) |
| 2127 | sce_net["vim_id_sites"][datacenter_id] = task_id |
| 2128 | auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = task_id |
| 2129 | rollbackList.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id, 'uuid':task_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2130 | sce_net["created"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2131 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2132 | # 2. Creating new nets (vnf internal nets) in the VIM" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2133 | #For each vnf net, we create it and we add it to instanceNetlist. |
| 2134 | for sce_vnf in scenarioDict['vnfs']: |
| 2135 | for net in sce_vnf['nets']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2136 | if sce_vnf.get("datacenter"): |
| 2137 | vim = myvims[ sce_vnf["datacenter"] ] |
| 2138 | datacenter_id = sce_vnf["datacenter"] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2139 | myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"]] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2140 | else: |
| 2141 | vim = myvims[ default_datacenter_id ] |
| 2142 | datacenter_id = default_datacenter_id |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2143 | myvim_thread_id = myvim_threads_id[default_datacenter_id] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2144 | descriptor_net = instance_dict.get("vnfs",{}).get(sce_vnf["name"],{}) |
| 2145 | net_name = descriptor_net.get("name") |
| 2146 | if not net_name: |
| 2147 | net_name = "%s.%s" %(instance_name, net["name"]) |
| 2148 | net_name = net_name[:255] #limit length |
| 2149 | net_type = net['type'] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2150 | 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] | 2151 | task_id = task["id"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2152 | instance_tasks[task_id] = task |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2153 | tasks_to_launch[myvim_thread_id].append(task) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2154 | # network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None)) |
| 2155 | net['vim_id'] = task_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2156 | if sce_vnf['uuid'] not in auxNetDict: |
| 2157 | auxNetDict[sce_vnf['uuid']] = {} |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2158 | auxNetDict[sce_vnf['uuid']][net['uuid']] = task_id |
| 2159 | rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':task_id}) |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2160 | net["created"] = True |
| 2161 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2162 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2163 | #print "auxNetDict:" |
| 2164 | #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2165 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2166 | # 3. Creating new vm instances in the VIM |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2167 | #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2168 | for sce_vnf in scenarioDict['vnfs']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2169 | if sce_vnf.get("datacenter"): |
| 2170 | vim = myvims[ sce_vnf["datacenter"] ] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2171 | myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2172 | datacenter_id = sce_vnf["datacenter"] |
| 2173 | else: |
| 2174 | vim = myvims[ default_datacenter_id ] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2175 | myvim_thread_id = myvim_threads_id[ default_datacenter_id ] |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2176 | datacenter_id = default_datacenter_id |
| 2177 | sce_vnf["datacenter_id"] = datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2178 | i = 0 |
| 2179 | for vm in sce_vnf['vms']: |
| 2180 | i += 1 |
| 2181 | myVMDict = {} |
| tierno | ae65a48 | 2016-11-24 16:20:05 +0100 | [diff] [blame] | 2182 | myVMDict['name'] = "{}.{}.{}".format(instance_name,sce_vnf['name'],chr(96+i)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2183 | myVMDict['description'] = myVMDict['name'][0:99] |
| 2184 | # if not startvms: |
| 2185 | # myVMDict['start'] = "no" |
| 2186 | myVMDict['name'] = myVMDict['name'][0:255] #limit name length |
| 2187 | #create image at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2188 | image_dict = mydb.get_table_by_uuid_name("images", vm['image_id']) |
| tierno | 5e91eb8 | 2016-10-04 09:39:07 +0000 | [diff] [blame] | 2189 | image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2190 | vm['vim_image_id'] = image_id |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2191 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2192 | #create flavor at vim in case it not exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2193 | flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2194 | if flavor_dict['extended']!=None: |
| 2195 | flavor_dict['extended']= yaml.load(flavor_dict['extended']) |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2196 | flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True) |
| 2197 | |
| montesmoreno | 0c8def0 | 2016-12-22 12:16:23 +0000 | [diff] [blame] | 2198 | #Obtain information for additional disks |
| 2199 | extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id}) |
| 2200 | if not extended_flavor_dict: |
| 2201 | raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found) |
| 2202 | return |
| 2203 | |
| 2204 | #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0]) |
| 2205 | myVMDict['disks'] = None |
| 2206 | extended_info = extended_flavor_dict[0]['extended'] |
| 2207 | if extended_info != None: |
| 2208 | extended_flavor_dict_yaml = yaml.load(extended_info) |
| 2209 | if 'disks' in extended_flavor_dict_yaml: |
| 2210 | myVMDict['disks'] = extended_flavor_dict_yaml['disks'] |
| 2211 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2212 | vm['vim_flavor_id'] = flavor_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2213 | myVMDict['imageRef'] = vm['vim_image_id'] |
| 2214 | myVMDict['flavorRef'] = vm['vim_flavor_id'] |
| 2215 | myVMDict['networks'] = [] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2216 | task_depends = {} |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2217 | #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2218 | for iface in vm['interfaces']: |
| 2219 | netDict = {} |
| 2220 | if iface['type']=="data": |
| 2221 | netDict['type'] = iface['model'] |
| 2222 | elif "model" in iface and iface["model"]!=None: |
| 2223 | netDict['model']=iface['model'] |
| 2224 | #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model |
| 2225 | #discover type of interface looking at flavor |
| 2226 | for numa in flavor_dict.get('extended',{}).get('numas',[]): |
| 2227 | for flavor_iface in numa.get('interfaces',[]): |
| 2228 | if flavor_iface.get('name') == iface['internal_name']: |
| 2229 | if flavor_iface['dedicated'] == 'yes': |
| 2230 | netDict['type']="PF" #passthrough |
| 2231 | elif flavor_iface['dedicated'] == 'no': |
| 2232 | netDict['type']="VF" #siov |
| 2233 | elif flavor_iface['dedicated'] == 'yes:sriov': |
| 2234 | netDict['type']="VFnotShared" #sriov but only one sriov on the PF |
| 2235 | netDict["mac_address"] = flavor_iface.get("mac_address") |
| 2236 | break; |
| 2237 | netDict["use"]=iface['type'] |
| 2238 | if netDict["use"]=="data" and not netDict.get("type"): |
| 2239 | #print "netDict", netDict |
| 2240 | #print "iface", iface |
| 2241 | 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']) |
| 2242 | if flavor_dict.get('extended')==None: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2243 | raise NfvoException(e_text + "After database migration some information is not available. \ |
| 2244 | Try to delete and create the scenarios and VNFs again", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2245 | else: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2246 | raise NfvoException(e_text, HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2247 | if netDict["use"]=="mgmt" or netDict["use"]=="bridge": |
| 2248 | netDict["type"]="virtual" |
| 2249 | if "vpci" in iface and iface["vpci"] is not None: |
| 2250 | netDict['vpci'] = iface['vpci'] |
| 2251 | if "mac" in iface and iface["mac"] is not None: |
| 2252 | netDict['mac_address'] = iface['mac'] |
| montesmoreno | 2a1fc4e | 2017-01-09 16:46:04 +0000 | [diff] [blame] | 2253 | if "port-security" in iface and iface["port-security"] is not None: |
| 2254 | netDict['port_security'] = iface['port-security'] |
| 2255 | if "floating-ip" in iface and iface["floating-ip"] is not None: |
| 2256 | netDict['floating_ip'] = iface['floating-ip'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2257 | netDict['name'] = iface['internal_name'] |
| 2258 | if iface['net_id'] is None: |
| 2259 | for vnf_iface in sce_vnf["interfaces"]: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2260 | #print iface |
| 2261 | #print vnf_iface |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2262 | if vnf_iface['interface_id']==iface['uuid']: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2263 | netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2264 | break |
| 2265 | else: |
| 2266 | netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2267 | if netDict.get('net_id') and is_task_id(netDict['net_id']): |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2268 | task_depends[netDict['net_id']] = instance_tasks[netDict['net_id']] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2269 | #skip bridge ifaces not connected to any net |
| 2270 | #if 'net_id' not in netDict or netDict['net_id']==None: |
| 2271 | # continue |
| 2272 | myVMDict['networks'].append(netDict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2273 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| 2274 | #print myVMDict['name'] |
| 2275 | #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False) |
| 2276 | #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False) |
| 2277 | #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>" |
| tierno | 36c0b17 | 2017-01-12 18:32:28 +0100 | [diff] [blame] | 2278 | if vm.get("boot_data"): |
| 2279 | cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config) |
| 2280 | else: |
| 2281 | cloud_config_vm = cloud_config |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2282 | task = new_task("new-vm", (myVMDict['name'], myVMDict['description'], myVMDict.get('start', None), |
| 2283 | myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], |
| 2284 | cloud_config_vm, myVMDict['disks']), depends=task_depends) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2285 | instance_tasks[task["id"]] = task |
| 2286 | tasks_to_launch[myvim_thread_id].append(task) |
| 2287 | vm_id = task["id"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2288 | vm['vim_id'] = vm_id |
| 2289 | rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id}) |
| 2290 | #put interface uuid back to scenario[vnfs][vms[[interfaces] |
| 2291 | for net in myVMDict['networks']: |
| 2292 | if "vim_id" in net: |
| 2293 | for iface in vm['interfaces']: |
| 2294 | if net["name"]==iface["internal_name"]: |
| 2295 | iface["vim_id"]=net["vim_id"] |
| 2296 | break |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2297 | scenarioDict["datacenter2tenant"] = myvim_threads_id |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2298 | logger.debug("create_instance Deployment done scenarioDict: %s", |
| 2299 | yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) ) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2300 | instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict) |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2301 | for myvim_thread_id,task_list in tasks_to_launch.items(): |
| 2302 | for task in task_list: |
| 2303 | vim_threads["running"][myvim_thread_id].insert_task(task) |
| 2304 | |
| 2305 | global_instance_tasks[instance_id] = instance_tasks |
| 2306 | # Update database with those ended instance_tasks |
| 2307 | # for task in instance_tasks.values(): |
| 2308 | # if task["status"] == "ok": |
| 2309 | # if task["name"] == "new-vm": |
| 2310 | # mydb.update_rows("instance_vms", UPDATE={"vim_vm_id": task["result"]}, |
| 2311 | # WHERE={"vim_vm_id": task["id"]}) |
| 2312 | # elif task["name"] == "new-net": |
| 2313 | # mydb.update_rows("instance_nets", UPDATE={"vim_net_id": task["result"]}, |
| 2314 | # WHERE={"vim_net_id": task["id"]}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2315 | return mydb.get_instance_scenario(instance_id) |
| 2316 | except (NfvoException, vimconn.vimconnException,db_base_Exception) as e: |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 2317 | message = rollback(mydb, myvims, rollbackList) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2318 | if isinstance(e, db_base_Exception): |
| 2319 | error_text = "database Exception" |
| 2320 | elif isinstance(e, vimconn.vimconnException): |
| 2321 | error_text = "VIM Exception" |
| 2322 | else: |
| 2323 | error_text = "Exception" |
| 2324 | error_text += " {} {}. {}".format(type(e).__name__, str(e), message) |
| 2325 | #logger.error("create_instance: %s", error_text) |
| 2326 | raise NfvoException(error_text, e.http_code) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2327 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2328 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2329 | def delete_instance(mydb, tenant_id, instance_id): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2330 | #print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2331 | instanceDict = mydb.get_instance_scenario(instance_id, tenant_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2332 | #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2333 | tenant_id = instanceDict["tenant_id"] |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2334 | #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] | 2335 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2336 | #1. Delete from Database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2337 | message = mydb.delete_instance_scenario(instance_id, tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2338 | |
| 2339 | #2. delete from VIM |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2340 | error_msg = "" |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2341 | myvims = {} |
| 2342 | myvim_threads = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2343 | |
| 2344 | #2.1 deleting VMs |
| 2345 | #vm_fail_list=[] |
| 2346 | for sce_vnf in instanceDict['vnfs']: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2347 | datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 2348 | if datacenter_key not in myvims: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2349 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2350 | _,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] | 2351 | except NfvoException as e: |
| 2352 | logger.error(str(e)) |
| 2353 | myvim_thread = None |
| 2354 | myvim_threads[datacenter_key] = myvim_thread |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2355 | vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"], |
| 2356 | datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 2357 | if len(vims) == 0: |
| 2358 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], |
| 2359 | sce_vnf["datacenter_tenant_id"])) |
| 2360 | myvims[datacenter_key] = None |
| 2361 | else: |
| 2362 | myvims[datacenter_key] = vims.values()[0] |
| 2363 | myvim = myvims[datacenter_key] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2364 | myvim_thread = myvim_threads[datacenter_key] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2365 | for vm in sce_vnf['vms']: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2366 | if not myvim: |
| 2367 | error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"]) |
| 2368 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2369 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2370 | task=None |
| 2371 | if is_task_id(vm['vim_vm_id']): |
| 2372 | task_id = vm['vim_vm_id'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2373 | old_task = global_instance_tasks[instance_id].get(task_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2374 | if not old_task: |
| 2375 | error_msg += "\n VM was scheduled for create, but task {} is not found".format(task_id) |
| 2376 | continue |
| 2377 | with task_lock: |
| 2378 | if old_task["status"] == "enqueued": |
| 2379 | old_task["status"] = "deleted" |
| 2380 | elif old_task["status"] == "error": |
| 2381 | continue |
| 2382 | elif old_task["status"] == "processing": |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2383 | 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] | 2384 | else: #ok |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2385 | task = new_task("del-vm", (old_task["result"], vm["interfaces"])) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2386 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2387 | task = new_task("del-vm", (vm['vim_vm_id'], vm["interfaces"]) ) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2388 | if task: |
| 2389 | myvim_thread.insert_task(task) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2390 | except vimconn.vimconnNotFoundException as e: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2391 | 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] | 2392 | logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found", |
| 2393 | vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id']) |
| 2394 | except vimconn.vimconnException as e: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2395 | error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e)) |
| 2396 | 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] | 2397 | 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] | 2398 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2399 | #2.2 deleting NETS |
| 2400 | #net_fail_list=[] |
| 2401 | for net in instanceDict['nets']: |
| tierno | 66345bc | 2016-09-26 11:37:55 +0200 | [diff] [blame] | 2402 | if not net['created']: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2403 | continue #skip not created nets |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2404 | datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 2405 | if datacenter_key not in myvims: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2406 | try: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2407 | _,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] | 2408 | except NfvoException as e: |
| 2409 | logger.error(str(e)) |
| 2410 | myvim_thread = None |
| 2411 | myvim_threads[datacenter_key] = myvim_thread |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2412 | vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"], |
| 2413 | datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 2414 | if len(vims) == 0: |
| 2415 | logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 2416 | myvims[datacenter_key] = None |
| 2417 | else: |
| 2418 | myvims[datacenter_key] = vims.values()[0] |
| 2419 | myvim = myvims[datacenter_key] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2420 | myvim_thread = myvim_threads[datacenter_key] |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2421 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2422 | if not myvim: |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2423 | 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] | 2424 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2425 | try: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2426 | task = None |
| 2427 | if is_task_id(net['vim_net_id']): |
| 2428 | task_id = net['vim_net_id'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2429 | old_task = global_instance_tasks[instance_id].get(task_id) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2430 | if not old_task: |
| 2431 | error_msg += "\n NET was scheduled for create, but task {} is not found".format(task_id) |
| 2432 | continue |
| 2433 | with task_lock: |
| 2434 | if old_task["status"] == "enqueued": |
| 2435 | old_task["status"] = "deleted" |
| 2436 | elif old_task["status"] == "error": |
| 2437 | continue |
| 2438 | elif old_task["status"] == "processing": |
| 2439 | task = new_task("del-net", task_id, depends={task_id: old_task}) |
| 2440 | else: # ok |
| 2441 | task = new_task("del-net", old_task["result"]) |
| 2442 | else: |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2443 | task = new_task("del-net", (net['vim_net_id'], net['sdn_net_id'])) |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2444 | if task: |
| 2445 | myvim_thread.insert_task(task) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2446 | except vimconn.vimconnNotFoundException as e: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2447 | 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] | 2448 | 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] | 2449 | net['uuid'], net['vim_net_id'], str(net['vnf_net_id'])) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2450 | except vimconn.vimconnException as e: |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2451 | error_msg += "\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'], |
| 2452 | net["datacenter_id"], |
| 2453 | e.http_code, str(e)) |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2454 | 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] | 2455 | e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e)) |
| 2456 | if len(error_msg) > 0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2457 | 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] | 2458 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2459 | return 'instance ' + message + ' deleted' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2460 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2461 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2462 | def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None): |
| 2463 | '''Refreshes a scenario instance. It modifies instanceDict''' |
| 2464 | '''Returns: |
| 2465 | - 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 |
| 2466 | - error_msg |
| 2467 | ''' |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2468 | # # Assumption: nfvo_tenant and instance_id were checked before entering into this function |
| 2469 | # #print "nfvo.refresh_instance begins" |
| 2470 | # #print json.dumps(instanceDict, indent=4) |
| 2471 | # |
| 2472 | # #print "Getting the VIM URL and the VIM tenant_id" |
| 2473 | # myvims={} |
| 2474 | # |
| 2475 | # # 1. Getting VIM vm and net list |
| 2476 | # vms_updated = [] #List of VM instance uuids in openmano that were updated |
| 2477 | # vms_notupdated=[] |
| 2478 | # vm_list = {} |
| 2479 | # for sce_vnf in instanceDict['vnfs']: |
| 2480 | # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]) |
| 2481 | # if datacenter_key not in vm_list: |
| 2482 | # vm_list[datacenter_key] = [] |
| 2483 | # if datacenter_key not in myvims: |
| 2484 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"], |
| 2485 | # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"]) |
| 2486 | # if len(vims) == 0: |
| 2487 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])) |
| 2488 | # myvims[datacenter_key] = None |
| 2489 | # else: |
| 2490 | # myvims[datacenter_key] = vims.values()[0] |
| 2491 | # for vm in sce_vnf['vms']: |
| 2492 | # vm_list[datacenter_key].append(vm['vim_vm_id']) |
| 2493 | # vms_notupdated.append(vm["uuid"]) |
| 2494 | # |
| 2495 | # nets_updated = [] #List of VM instance uuids in openmano that were updated |
| 2496 | # nets_notupdated=[] |
| 2497 | # net_list = {} |
| 2498 | # for net in instanceDict['nets']: |
| 2499 | # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"]) |
| 2500 | # if datacenter_key not in net_list: |
| 2501 | # net_list[datacenter_key] = [] |
| 2502 | # if datacenter_key not in myvims: |
| 2503 | # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"], |
| 2504 | # datacenter_tenant_id=net["datacenter_tenant_id"]) |
| 2505 | # if len(vims) == 0: |
| 2506 | # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])) |
| 2507 | # myvims[datacenter_key] = None |
| 2508 | # else: |
| 2509 | # myvims[datacenter_key] = vims.values()[0] |
| 2510 | # |
| 2511 | # net_list[datacenter_key].append(net['vim_net_id']) |
| 2512 | # nets_notupdated.append(net["uuid"]) |
| 2513 | # |
| 2514 | # # 1. Getting the status of all VMs |
| 2515 | # vm_dict={} |
| 2516 | # for datacenter_key in myvims: |
| 2517 | # if not vm_list.get(datacenter_key): |
| 2518 | # continue |
| 2519 | # failed = True |
| 2520 | # failed_message="" |
| 2521 | # if not myvims[datacenter_key]: |
| 2522 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 2523 | # else: |
| 2524 | # try: |
| 2525 | # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) ) |
| 2526 | # failed = False |
| 2527 | # except vimconn.vimconnException as e: |
| 2528 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 2529 | # failed_message = str(e) |
| 2530 | # if failed: |
| 2531 | # for vm in vm_list[datacenter_key]: |
| 2532 | # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 2533 | # |
| 2534 | # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed |
| 2535 | # for sce_vnf in instanceDict['vnfs']: |
| 2536 | # for vm in sce_vnf['vms']: |
| 2537 | # vm_id = vm['vim_vm_id'] |
| 2538 | # interfaces = vm_dict[vm_id].pop('interfaces', []) |
| 2539 | # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE |
| 2540 | # has_mgmt_iface = False |
| 2541 | # for iface in vm["interfaces"]: |
| 2542 | # if iface["type"]=="mgmt": |
| 2543 | # has_mgmt_iface = True |
| 2544 | # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface: |
| 2545 | # vm_dict[vm_id]['status'] = "ACTIVE" |
| 2546 | # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024: |
| 2547 | # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:] |
| 2548 | # 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'): |
| 2549 | # vm['status'] = vm_dict[vm_id]['status'] |
| 2550 | # vm['error_msg'] = vm_dict[vm_id].get('error_msg') |
| 2551 | # vm['vim_info'] = vm_dict[vm_id].get('vim_info') |
| 2552 | # # 2.1. Update in openmano DB the VMs whose status changed |
| 2553 | # try: |
| 2554 | # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]}) |
| 2555 | # vms_notupdated.remove(vm["uuid"]) |
| 2556 | # if updates>0: |
| 2557 | # vms_updated.append(vm["uuid"]) |
| 2558 | # except db_base_Exception as e: |
| 2559 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 2560 | # # 2.2. Update in openmano DB the interface VMs |
| 2561 | # for interface in interfaces: |
| 2562 | # #translate from vim_net_id to instance_net_id |
| 2563 | # network_id_list=[] |
| 2564 | # for net in instanceDict['nets']: |
| 2565 | # if net["vim_net_id"] == interface["vim_net_id"]: |
| 2566 | # network_id_list.append(net["uuid"]) |
| 2567 | # if not network_id_list: |
| 2568 | # continue |
| 2569 | # del interface["vim_net_id"] |
| 2570 | # try: |
| 2571 | # for network_id in network_id_list: |
| 2572 | # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id}) |
| 2573 | # except db_base_Exception as e: |
| 2574 | # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id) |
| 2575 | # |
| 2576 | # # 3. Getting the status of all nets |
| 2577 | # net_dict = {} |
| 2578 | # for datacenter_key in myvims: |
| 2579 | # if not net_list.get(datacenter_key): |
| 2580 | # continue |
| 2581 | # failed = True |
| 2582 | # failed_message = "" |
| 2583 | # if not myvims[datacenter_key]: |
| 2584 | # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]) |
| 2585 | # else: |
| 2586 | # try: |
| 2587 | # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) ) |
| 2588 | # failed = False |
| 2589 | # except vimconn.vimconnException as e: |
| 2590 | # logger.error("VIM exception %s %s", type(e).__name__, str(e)) |
| 2591 | # failed_message = str(e) |
| 2592 | # if failed: |
| 2593 | # for net in net_list[datacenter_key]: |
| 2594 | # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message} |
| 2595 | # |
| 2596 | # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed |
| 2597 | # # TODO: update nets inside a vnf |
| 2598 | # for net in instanceDict['nets']: |
| 2599 | # net_id = net['vim_net_id'] |
| 2600 | # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024: |
| 2601 | # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:] |
| 2602 | # 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'): |
| 2603 | # net['status'] = net_dict[net_id]['status'] |
| 2604 | # net['error_msg'] = net_dict[net_id].get('error_msg') |
| 2605 | # net['vim_info'] = net_dict[net_id].get('vim_info') |
| 2606 | # # 5.1. Update in openmano DB the nets whose status changed |
| 2607 | # try: |
| 2608 | # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]}) |
| 2609 | # nets_notupdated.remove(net["uuid"]) |
| 2610 | # if updated>0: |
| 2611 | # nets_updated.append(net["uuid"]) |
| 2612 | # except db_base_Exception as e: |
| 2613 | # logger.error("nfvo.refresh_instance error database update: %s", str(e)) |
| 2614 | # |
| 2615 | # # Returns appropriate output |
| 2616 | # #print "nfvo.refresh_instance finishes" |
| 2617 | # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s", |
| 2618 | # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2619 | instance_id = instanceDict['uuid'] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2620 | # if len(vms_notupdated)+len(nets_notupdated)>0: |
| 2621 | # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated) |
| 2622 | # 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] | 2623 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2624 | return 0, 'Scenario instance ' + instance_id + ' refreshed.' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2625 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2626 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2627 | def instance_action(mydb,nfvo_tenant,instance_id, action_dict): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2628 | #print "Checking that the instance_id exists and getting the instance dictionary" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2629 | instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2630 | #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False) |
| 2631 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2632 | #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] | 2633 | vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id']) |
| 2634 | if len(vims) == 0: |
| 2635 | raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2636 | myvim = vims.values()[0] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2637 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2638 | |
| 2639 | input_vnfs = action_dict.pop("vnfs", []) |
| 2640 | input_vms = action_dict.pop("vms", []) |
| 2641 | action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False |
| 2642 | vm_result = {} |
| 2643 | vm_error = 0 |
| 2644 | vm_ok = 0 |
| 2645 | for sce_vnf in instanceDict['vnfs']: |
| 2646 | for vm in sce_vnf['vms']: |
| 2647 | if not action_over_all: |
| 2648 | if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \ |
| 2649 | vm['uuid'] not in input_vms and vm['name'] not in input_vms: |
| 2650 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2651 | try: |
| 2652 | data = myvim.action_vminstance(vm['vim_vm_id'], action_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2653 | if "console" in action_dict: |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 2654 | if not global_config["http_console_proxy"]: |
| 2655 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 2656 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 2657 | protocol=data["protocol"], |
| 2658 | ip = data["server"], |
| 2659 | port = data["port"], |
| 2660 | suffix = data["suffix"]), |
| 2661 | "name":vm['name'] |
| 2662 | } |
| 2663 | vm_ok +=1 |
| 2664 | elif data["server"]=="127.0.0.1" or data["server"]=="localhost": |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2665 | vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized, |
| 2666 | "description": "this console is only reachable by local interface", |
| 2667 | "name":vm['name'] |
| 2668 | } |
| 2669 | vm_error+=1 |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 2670 | else: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2671 | #print "console data", data |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2672 | try: |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 2673 | console_thread = create_or_use_console_proxy_thread(data["server"], data["port"]) |
| 2674 | vm_result[ vm['uuid'] ] = {"vim_result": 200, |
| 2675 | "description": "{protocol}//{ip}:{port}/{suffix}".format( |
| 2676 | protocol=data["protocol"], |
| 2677 | ip = global_config["http_console_host"], |
| 2678 | port = console_thread.port, |
| 2679 | suffix = data["suffix"]), |
| 2680 | "name":vm['name'] |
| 2681 | } |
| 2682 | vm_ok +=1 |
| 2683 | except NfvoException as e: |
| 2684 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 2685 | vm_error+=1 |
| 2686 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2687 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2688 | vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2689 | vm_ok +=1 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2690 | except vimconn.vimconnException as e: |
| 2691 | vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)} |
| 2692 | vm_error+=1 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2693 | |
| 2694 | if vm_ok==0: #all goes wrong |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 2695 | return vm_result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2696 | else: |
| tierno | 351863c | 2016-07-23 01:46:03 +0200 | [diff] [blame] | 2697 | return vm_result |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2698 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2699 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2700 | def create_or_use_console_proxy_thread(console_server, console_port): |
| 2701 | #look for a non-used port |
| 2702 | console_thread_key = console_server + ":" + str(console_port) |
| 2703 | if console_thread_key in global_config["console_thread"]: |
| 2704 | #global_config["console_thread"][console_thread_key].start_timeout() |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2705 | return global_config["console_thread"][console_thread_key] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2706 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2707 | for port in global_config["console_port_iterator"](): |
| tierno | 20fc2a2 | 2016-08-19 17:02:35 +0200 | [diff] [blame] | 2708 | #print "create_or_use_console_proxy_thread() port:", port |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2709 | if port in global_config["console_ports"]: |
| 2710 | continue |
| 2711 | try: |
| 2712 | clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port) |
| 2713 | clithread.start() |
| 2714 | global_config["console_thread"][console_thread_key] = clithread |
| 2715 | global_config["console_ports"][port] = console_thread_key |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2716 | return clithread |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2717 | except cli.ConsoleProxyExceptionPortUsed as e: |
| 2718 | #port used, try with onoher |
| 2719 | continue |
| 2720 | except cli.ConsoleProxyException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2721 | raise NfvoException(str(e), HTTP_Bad_Request) |
| 2722 | raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2723 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2724 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2725 | def check_tenant(mydb, tenant_id): |
| 2726 | '''check that tenant exists at database''' |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2727 | tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id}) |
| 2728 | if not tenant: |
| 2729 | raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found) |
| 2730 | return |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2731 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2732 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2733 | def new_tenant(mydb, tenant_dict): |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2734 | tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, add_uuid=True) |
| 2735 | return tenant_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2736 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2737 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2738 | def delete_tenant(mydb, tenant): |
| 2739 | #get nfvo_tenant info |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2740 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2741 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant') |
| 2742 | mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid']) |
| 2743 | return tenant_dict['uuid'] + " " + tenant_dict["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2744 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2745 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2746 | def new_datacenter(mydb, datacenter_descriptor): |
| 2747 | if "config" in datacenter_descriptor: |
| 2748 | 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] | 2749 | #Check that datacenter-type is correct |
| 2750 | datacenter_type = datacenter_descriptor.get("type", "openvim"); |
| 2751 | module_info = None |
| 2752 | try: |
| 2753 | module = "vimconn_" + datacenter_type |
| 2754 | module_info = imp.find_module(module) |
| 2755 | except (IOError, ImportError): |
| 2756 | if module_info and module_info[0]: |
| 2757 | file.close(module_info[0]) |
| 2758 | 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] | 2759 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2760 | datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True) |
| 2761 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2762 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2763 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2764 | def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): |
| 2765 | #obtain data, check that only one exist |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2766 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2767 | #edit data |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2768 | datacenter_id = datacenter['uuid'] |
| 2769 | where={'uuid': datacenter['uuid']} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2770 | if "config" in datacenter_descriptor: |
| 2771 | if datacenter_descriptor['config']!=None: |
| 2772 | try: |
| 2773 | new_config_dict = datacenter_descriptor["config"] |
| 2774 | #delete null fields |
| 2775 | to_delete=[] |
| 2776 | for k in new_config_dict: |
| 2777 | if new_config_dict[k]==None: |
| 2778 | to_delete.append(k) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2779 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 2780 | config_text = datacenter.get("config") |
| 2781 | if not config_text: |
| 2782 | config_text = '{}' |
| 2783 | config_dict = yaml.load(config_text) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2784 | config_dict.update(new_config_dict) |
| 2785 | #delete null fields |
| 2786 | for k in to_delete: |
| 2787 | del config_dict[k] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2788 | except Exception as e: |
| 2789 | raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2790 | datacenter_descriptor["config"]= yaml.safe_dump(config_dict,default_flow_style=True,width=256) if len(config_dict)>0 else None |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2791 | mydb.update_rows('datacenters', datacenter_descriptor, where) |
| 2792 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2793 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2794 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2795 | def delete_datacenter(mydb, datacenter): |
| 2796 | #get nfvo_tenant info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2797 | datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter') |
| 2798 | mydb.delete_row_by_id("datacenters", datacenter_dict['uuid']) |
| 2799 | return datacenter_dict['uuid'] + " " + datacenter_dict['name'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2800 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2801 | |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 2802 | 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] | 2803 | #get datacenter info |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2804 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter) |
| 2805 | datacenter_name = myvim["name"] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2806 | |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2807 | create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False |
| 2808 | |
| 2809 | # get nfvo_tenant info |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2810 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2811 | if vim_tenant_name==None: |
| 2812 | vim_tenant_name=tenant_dict['name'] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2813 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2814 | #check that this association does not exist before |
| 2815 | tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id } |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2816 | tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 2817 | if len(tenants_datacenters)>0: |
| 2818 | 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] | 2819 | |
| 2820 | vim_tenant_id_exist_atdb=False |
| 2821 | if not create_vim_tenant: |
| 2822 | where_={"datacenter_id": datacenter_id} |
| 2823 | if vim_tenant_id!=None: |
| 2824 | where_["vim_tenant_id"] = vim_tenant_id |
| 2825 | if vim_tenant_name!=None: |
| 2826 | where_["vim_tenant_name"] = vim_tenant_name |
| 2827 | #check if vim_tenant_id is already at database |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2828 | datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_) |
| 2829 | if len(datacenter_tenants_dict)>=1: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2830 | datacenter_tenants_dict = datacenter_tenants_dict[0] |
| 2831 | vim_tenant_id_exist_atdb=True |
| 2832 | #TODO check if a field has changed and edit entry at datacenter_tenants at DB |
| 2833 | else: #result=0 |
| 2834 | datacenter_tenants_dict = {} |
| 2835 | #insert at table datacenter_tenants |
| 2836 | else: #if vim_tenant_id==None: |
| 2837 | #create tenant at VIM if not provided |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2838 | try: |
| 2839 | vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name) |
| 2840 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2841 | 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] | 2842 | datacenter_tenants_dict = {} |
| 2843 | datacenter_tenants_dict["created"]="true" |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2844 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2845 | #fill datacenter_tenants table |
| 2846 | if not vim_tenant_id_exist_atdb: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2847 | datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2848 | datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2849 | datacenter_tenants_dict["user"] = vim_username |
| 2850 | datacenter_tenants_dict["passwd"] = vim_password |
| 2851 | datacenter_tenants_dict["datacenter_id"] = datacenter_id |
| tierno | 8008c3a | 2016-10-13 15:34:28 +0000 | [diff] [blame] | 2852 | if config: |
| 2853 | 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] | 2854 | id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2855 | datacenter_tenants_dict["uuid"] = id_ |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2856 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2857 | #fill tenants_datacenters table |
| 2858 | tenants_datacenter_dict["datacenter_tenant_id"]=datacenter_tenants_dict["uuid"] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2859 | mydb.new_row('tenants_datacenters', tenants_datacenter_dict) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2860 | # create thread |
| 2861 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data |
| 2862 | thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid']) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 2863 | new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, db=db, db_lock=db_lock, ovim=ovim) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2864 | new_thread.start() |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2865 | thread_id = datacenter_tenants_dict["uuid"] |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2866 | vim_threads["running"][thread_id] = new_thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2867 | return datacenter_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2868 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 2869 | def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None): |
| 2870 | #Obtain the data of this datacenter_tenant_id |
| 2871 | vim_data = mydb.get_rows( |
| 2872 | SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user", |
| 2873 | "datacenter_tenants.passwd", "datacenter_tenants.config"), |
| 2874 | FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id", |
| 2875 | WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant, |
| 2876 | "tenants_datacenters.datacenter_id": datacenter_id}) |
| 2877 | |
| 2878 | logger.debug(str(vim_data)) |
| 2879 | if len(vim_data) < 1: |
| 2880 | raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict) |
| 2881 | |
| 2882 | v = vim_data[0] |
| 2883 | if v['config']: |
| 2884 | v['config'] = yaml.load(v['config']) |
| 2885 | |
| 2886 | if vim_tenant_id: |
| 2887 | v['vim_tenant_id'] = vim_tenant_id |
| 2888 | if vim_tenant_name: |
| 2889 | v['vim_tenant_name'] = vim_tenant_name |
| 2890 | if vim_username: |
| 2891 | v['user'] = vim_username |
| 2892 | if vim_password: |
| 2893 | v['passwd'] = vim_password |
| 2894 | if config: |
| 2895 | if not v['config']: |
| 2896 | v['config'] = {} |
| 2897 | v['config'].update(config) |
| 2898 | |
| 2899 | logger.debug(str(v)) |
| 2900 | deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id']) |
| 2901 | associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'], |
| 2902 | vim_username=v['user'], vim_password=v['passwd'], config=v['config']) |
| 2903 | |
| 2904 | return datacenter_id |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2905 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2906 | def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None): |
| 2907 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2908 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2909 | |
| 2910 | #get nfvo_tenant info |
| 2911 | if not tenant_id or tenant_id=="any": |
| 2912 | tenant_uuid = None |
| 2913 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2914 | tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2915 | tenant_uuid = tenant_dict['uuid'] |
| 2916 | |
| 2917 | #check that this association exist before |
| 2918 | tenants_datacenter_dict={"datacenter_id":datacenter_id } |
| 2919 | if tenant_uuid: |
| 2920 | tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2921 | tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| 2922 | if len(tenant_datacenter_list)==0 and tenant_uuid: |
| 2923 | 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] | 2924 | |
| 2925 | #delete this association |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2926 | mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2927 | |
| 2928 | #get vim_tenant info and deletes |
| 2929 | warning='' |
| 2930 | for tenant_datacenter_item in tenant_datacenter_list: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2931 | vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 2932 | #try to delete vim:tenant |
| 2933 | try: |
| 2934 | mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id']) |
| 2935 | if vim_tenant_dict['created']=='true': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2936 | #delete tenant at VIM if created by NFVO |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2937 | try: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2938 | myvim.delete_tenant(vim_tenant_dict['vim_tenant_id']) |
| 2939 | except vimconn.vimconnException as e: |
| 2940 | warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e)) |
| 2941 | logger.warn(warning) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2942 | except db_base_Exception as e: |
| 2943 | logger.error("Cannot delete datacenter_tenants " + str(e)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2944 | 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] | 2945 | thread_id = tenant_datacenter_item["datacenter_tenant_id"] |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2946 | thread = vim_threads["running"][thread_id] |
| tierno | 867ffe9 | 2017-03-27 12:50:34 +0200 | [diff] [blame] | 2947 | thread.insert_task(new_task("exit", None)) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2948 | vim_threads["deleting"][thread_id] = thread |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2949 | return "datacenter {} detached. {}".format(datacenter_id, warning) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2950 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2951 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2952 | def datacenter_action(mydb, tenant_id, datacenter, action_dict): |
| 2953 | #DEPRECATED |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2954 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2955 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2956 | |
| 2957 | if 'net-update' in action_dict: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 2958 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2959 | 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] | 2960 | #print content |
| 2961 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2962 | #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e)) |
| 2963 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2964 | #update nets Change from VIM format to NFVO format |
| 2965 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2966 | for net in nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2967 | net_nfvo={'datacenter_id': datacenter_id} |
| 2968 | net_nfvo['name'] = net['name'] |
| 2969 | #net_nfvo['description']= net['name'] |
| 2970 | net_nfvo['vim_net_id'] = net['id'] |
| 2971 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 2972 | net_nfvo['shared'] = net['shared'] |
| 2973 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| 2974 | net_list.append(net_nfvo) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2975 | inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list) |
| 2976 | logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted) |
| 2977 | return inserted |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2978 | elif 'net-edit' in action_dict: |
| 2979 | net = action_dict['net-edit'].pop('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 2980 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2981 | result = mydb.update_rows('datacenter_nets', action_dict['net-edit'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2982 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2983 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2984 | elif 'net-delete' in action_dict: |
| 2985 | net = action_dict['net-deelte'].get('net') |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 2986 | what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 2987 | result = mydb.delete_row(FROM='datacenter_nets', |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2988 | WHERE={'datacenter_id':datacenter_id, what: net}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2989 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2990 | |
| 2991 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 2992 | raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2993 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 2994 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2995 | def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): |
| 2996 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 2997 | datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 2998 | |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 2999 | what = 'uuid' if utils.check_valid_uuid(netmap) else 'name' |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3000 | result = mydb.update_rows('datacenter_nets', action_dict['netmap'], |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3001 | WHERE={'datacenter_id':datacenter_id, what: netmap}) |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3002 | return result |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3003 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3004 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3005 | def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): |
| 3006 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3007 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3008 | filter_dict={} |
| 3009 | if action_dict: |
| 3010 | action_dict = action_dict["netmap"] |
| 3011 | if 'vim_id' in action_dict: |
| 3012 | filter_dict["id"] = action_dict['vim_id'] |
| 3013 | if 'vim_name' in action_dict: |
| 3014 | filter_dict["name"] = action_dict['vim_name'] |
| 3015 | else: |
| 3016 | filter_dict["shared"] = True |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3017 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3018 | try: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3019 | vim_nets = myvim.get_network_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3020 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3021 | #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e)) |
| 3022 | raise NfvoException(str(e), HTTP_Internal_Server_Error) |
| 3023 | if len(vim_nets)>1 and action_dict: |
| 3024 | raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict) |
| 3025 | elif len(vim_nets)==0: # and action_dict: |
| 3026 | 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] | 3027 | net_list=[] |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3028 | for net in vim_nets: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3029 | net_nfvo={'datacenter_id': datacenter_id} |
| 3030 | if action_dict and "name" in action_dict: |
| 3031 | net_nfvo['name'] = action_dict['name'] |
| 3032 | else: |
| 3033 | net_nfvo['name'] = net['name'] |
| 3034 | #net_nfvo['description']= net['name'] |
| 3035 | net_nfvo['vim_net_id'] = net['id'] |
| 3036 | net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp') |
| 3037 | net_nfvo['shared'] = net['shared'] |
| 3038 | net_nfvo['multipoint'] = False if net['type']=='ptp' else True |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3039 | try: |
| 3040 | net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3041 | net_nfvo["status"] = "OK" |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3042 | net_nfvo["uuid"] = net_id |
| 3043 | except db_base_Exception as e: |
| 3044 | if action_dict: |
| 3045 | raise |
| 3046 | else: |
| 3047 | net_nfvo["status"] = "FAIL: " + str(e) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3048 | net_list.append(net_nfvo) |
| 3049 | return net_list |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3050 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3051 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3052 | def vim_action_get(mydb, tenant_id, datacenter, item, name): |
| 3053 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3054 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3055 | filter_dict={} |
| 3056 | if name: |
| tierno | 42fcc3b | 2016-07-06 17:20:40 +0200 | [diff] [blame] | 3057 | if utils.check_valid_uuid(name): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3058 | filter_dict["id"] = name |
| 3059 | else: |
| 3060 | filter_dict["name"] = name |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3061 | try: |
| 3062 | if item=="networks": |
| 3063 | #filter_dict['tenant_id'] = myvim['tenant_id'] |
| 3064 | content = myvim.get_network_list(filter_dict=filter_dict) |
| 3065 | elif item=="tenants": |
| 3066 | content = myvim.get_tenant_list(filter_dict=filter_dict) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 3067 | elif item == "images": |
| 3068 | content = myvim.get_image_list(filter_dict=filter_dict) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3069 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3070 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | be41e22 | 2016-09-02 15:16:13 +0200 | [diff] [blame] | 3071 | 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] | 3072 | if name and len(content)==1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3073 | return {item[:-1]: content[0]} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3074 | elif name and len(content)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3075 | 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] | 3076 | datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3077 | else: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3078 | return {item: content} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3079 | except vimconn.vimconnException as e: |
| 3080 | 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] | 3081 | 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] | 3082 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3083 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3084 | def vim_action_delete(mydb, tenant_id, datacenter, item, name): |
| 3085 | #get datacenter info |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 3086 | if tenant_id == "any": |
| 3087 | tenant_id=None |
| 3088 | |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3089 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 3090 | #get uuid name |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3091 | content = vim_action_get(mydb, tenant_id, datacenter, item, name) |
| 3092 | logger.debug("vim_action_delete vim response: " + str(content)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 3093 | items = content.values()[0] |
| 3094 | if type(items)==list and len(items)==0: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3095 | raise NfvoException("Not found " + item, HTTP_Not_Found) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 3096 | elif type(items)==list and len(items)>1: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3097 | 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] | 3098 | else: # it is a dict |
| 3099 | item_id = items["id"] |
| 3100 | item_name = str(items.get("name")) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3101 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3102 | try: |
| 3103 | if item=="networks": |
| 3104 | content = myvim.delete_network(item_id) |
| 3105 | elif item=="tenants": |
| 3106 | content = myvim.delete_tenant(item_id) |
| tierno | 4540ea5 | 2017-01-18 17:44:32 +0100 | [diff] [blame] | 3107 | elif item == "images": |
| 3108 | content = myvim.delete_image(item_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3109 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3110 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3111 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3112 | #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e))) |
| 3113 | 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] | 3114 | |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3115 | return "{} {} {} deleted".format(item[:-1], item_id,item_name) |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3116 | |
| tierno | b3d3674 | 2017-03-03 23:51:05 +0100 | [diff] [blame] | 3117 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3118 | def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): |
| 3119 | #get datacenter info |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3120 | logger.debug("vim_action_create descriptor %s", str(descriptor)) |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 3121 | if tenant_id == "any": |
| 3122 | tenant_id=None |
| tierno | a279391 | 2016-10-04 08:15:08 +0000 | [diff] [blame] | 3123 | datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3124 | try: |
| 3125 | if item=="networks": |
| 3126 | net = descriptor["network"] |
| 3127 | net_name = net.pop("name") |
| 3128 | net_type = net.pop("type", "bridge") |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 3129 | net_public = net.pop("shared", False) |
| 3130 | net_ipprofile = net.pop("ip_profile", None) |
| tierno | a7d34d0 | 2017-02-23 14:42:07 +0100 | [diff] [blame] | 3131 | net_vlan = net.pop("vlan", None) |
| 3132 | content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3133 | elif item=="tenants": |
| 3134 | tenant = descriptor["tenant"] |
| 3135 | content = myvim.new_tenant(tenant["name"], tenant.get("description")) |
| 3136 | else: |
| tierno | 42026a0 | 2017-02-10 15:13:40 +0100 | [diff] [blame] | 3137 | raise NfvoException(item + "?", HTTP_Method_Not_Allowed) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 3138 | except vimconn.vimconnException as e: |
| tierno | f97fd27 | 2016-07-11 14:32:37 +0200 | [diff] [blame] | 3139 | 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] | 3140 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 3141 | return vim_action_get(mydb, tenant_id, datacenter, item, content) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3142 | |
| 3143 | def sdn_controller_create(mydb, tenant_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3144 | data = ovim.new_of_controller(sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3145 | logger.debug('New SDN controller created with uuid {}'.format(data)) |
| 3146 | return data |
| 3147 | |
| 3148 | def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3149 | data = ovim.edit_of_controller(controller_id, sdn_controller) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3150 | msg = 'SDN controller {} updated'.format(data) |
| 3151 | logger.debug(msg) |
| 3152 | return msg |
| 3153 | |
| 3154 | def sdn_controller_list(mydb, tenant_id, controller_id=None): |
| 3155 | if controller_id == None: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3156 | data = ovim.get_of_controllers() |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3157 | else: |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3158 | data = ovim.show_of_controller(controller_id) |
| 3159 | |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3160 | msg = 'SDN controller list:\n {}'.format(data) |
| 3161 | logger.debug(msg) |
| 3162 | return data |
| 3163 | |
| 3164 | def sdn_controller_delete(mydb, tenant_id, controller_id): |
| 3165 | select_ = ('uuid', 'config') |
| 3166 | datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_) |
| 3167 | for datacenter in datacenters: |
| 3168 | if datacenter['config']: |
| 3169 | config = yaml.load(datacenter['config']) |
| 3170 | if 'sdn-controller' in config and config['sdn-controller'] == controller_id: |
| 3171 | raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict) |
| 3172 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3173 | data = ovim.delete_of_controller(controller_id) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3174 | msg = 'SDN controller {} deleted'.format(data) |
| 3175 | logger.debug(msg) |
| 3176 | return msg |
| 3177 | |
| 3178 | def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping): |
| 3179 | controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id}) |
| 3180 | if len(controller) < 1: |
| 3181 | raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found) |
| 3182 | |
| 3183 | try: |
| 3184 | sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"] |
| 3185 | except: |
| 3186 | raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request) |
| 3187 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3188 | sdn_controller = ovim.show_of_controller(sdn_controller_id) |
| 3189 | switch_dpid = sdn_controller["dpid"] |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3190 | |
| 3191 | maps = list() |
| 3192 | for compute_node in sdn_port_mapping: |
| 3193 | #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid} |
| 3194 | element = dict() |
| 3195 | element["compute_node"] = compute_node["compute_node"] |
| 3196 | for port in compute_node["ports"]: |
| 3197 | element["pci"] = port.get("pci") |
| 3198 | element["switch_port"] = port.get("switch_port") |
| 3199 | element["switch_mac"] = port.get("switch_mac") |
| 3200 | if not element["pci"] or not (element["switch_port"] or element["switch_mac"]): |
| 3201 | raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'" |
| 3202 | " or 'switch_mac'", HTTP_Bad_Request) |
| 3203 | maps.append(dict(element)) |
| 3204 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3205 | 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] | 3206 | |
| 3207 | def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id): |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3208 | maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id}) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3209 | |
| 3210 | result = { |
| 3211 | "sdn-controller": None, |
| 3212 | "datacenter-id": datacenter_id, |
| 3213 | "dpid": None, |
| 3214 | "ports_mapping": list() |
| 3215 | } |
| 3216 | |
| 3217 | datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id) |
| 3218 | if datacenter['config']: |
| 3219 | config = yaml.load(datacenter['config']) |
| 3220 | if 'sdn-controller' in config: |
| 3221 | controller_id = config['sdn-controller'] |
| 3222 | sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id) |
| 3223 | result["sdn-controller"] = controller_id |
| 3224 | result["dpid"] = sdn_controller["dpid"] |
| 3225 | |
| Pablo Montes Moreno | 7e0e9c6 | 2017-03-27 12:42:32 +0200 | [diff] [blame] | 3226 | if result["sdn-controller"] == None or result["dpid"] == None: |
| 3227 | raise NfvoException("Not all SDN controller information for datacenter {} could be found: {}".format(datacenter_id, result), |
| 3228 | HTTP_Internal_Server_Error) |
| Pablo Montes Moreno | 3fbff9b | 2017-03-08 11:28:15 +0100 | [diff] [blame] | 3229 | |
| 3230 | if len(maps) == 0: |
| 3231 | return result |
| 3232 | |
| 3233 | ports_correspondence_dict = dict() |
| 3234 | for link in maps: |
| 3235 | if result["sdn-controller"] != link["ofc_id"]: |
| 3236 | raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 3237 | if result["dpid"] != link["switch_dpid"]: |
| 3238 | raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error) |
| 3239 | element = dict() |
| 3240 | element["pci"] = link["pci"] |
| 3241 | if link["switch_port"]: |
| 3242 | element["switch_port"] = link["switch_port"] |
| 3243 | if link["switch_mac"]: |
| 3244 | element["switch_mac"] = link["switch_mac"] |
| 3245 | |
| 3246 | if not link["compute_node"] in ports_correspondence_dict: |
| 3247 | content = dict() |
| 3248 | content["compute_node"] = link["compute_node"] |
| 3249 | content["ports"] = list() |
| 3250 | ports_correspondence_dict[link["compute_node"]] = content |
| 3251 | |
| 3252 | ports_correspondence_dict[link["compute_node"]]["ports"].append(element) |
| 3253 | |
| 3254 | for key in sorted(ports_correspondence_dict): |
| 3255 | result["ports_mapping"].append(ports_correspondence_dict[key]) |
| 3256 | |
| 3257 | return result |
| 3258 | |
| 3259 | def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id): |
| tierno | 639520f | 2017-04-05 19:55:36 +0200 | [diff] [blame^] | 3260 | return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id}) |