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