blob: 0c8cef652db98b6856e82b14beca47190e607063 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno7edb6752016-03-21 17:37:52 +01005# 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'''
25NFVO 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
tierno361275f2017-04-25 16:24:34 +020030# import imp
31# import json
tierno7edb6752016-03-21 17:37:52 +010032import yaml
tierno42fcc3b2016-07-06 17:20:40 +020033import utils
tiernob8569aa2018-08-24 11:34:54 +020034from utils import deprecated
tierno42026a02017-02-10 15:13:40 +010035import vim_thread
tiernof97fd272016-07-11 14:32:37 +020036from db_base import HTTP_Unauthorized, HTTP_Bad_Request, HTTP_Internal_Server_Error, HTTP_Not_Found,\
tierno7edb6752016-03-21 17:37:52 +010037 HTTP_Conflict, HTTP_Method_Not_Allowed
38import console_proxy_thread as cli
tiernoae4a8d12016-07-08 12:30:39 +020039import vimconn
40import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020041import collections
tierno66eba6e2017-11-10 17:09:18 +010042import math
tierno8e690322017-08-10 15:58:50 +020043from uuid import uuid4
tiernof97fd272016-07-11 14:32:37 +020044from db_base import db_base_Exception
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010045
tiernob3d36742017-03-03 23:51:05 +010046import nfvo_db
47from threading import Lock
tierno868220c2017-09-26 00:11:05 +020048import time as t
tierno01b3e172017-04-21 10:52:34 +020049from lib_osm_openvim import ovim as ovim_module
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +020050from lib_osm_openvim.ovim import ovimException
gcalvinoe580c7d2017-09-22 14:09:51 +020051from Crypto.PublicKey import RSA
tierno7edb6752016-03-21 17:37:52 +010052
tiernof1ba57e2017-09-07 12:23:19 +020053import osm_im.vnfd as vnfd_catalog
54import osm_im.nsd as nsd_catalog
tiernof1ba57e2017-09-07 12:23:19 +020055from pyangbind.lib.serialise import pybindJSONDecoder
tiernofc5f80b2018-05-29 16:00:43 +020056from copy import deepcopy
57
tiernof1ba57e2017-09-07 12:23:19 +020058
tierno7edb6752016-03-21 17:37:52 +010059global global_config
60global vimconn_imported
tierno73ad9e42016-09-12 18:11:11 +020061global logger
montesmoreno0c8def02016-12-22 12:16:23 +000062global default_volume_size
63default_volume_size = '5' #size in GB
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010064global ovim
65ovim = None
tiernoc5651792017-03-27 10:50:43 +020066global_config = None
tiernoae4a8d12016-07-08 12:30:39 +020067
tierno42026a02017-02-10 15:13:40 +010068vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
69vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
tiernob3d36742017-03-03 23:51:05 +010070vim_persistent_info = {}
tierno73ad9e42016-09-12 18:11:11 +020071logger = logging.getLogger('openmano.nfvo')
tiernob3d36742017-03-03 23:51:05 +010072task_lock = Lock()
tiernob3d36742017-03-03 23:51:05 +010073last_task_id = 0.0
tierno868220c2017-09-26 00:11:05 +020074db = None
75db_lock = Lock()
tierno7edb6752016-03-21 17:37:52 +010076
77class NfvoException(Exception):
tiernoae4a8d12016-07-08 12:30:39 +020078 def __init__(self, message, http_code):
79 self.http_code = http_code
80 Exception.__init__(self, message)
tierno7edb6752016-03-21 17:37:52 +010081
82
tiernob3d36742017-03-03 23:51:05 +010083def get_task_id():
84 global last_task_id
tierno868220c2017-09-26 00:11:05 +020085 task_id = t.time()
tiernob3d36742017-03-03 23:51:05 +010086 if task_id <= last_task_id:
87 task_id = last_task_id + 0.000001
88 last_task_id = task_id
tierno868220c2017-09-26 00:11:05 +020089 return "ACTION-{:.6f}".format(task_id)
90 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
tiernob3d36742017-03-03 23:51:05 +010091
92
tierno867ffe92017-03-27 12:50:34 +020093def new_task(name, params, depends=None):
tierno868220c2017-09-26 00:11:05 +020094 """Deprected!!!"""
tiernob3d36742017-03-03 23:51:05 +010095 task_id = get_task_id()
96 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
97 if depends:
98 task["depends"] = depends
tiernob3d36742017-03-03 23:51:05 +010099 return task
100
101
102def is_task_id(id):
tierno868220c2017-09-26 00:11:05 +0200103 return True if id[:5] == "TASK-" else False
tiernob3d36742017-03-03 23:51:05 +0100104
105
tierno42026a02017-02-10 15:13:40 +0100106def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
107 name = datacenter_name[:16]
108 if name not in vim_threads["names"]:
109 vim_threads["names"].append(name)
110 return name
tiernob3d36742017-03-03 23:51:05 +0100111 name = datacenter_name[:16] + "." + tenant_name[:16]
tierno42026a02017-02-10 15:13:40 +0100112 if name not in vim_threads["names"]:
113 vim_threads["names"].append(name)
114 return name
115 name = datacenter_id + "-" + tenant_id
116 vim_threads["names"].append(name)
117 return name
118
119
120def start_service(mydb):
tiernob3d36742017-03-03 23:51:05 +0100121 global db, global_config
122 db = nfvo_db.nfvo_db()
123 db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100124 global ovim
125
126 # Initialize openvim for SDN control
127 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
128 # TODO: review ovim.py to delete not needed configuration
129 ovim_configuration = {
tierno639520f2017-04-05 19:55:36 +0200130 'logger_name': 'openmano.ovim',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100131 'network_vlan_range_start': 1000,
132 'network_vlan_range_end': 4096,
tierno639520f2017-04-05 19:55:36 +0200133 'db_name': global_config["db_ovim_name"],
134 'db_host': global_config["db_ovim_host"],
135 'db_user': global_config["db_ovim_user"],
136 'db_passwd': global_config["db_ovim_passwd"],
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100137 'bridge_ifaces': {},
138 'mode': 'normal',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100139 'network_type': 'bridge',
140 #TODO: log_level_of should not be needed. To be modified in ovim
141 'log_level_of': 'DEBUG'
142 }
tierno42026a02017-02-10 15:13:40 +0100143 try:
tierno3fcfdb72017-10-24 07:48:24 +0200144 # starts ovim library
tierno46df9672017-05-26 13:12:21 +0200145 ovim = ovim_module.ovim(ovim_configuration)
146 ovim.start_service()
147
tierno3fcfdb72017-10-24 07:48:24 +0200148 #delete old unneeded vim_actions
149 clean_db(mydb)
150
151 # starts vim_threads
tierno46df9672017-05-26 13:12:21 +0200152 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
153 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
154 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
155 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
156 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
157 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
tierno42026a02017-02-10 15:13:40 +0100158 vims = mydb.get_rows(FROM=from_, SELECT=select_)
159 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200160 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
161 'datacenter_id': vim.get('datacenter_id')}
tierno42026a02017-02-10 15:13:40 +0100162 if vim["config"]:
163 extra.update(yaml.load(vim["config"]))
164 if vim.get('dt_config'):
165 extra.update(yaml.load(vim["dt_config"]))
166 if vim["type"] not in vimconn_imported:
167 module_info=None
168 try:
169 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200170 pkg = __import__("osm_ro." + module)
171 vim_conn = getattr(pkg, module)
172 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
173 # vim_conn = imp.load_module(vim["type"], *module_info)
tierno42026a02017-02-10 15:13:40 +0100174 vimconn_imported[vim["type"]] = vim_conn
175 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200176 # if module_info and module_info[0]:
177 # file.close(module_info[0])
tiernocdee8cc2017-04-25 13:42:06 +0200178 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
tiernob3d36742017-03-03 23:51:05 +0100179 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100180
tierno867ffe92017-03-27 12:50:34 +0200181 thread_id = vim['datacenter_tenant_id']
tiernob3d36742017-03-03 23:51:05 +0100182 vim_persistent_info[thread_id] = {}
tierno42026a02017-02-10 15:13:40 +0100183 try:
184 #if not tenant:
185 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
186 myvim = vimconn_imported[ vim["type"] ].vimconnector(
tiernob3d36742017-03-03 23:51:05 +0100187 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
188 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
189 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
190 user=vim['user'], passwd=vim['passwd'],
191 config=extra, persistent_info=vim_persistent_info[thread_id]
192 )
tierno9c22f2d2017-10-09 16:23:55 +0200193 except vimconn.vimconnException as e:
194 myvim = e
195 logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
196 vim['datacenter_id'], e))
tierno42026a02017-02-10 15:13:40 +0100197 except Exception as e:
tierno46df9672017-05-26 13:12:21 +0200198 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
199 HTTP_Internal_Server_Error)
200 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
201 vim['vim_tenant_id'])
tiernod3750b32018-07-20 15:33:08 +0200202 new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'],
tierno867ffe92017-03-27 12:50:34 +0200203 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +0100204 new_thread.start()
tierno42026a02017-02-10 15:13:40 +0100205 vim_threads["running"][thread_id] = new_thread
206 except db_base_Exception as e:
207 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno46df9672017-05-26 13:12:21 +0200208 except ovim_module.ovimException as e:
209 message = str(e)
210 if message[:22] == "DATABASE wrong version":
211 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
212 "at host {dbhost}".format(
213 msg=message[22:-3], dbname=global_config["db_ovim_name"],
214 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
215 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
216 raise NfvoException(message, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100217
tierno867ffe92017-03-27 12:50:34 +0200218
tierno42026a02017-02-10 15:13:40 +0100219def stop_service():
tiernoc5651792017-03-27 10:50:43 +0200220 global ovim, global_config
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100221 if ovim:
222 ovim.stop_service()
tierno42026a02017-02-10 15:13:40 +0100223 for thread_id,thread in vim_threads["running"].items():
tierno868220c2017-09-26 00:11:05 +0200224 thread.insert_task("exit")
tierno42026a02017-02-10 15:13:40 +0100225 vim_threads["deleting"][thread_id] = thread
tiernob3d36742017-03-03 23:51:05 +0100226 vim_threads["running"] = {}
tiernoc5651792017-03-27 10:50:43 +0200227 if global_config and global_config.get("console_thread"):
228 for thread in global_config["console_thread"]:
229 thread.terminate = True
tiernob3d36742017-03-03 23:51:05 +0100230
tierno6ddeded2017-05-16 15:40:26 +0200231def get_version():
232 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
233 global_config["version_date"] ))
234
tierno3fcfdb72017-10-24 07:48:24 +0200235def clean_db(mydb):
236 """
237 Clean unused or old entries at database to avoid unlimited growing
238 :param mydb: database connector
239 :return: None
240 """
241 # get and delete unused vim_actions: all elements deleted, one week before, instance not present
242 now = t.time()-3600*24*7
243 instance_action_id = None
244 nb_deleted = 0
245 while True:
246 actions_to_delete = mydb.get_rows(
247 SELECT=("item", "item_id", "instance_action_id"),
248 FROM="vim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
249 "left join instance_scenarios as i on ia.instance_id=i.uuid",
250 WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None,
251 "va.status": ("DONE", "SUPERSEDED")},
252 LIMIT=100
253 )
254 for to_delete in actions_to_delete:
255 mydb.delete_row(FROM="vim_actions", WHERE=to_delete)
256 if instance_action_id != to_delete["instance_action_id"]:
257 instance_action_id = to_delete["instance_action_id"]
258 mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id})
259 nb_deleted += len(actions_to_delete)
260 if len(actions_to_delete) < 100:
261 break
262 if nb_deleted:
263 logger.debug("Removed {} unused vim_actions".format(nb_deleted))
264
265
tierno42026a02017-02-10 15:13:40 +0100266
tierno7edb6752016-03-21 17:37:52 +0100267def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
268 '''Obtain flavorList
269 return result, content:
270 <0, error_text upon error
271 nb_records, flavor_list on success
272 '''
273 WHERE_dict={}
274 WHERE_dict['vnf_id'] = vnf_id
275 if nfvo_tenant is not None:
276 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100277
tierno7edb6752016-03-21 17:37:52 +0100278 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
279 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200280 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
281 #print "get_flavor_list result:", result
282 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +0100283 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +0200284 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +0100285 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +0200286 return flavorList
tierno7edb6752016-03-21 17:37:52 +0100287
tiernob3d36742017-03-03 23:51:05 +0100288
tierno7edb6752016-03-21 17:37:52 +0100289def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
tierno16e3dd42018-04-24 12:52:40 +0200290 """
291 Get used images of all vms belonging to this VNFD
292 :param mydb: database conector
293 :param vnf_id: vnfd uuid
294 :param nfvo_tenant: tenant, not used
295 :return: The list of image uuid used
296 """
297 image_list = []
298 vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id})
299 for vm in vms:
300 if vm["image_id"] not in image_list:
301 image_list.append(vm["image_id"])
302 if vm["image_list"]:
303 vm_image_list = yaml.load(vm["image_list"])
304 for image_dict in vm_image_list:
305 if image_dict["image_id"] not in image_list:
306 image_list.append(image_dict["image_id"])
307 return image_list
tierno7edb6752016-03-21 17:37:52 +0100308
tiernob3d36742017-03-03 23:51:05 +0100309
tiernoa2793912016-10-04 08:15:08 +0000310def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
tiernocbb52052018-05-31 18:57:30 +0200311 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None, ignore_errors=False):
tierno7edb6752016-03-21 17:37:52 +0100312 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tierno42026a02017-02-10 15:13:40 +0100313 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100314 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200315 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100316 '''
317 WHERE_dict={}
318 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
319 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000320 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100321 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
322 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000323 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
324 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100325 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'
tierno8008c3a2016-10-13 15:34:28 +0000326 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
tierno7edb6752016-03-21 17:37:52 +0100327 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
tierno8008c3a2016-10-13 15:34:28 +0000328 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100329 else:
330 from_ = 'datacenters as d'
331 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200332 try:
333 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
334 vim_dict={}
335 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200336 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
tierno16e3dd42018-04-24 12:52:40 +0200337 'datacenter_id': vim.get('datacenter_id'),
tiernob6434212018-04-26 16:27:47 +0200338 '_vim_type_internal': vim.get('type')}
tierno8008c3a2016-10-13 15:34:28 +0000339 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200340 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000341 if vim.get('dt_config'):
342 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200343 if vim["type"] not in vimconn_imported:
344 module_info=None
345 try:
346 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200347 pkg = __import__("osm_ro." + module)
348 vim_conn = getattr(pkg, module)
349 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
350 # vim_conn = imp.load_module(vim["type"], *module_info)
tiernof97fd272016-07-11 14:32:37 +0200351 vimconn_imported[vim["type"]] = vim_conn
352 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200353 # if module_info and module_info[0]:
354 # file.close(module_info[0])
tiernocbb52052018-05-31 18:57:30 +0200355 if ignore_errors:
356 logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
357 vim["type"], module, type(e).__name__, str(e)))
358 continue
tiernof97fd272016-07-11 14:32:37 +0200359 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
360 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100361
tierno7edb6752016-03-21 17:37:52 +0100362 try:
tierno867ffe92017-03-27 12:50:34 +0200363 if 'datacenter_tenant_id' in vim:
364 thread_id = vim["datacenter_tenant_id"]
tiernob3d36742017-03-03 23:51:05 +0100365 if thread_id not in vim_persistent_info:
366 vim_persistent_info[thread_id] = {}
367 persistent_info = vim_persistent_info[thread_id]
368 else:
369 persistent_info = {}
tiernof97fd272016-07-11 14:32:37 +0200370 #if not tenant:
371 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
372 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
373 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tiernob3d36742017-03-03 23:51:05 +0100374 tenant_id=vim.get('vim_tenant_id',vim_tenant),
375 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tierno42026a02017-02-10 15:13:40 +0100376 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200377 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernob3d36742017-03-03 23:51:05 +0100378 config=extra, persistent_info=persistent_info
tiernof97fd272016-07-11 14:32:37 +0200379 )
380 except Exception as e:
tiernocbb52052018-05-31 18:57:30 +0200381 if ignore_errors:
382 logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)))
383 continue
tiernoa3572692018-05-14 13:09:33 +0200384 http_code = HTTP_Internal_Server_Error
385 if isinstance(e, vimconn.vimconnException):
386 http_code = e.http_code
387 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code)
tiernof97fd272016-07-11 14:32:37 +0200388 return vim_dict
389 except db_base_Exception as e:
390 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno42026a02017-02-10 15:13:40 +0100391
tiernob3d36742017-03-03 23:51:05 +0100392
tierno7edb6752016-03-21 17:37:52 +0100393def rollback(mydb, vims, rollback_list):
394 undeleted_items=[]
tierno42026a02017-02-10 15:13:40 +0100395 #delete things by reverse order
tierno7edb6752016-03-21 17:37:52 +0100396 for i in range(len(rollback_list)-1, -1, -1):
397 item = rollback_list[i]
398 if item["where"]=="vim":
399 if item["vim_id"] not in vims:
400 continue
tierno56d73d22017-08-02 13:53:02 +0200401 if is_task_id(item["uuid"]):
402 continue
403 vim = vims[item["vim_id"]]
tiernoae4a8d12016-07-08 12:30:39 +0200404 try:
405 if item["what"]=="image":
406 vim.delete_image(item["uuid"])
tierno868220c2017-09-26 00:11:05 +0200407 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200408 elif item["what"]=="flavor":
409 vim.delete_flavor(item["uuid"])
tiernoad6bdd42018-01-10 10:43:46 +0100410 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200411 elif item["what"]=="network":
412 vim.delete_network(item["uuid"])
413 elif item["what"]=="vm":
414 vim.delete_vminstance(item["uuid"])
415 except vimconn.vimconnException as e:
416 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
417 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200418 except db_base_Exception as e:
419 logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e))
tierno42026a02017-02-10 15:13:40 +0100420
tierno7edb6752016-03-21 17:37:52 +0100421 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200422 try:
423 if item["what"]=="image":
424 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
425 elif item["what"]=="flavor":
426 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
427 except db_base_Exception as e:
428 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
429 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno42026a02017-02-10 15:13:40 +0100430 if len(undeleted_items)==0:
tierno7edb6752016-03-21 17:37:52 +0100431 return True," Rollback successful."
432 else:
433 return False," Rollback fails to delete: " + str(undeleted_items)
tierno42026a02017-02-10 15:13:40 +0100434
tiernob3d36742017-03-03 23:51:05 +0100435
tiernoafed5f12017-01-26 17:57:43 +0100436def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100437 global global_config
tierno42026a02017-02-10 15:13:40 +0100438 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
tierno7edb6752016-03-21 17:37:52 +0100439 vnfc_interfaces={}
440 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100441 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100442 #dataplane interfaces
443 for numa in vnfc.get("numas",() ):
444 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100445 if interface["name"] in name_dict:
446 raise NfvoException(
447 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
448 vnfc["name"], interface["name"]),
449 HTTP_Bad_Request)
450 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100451 #bridge interfaces
452 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100453 if interface["name"] in name_dict:
454 raise NfvoException(
455 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
456 vnfc["name"], interface["name"]),
457 HTTP_Bad_Request)
458 name_dict[ interface["name"] ] = "overlay"
459 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100460 # check bood-data info
tierno40e1bce2017-08-09 09:12:04 +0200461 # if "boot-data" in vnfc:
462 # # check that user-data is incompatible with users and config-files
463 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
464 # raise NfvoException(
465 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
466 # HTTP_Bad_Request)
tierno36c0b172017-01-12 18:32:28 +0100467
tierno7edb6752016-03-21 17:37:52 +0100468 #check if the info in external_connections matches with the one in the vnfcs
469 name_list=[]
470 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
471 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100472 raise NfvoException(
473 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
474 external_connection["name"]),
475 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100476 name_list.append(external_connection["name"])
477 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100478 raise NfvoException(
479 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
480 external_connection["name"], external_connection["VNFC"]),
481 HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100482
tierno7edb6752016-03-21 17:37:52 +0100483 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100484 raise NfvoException(
485 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
486 external_connection["name"],
487 external_connection["local_iface_name"]),
488 HTTP_Bad_Request )
tierno42026a02017-02-10 15:13:40 +0100489
tierno7edb6752016-03-21 17:37:52 +0100490 #check if the info in internal_connections matches with the one in the vnfcs
491 name_list=[]
492 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
493 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100494 raise NfvoException(
495 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
496 internal_connection["name"]),
497 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100498 name_list.append(internal_connection["name"])
499 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100500
501 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
502 raise NfvoException(
503 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
504 internal_connection["name"],
505 'ptp' if vnf_descriptor_version==1 else 'e-line',
506 'data' if vnf_descriptor_version==1 else "e-lan"),
507 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100508 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100509 vnf = port["VNFC"]
510 iface = port["local_iface_name"]
511 if vnf not in vnfc_interfaces:
512 raise NfvoException(
513 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
514 internal_connection["name"], vnf),
515 HTTP_Bad_Request)
516 if iface not in vnfc_interfaces[ vnf ]:
517 raise NfvoException(
518 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
519 internal_connection["name"], iface),
520 HTTP_Bad_Request)
521 return -HTTP_Bad_Request,
522 if vnf_descriptor_version==1 and "type" not in internal_connection:
523 if vnfc_interfaces[vnf][iface] == "overlay":
524 internal_connection["type"] = "bridge"
525 else:
526 internal_connection["type"] = "data"
527 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
528 if vnfc_interfaces[vnf][iface] == "overlay":
529 internal_connection["implementation"] = "overlay"
530 else:
531 internal_connection["implementation"] = "underlay"
532 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
533 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
534 raise NfvoException(
535 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
536 internal_connection["name"],
537 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
538 'data' if vnf_descriptor_version==1 else 'underlay'),
539 HTTP_Bad_Request)
540 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
541 vnfc_interfaces[vnf][iface] == "underlay":
542 raise NfvoException(
543 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
544 internal_connection["name"], iface,
545 'data' if vnf_descriptor_version==1 else 'underlay',
546 'bridge' if vnf_descriptor_version==1 else 'overlay'),
547 HTTP_Bad_Request)
548
tierno7edb6752016-03-21 17:37:52 +0100549
tierno56d73d22017-08-02 13:53:02 +0200550def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=None):
tierno7edb6752016-03-21 17:37:52 +0100551 #look if image exist
552 if only_create_at_vim:
553 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000554 if return_on_error == None:
555 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100556 else:
garciadeblas14480452017-01-10 13:08:07 +0100557 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200558 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
559 else:
560 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200561 if len(images)>=1:
562 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100563 else:
garciadeblas14480452017-01-10 13:08:07 +0100564 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100565 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200566 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
567 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100568 }
garciadeblas14480452017-01-10 13:08:07 +0100569 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200570 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
571 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100572 #create image at every vim
573 for vim_id,vim in vims.iteritems():
tierno868220c2017-09-26 00:11:05 +0200574 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100575 image_created="false"
576 #look at database
tierno868220c2017-09-26 00:11:05 +0200577 image_db = mydb.get_rows(FROM="datacenters_images",
578 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100579 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200580 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200581 if image_dict['location'] is not None:
582 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
583 else:
garciadeblas30833382017-01-09 09:46:31 +0100584 filter_dict = {}
585 filter_dict['name'] = image_dict['universal_name']
586 if image_dict.get('checksum') != None:
587 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000588 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200589 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100590 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200591 if len(vim_images) > 1:
garciadeblas3fa2c052017-01-05 12:00:08 +0100592 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000593 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100594 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200595 else:
garciadeblas14480452017-01-10 13:08:07 +0100596 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
597 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200598
tiernoae4a8d12016-07-08 12:30:39 +0200599 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100600 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tierno42026a02017-02-10 15:13:40 +0100601 try:
garciadeblas14480452017-01-10 13:08:07 +0100602 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
603 if image_dict['location']:
604 image_vim_id = vim.new_image(image_dict)
605 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
606 image_created="true"
607 else:
garciadeblasb6153a22017-02-06 15:38:33 +0100608 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
609 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200610 except vimconn.vimconnException as e:
611 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100612 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200613 raise
tierno5e91eb82016-10-04 09:39:07 +0000614 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100615 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200616 continue
617 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000618 if return_on_error:
619 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
620 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200621 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000622 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100623 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200624 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200625 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100626 #add new vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200627 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
628 'image_id':image_mano_id,
629 'vim_id': image_vim_id,
630 'created':image_created})
tierno7edb6752016-03-21 17:37:52 +0100631 elif image_db[0]["vim_id"]!=image_vim_id:
632 #modify existing vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200633 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_vim_id':vim_id, 'image_id':image_mano_id})
tierno42026a02017-02-10 15:13:40 +0100634
tiernof97fd272016-07-11 14:32:37 +0200635 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100636
tiernob3d36742017-03-03 23:51:05 +0100637
tierno5e91eb82016-10-04 09:39:07 +0000638def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
garciadeblas79d1a1a2017-12-11 16:07:07 +0100639 temp_flavor_dict= {'disk':flavor_dict.get('disk',0),
tierno7edb6752016-03-21 17:37:52 +0100640 'ram':flavor_dict.get('ram'),
641 'vcpus':flavor_dict.get('vcpus'),
642 }
643 if 'extended' in flavor_dict and flavor_dict['extended']==None:
644 del flavor_dict['extended']
645 if 'extended' in flavor_dict:
646 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
647
648 #look if flavor exist
649 if only_create_at_vim:
650 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000651 if return_on_error == None:
652 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100653 else:
tiernof97fd272016-07-11 14:32:37 +0200654 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
655 if len(flavors)>=1:
656 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100657 else:
658 #create flavor
659 #create one by one the images of aditional disks
660 dev_image_list=[] #list of images
661 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
662 dev_nb=0
663 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200664 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100665 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200666 image_dict={}
667 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
668 image_dict['universal_name']=device.get('image name')
669 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
670 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100671 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200672 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100673 image_metadata_dict = device.get('image metadata', None)
674 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100675 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100676 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
677 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200678 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
679 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100680 dev_image_list.append(image_id)
tierno42026a02017-02-10 15:13:40 +0100681 dev_nb += 1
tierno7edb6752016-03-21 17:37:52 +0100682 temp_flavor_dict['name'] = flavor_dict['name']
683 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200684 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
685 flavor_mano_id= content
686 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100687 #create flavor at every vim
688 if 'uuid' in flavor_dict:
689 del flavor_dict['uuid']
690 flavor_vim_id=None
691 for vim_id,vim in vims.items():
tierno868220c2017-09-26 00:11:05 +0200692 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100693 flavor_created="false"
694 #look at database
tierno868220c2017-09-26 00:11:05 +0200695 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
696 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100697 #look at VIM if this flavor exist SKIPPED
698 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
699 #if res_vim < 0:
700 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
701 # continue
702 #elif res_vim==0:
tierno42026a02017-02-10 15:13:40 +0100703
tiernof1ba57e2017-09-07 12:23:19 +0200704 # Create the flavor in VIM
705 # Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000706 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100707 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
tiernof1ba57e2017-09-07 12:23:19 +0200708 # make a copy of original devices
tierno7edb6752016-03-21 17:37:52 +0100709 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000710
tierno7edb6752016-03-21 17:37:52 +0100711 for device in flavor_dict["extended"].get("devices",[]):
712 dev={}
713 dev.update(device)
714 devices_original.append(dev)
715 if 'image' in device:
716 del device['image']
717 if 'image metadata' in device:
718 del device['image metadata']
tiernof1ba57e2017-09-07 12:23:19 +0200719 if 'image checksum' in device:
720 del device['image checksum']
721 dev_nb = 0
tierno7edb6752016-03-21 17:37:52 +0100722 for index in range(0,len(devices_original)) :
723 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000724 if "image" not in device and "image name" not in device:
tiernoecc68392018-09-06 13:47:11 +0200725 # if 'size' in device:
726 disk_list.append({'size': device.get('size', default_volume_size), 'name': device.get('name')})
tierno7edb6752016-03-21 17:37:52 +0100727 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200728 image_dict={}
729 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
730 image_dict['universal_name']=device.get('image name')
731 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
732 image_dict['location']=device.get('image')
tiernof1ba57e2017-09-07 12:23:19 +0200733 # image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200734 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100735 image_metadata_dict = device.get('image metadata', None)
736 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100737 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100738 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
739 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200740 image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error )
tierno7edb6752016-03-21 17:37:52 +0100741 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200742 image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error)
montesmoreno0c8def02016-12-22 12:16:23 +0000743
744 #save disk information (image must be based on and size
745 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
746
tierno7edb6752016-03-21 17:37:52 +0100747 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
748 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200749 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100750 #check that this vim_id exist in VIM, if not create
751 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200752 try:
753 vim.get_flavor(flavor_vim_id)
754 continue #flavor exist
755 except vimconn.vimconnException:
756 pass
tierno7edb6752016-03-21 17:37:52 +0100757 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200758 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
759 try:
tiernocf157a82017-01-30 14:07:06 +0100760 flavor_vim_id = None
761 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
762 flavor_create="false"
763 except vimconn.vimconnException as e:
764 pass
765 try:
766 if not flavor_vim_id:
767 flavor_vim_id = vim.new_flavor(flavor_dict)
768 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
769 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200770 except vimconn.vimconnException as e:
771 if return_on_error:
772 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200773 raise
tiernoae4a8d12016-07-08 12:30:39 +0200774 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000775 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200776 continue
tierno7edb6752016-03-21 17:37:52 +0100777 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200778 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100779 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000780 extended_devices_yaml = None
781 if len(disk_list) > 0:
782 extended_devices = dict()
783 extended_devices['disks'] = disk_list
784 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
785 mydb.new_row('datacenters_flavors',
tierno868220c2017-09-26 00:11:05 +0200786 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
787 'created': flavor_created, 'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100788 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
789 #modify existing vim_id at datacenters_flavors
tierno868220c2017-09-26 00:11:05 +0200790 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
791 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno42026a02017-02-10 15:13:40 +0100792
tiernof97fd272016-07-11 14:32:37 +0200793 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100794
tiernob3d36742017-03-03 23:51:05 +0100795
tiernof1ba57e2017-09-07 12:23:19 +0200796def get_str(obj, field, length):
797 """
798 Obtain the str value,
799 :param obj:
800 :param length:
801 :return:
802 """
803 value = obj.get(field)
804 if value is not None:
805 value = str(value)[:length]
806 return value
807
808def _lookfor_or_create_image(db_image, mydb, descriptor):
809 """
810 fill image content at db_image dictionary. Check if the image with this image and checksum exist
811 :param db_image: dictionary to insert data
812 :param mydb: database connector
813 :param descriptor: yang descriptor
814 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
815 """
816
817 db_image["name"] = get_str(descriptor, "image", 255)
818 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
819 if not db_image["checksum"]: # Ensure that if empty string, None is stored
820 db_image["checksum"] = None
821 if db_image["name"].startswith("/"):
822 db_image["location"] = db_image["name"]
823 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
824 else:
825 db_image["universal_name"] = db_image["name"]
826 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
827 'checksum': db_image['checksum']})
828 if existing_images:
829 return existing_images[0]["uuid"]
830 else:
831 image_uuid = str(uuid4())
832 db_image["uuid"] = image_uuid
833 return None
834
835def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
836 """
837 Parses an OSM IM vnfd_catalog and insert at DB
838 :param mydb:
839 :param tenant_id:
840 :param vnf_descriptor:
841 :return: The list of cretated vnf ids
842 """
843 try:
844 myvnfd = vnfd_catalog.vnfd()
tiernoa9550202017-09-22 13:31:35 +0200845 try:
tiernoad6bdd42018-01-10 10:43:46 +0100846 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True)
tiernoa9550202017-09-22 13:31:35 +0200847 except Exception as e:
tiernob2880eb2017-10-04 15:04:53 +0200848 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200849 db_vnfs = []
850 db_nets = []
851 db_vms = []
852 db_vms_index = 0
853 db_interfaces = []
854 db_images = []
855 db_flavors = []
tierno41a69812018-02-16 14:34:33 +0100856 db_ip_profiles_index = 0
857 db_ip_profiles = []
tiernof1ba57e2017-09-07 12:23:19 +0200858 uuid_list = []
859 vnfd_uuid_list = []
tiernoe18ba432017-10-12 10:22:45 +0200860 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
861 if not vnfd_catalog_descriptor:
862 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
863 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
864 if not vnfd_descriptor_list:
865 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
tiernob2880eb2017-10-04 15:04:53 +0200866 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
867 vnfd = vnfd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +0200868
869 # table vnf
870 vnf_uuid = str(uuid4())
871 uuid_list.append(vnf_uuid)
872 vnfd_uuid_list.append(vnf_uuid)
tierno66eba6e2017-11-10 17:09:18 +0100873 vnfd_id = get_str(vnfd, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +0200874 db_vnf = {
875 "uuid": vnf_uuid,
tierno66eba6e2017-11-10 17:09:18 +0100876 "osm_id": vnfd_id,
tiernof1ba57e2017-09-07 12:23:19 +0200877 "name": get_str(vnfd, "name", 255),
878 "description": get_str(vnfd, "description", 255),
879 "tenant_id": tenant_id,
880 "vendor": get_str(vnfd, "vendor", 255),
881 "short_name": get_str(vnfd, "short-name", 255),
882 "descriptor": str(vnf_descriptor)[:60000]
883 }
884
tiernoe18ba432017-10-12 10:22:45 +0200885 for vnfd_descriptor in vnfd_descriptor_list:
886 if vnfd_descriptor["id"] == str(vnfd["id"]):
887 break
888
tierno41a69812018-02-16 14:34:33 +0100889 # table ip_profiles (ip-profiles)
890 ip_profile_name2db_table_index = {}
891 for ip_profile in vnfd.get("ip-profiles").itervalues():
892 db_ip_profile = {
893 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
894 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
895 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
896 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
897 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
898 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
899 }
900 dns_list = []
901 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
902 dns_list.append(str(dns.get("address")))
903 db_ip_profile["dns_address"] = ";".join(dns_list)
904 if ip_profile["ip-profile-params"].get('security-group'):
905 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
906 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
907 db_ip_profiles_index += 1
908 db_ip_profiles.append(db_ip_profile)
909
tiernof1ba57e2017-09-07 12:23:19 +0200910 # table nets (internal-vld)
911 net_id2uuid = {} # for mapping interface with network
912 for vld in vnfd.get("internal-vld").itervalues():
913 net_uuid = str(uuid4())
914 uuid_list.append(net_uuid)
915 db_net = {
916 "name": get_str(vld, "name", 255),
917 "vnf_id": vnf_uuid,
918 "uuid": net_uuid,
919 "description": get_str(vld, "description", 255),
tierno1df468d2018-07-06 14:25:16 +0200920 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +0200921 "type": "bridge", # TODO adjust depending on connection point type
922 }
923 net_id2uuid[vld.get("id")] = net_uuid
924 db_nets.append(db_net)
tierno41a69812018-02-16 14:34:33 +0100925 # ip-profile, link db_ip_profile with db_sce_net
926 if vld.get("ip-profile-ref"):
927 ip_profile_name = vld.get("ip-profile-ref")
928 if ip_profile_name not in ip_profile_name2db_table_index:
929 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
930 "'{}'. Reference to a non-existing 'ip_profiles'".format(
931 str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
932 HTTP_Bad_Request)
933 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid
934 else: #check no ip-address has been defined
tierno45140f52018-03-26 12:11:46 +0200935 for icp in vld.get("internal-connection-point").itervalues():
tierno41a69812018-02-16 14:34:33 +0100936 if icp.get("ip-address"):
937 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
938 "contains an ip-address but no ip-profile has been defined at VLD".format(
939 str(vnfd["id"]), str(vld["id"]), str(icp["id"])),
940 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200941
tiernocf596692017-11-20 15:47:51 +0100942 # connection points vaiable declaration
943 cp_name2iface_uuid = {}
944 cp_name2vm_uuid = {}
945 cp_name2db_interface = {}
tiernob6990792018-11-13 10:37:42 +0100946 vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU
tiernocf596692017-11-20 15:47:51 +0100947
tiernof1ba57e2017-09-07 12:23:19 +0200948 # table vms (vdus)
949 vdu_id2uuid = {}
950 vdu_id2db_table_index = {}
951 for vdu in vnfd.get("vdu").itervalues():
tierno41a69812018-02-16 14:34:33 +0100952
953 for vdu_descriptor in vnfd_descriptor["vdu"]:
954 if vdu_descriptor["id"] == str(vdu["id"]):
955 break
tiernof1ba57e2017-09-07 12:23:19 +0200956 vm_uuid = str(uuid4())
957 uuid_list.append(vm_uuid)
tierno66eba6e2017-11-10 17:09:18 +0100958 vdu_id = get_str(vdu, "id", 255)
tiernof1ba57e2017-09-07 12:23:19 +0200959 db_vm = {
960 "uuid": vm_uuid,
tierno66eba6e2017-11-10 17:09:18 +0100961 "osm_id": vdu_id,
tiernof1ba57e2017-09-07 12:23:19 +0200962 "name": get_str(vdu, "name", 255),
963 "description": get_str(vdu, "description", 255),
tiernob6990792018-11-13 10:37:42 +0100964 "pdu_type": get_str(vdu, "pdu-type", 255),
tiernof1ba57e2017-09-07 12:23:19 +0200965 "vnf_id": vnf_uuid,
966 }
967 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
968 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
969 if vdu.get("count"):
970 db_vm["count"] = int(vdu["count"])
971
972 # table image
973 image_present = False
974 if vdu.get("image"):
975 image_present = True
976 db_image = {}
977 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
978 if not image_uuid:
979 image_uuid = db_image["uuid"]
980 db_images.append(db_image)
981 db_vm["image_id"] = image_uuid
tierno16e3dd42018-04-24 12:52:40 +0200982 if vdu.get("alternative-images"):
983 vm_alternative_images = []
984 for alt_image in vdu.get("alternative-images").itervalues():
985 db_image = {}
986 image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image)
987 if not image_uuid:
988 image_uuid = db_image["uuid"]
989 db_images.append(db_image)
990 vm_alternative_images.append({
991 "image_id": image_uuid,
992 "vim_type": str(alt_image["vim-type"]),
993 # "universal_name": str(alt_image["image"]),
994 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
995 })
996
997 db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256)
tiernof1ba57e2017-09-07 12:23:19 +0200998
999 # volumes
1000 devices = []
1001 if vdu.get("volumes"):
tierno1df468d2018-07-06 14:25:16 +02001002 for volume_key in vdu["volumes"]:
tiernof1ba57e2017-09-07 12:23:19 +02001003 volume = vdu["volumes"][volume_key]
1004 if not image_present:
1005 # Convert the first volume to vnfc.image
1006 image_present = True
1007 db_image = {}
1008 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
1009 if not image_uuid:
1010 image_uuid = db_image["uuid"]
1011 db_images.append(db_image)
1012 db_vm["image_id"] = image_uuid
1013 else:
1014 # Add Openmano devices
tierno1df468d2018-07-06 14:25:16 +02001015 device = {"name": str(volume.get("name"))}
tiernof1ba57e2017-09-07 12:23:19 +02001016 device["type"] = str(volume.get("device-type"))
1017 if volume.get("size"):
1018 device["size"] = int(volume["size"])
1019 if volume.get("image"):
1020 device["image name"] = str(volume["image"])
1021 if volume.get("image-checksum"):
1022 device["image checksum"] = str(volume["image-checksum"])
tierno1df468d2018-07-06 14:25:16 +02001023
tiernof1ba57e2017-09-07 12:23:19 +02001024 devices.append(device)
1025
tierno66eba6e2017-11-10 17:09:18 +01001026 # cloud-init
1027 boot_data = {}
1028 if vdu.get("cloud-init"):
1029 boot_data["user-data"] = str(vdu["cloud-init"])
1030 elif vdu.get("cloud-init-file"):
1031 # TODO Where this file content is present???
1032 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1033 boot_data["user-data"] = str(vdu["cloud-init-file"])
1034
1035 if vdu.get("supplemental-boot-data"):
1036 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1037 boot_data['boot-data-drive'] = True
1038 if vdu["supplemental-boot-data"].get('config-file'):
1039 om_cfgfile_list = list()
1040 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
1041 # TODO Where this file content is present???
1042 cfg_source = str(custom_config_file["source"])
1043 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1044 "content": cfg_source})
1045 boot_data['config-files'] = om_cfgfile_list
1046 if boot_data:
1047 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
1048
1049 db_vms.append(db_vm)
1050 db_vms_index += 1
1051
1052 # table interfaces (internal/external interfaces)
1053 flavor_epa_interfaces = []
tierno66eba6e2017-11-10 17:09:18 +01001054 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1055 for iface in vdu.get("interface").itervalues():
1056 flavor_epa_interface = {}
1057 iface_uuid = str(uuid4())
1058 uuid_list.append(iface_uuid)
1059 db_interface = {
1060 "uuid": iface_uuid,
1061 "internal_name": get_str(iface, "name", 255),
1062 "vm_id": vm_uuid,
1063 }
1064 flavor_epa_interface["name"] = db_interface["internal_name"]
1065 if iface.get("virtual-interface").get("vpci"):
1066 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1067 flavor_epa_interface["vpci"] = db_interface["vpci"]
1068
1069 if iface.get("virtual-interface").get("bandwidth"):
1070 bps = int(iface.get("virtual-interface").get("bandwidth"))
1071 db_interface["bw"] = int(math.ceil(bps/1000000.0))
1072 flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"])
1073
1074 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1075 db_interface["type"] = "mgmt"
garciadeblas31e141b2018-10-25 18:33:19 +02001076 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
tierno66eba6e2017-11-10 17:09:18 +01001077 db_interface["type"] = "bridge"
1078 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1079 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1080 db_interface["type"] = "data"
1081 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1082 flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \
1083 else "yes"
1084 flavor_epa_interfaces.append(flavor_epa_interface)
1085 else:
1086 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1087 "-interface':'type':'{}'. Interface type is not supported".format(
1088 vnfd_id, vdu_id, iface.get("virtual-interface").get("type")),
1089 HTTP_Bad_Request)
1090
tiernoe72710b2018-07-23 16:16:00 +02001091 if iface.get("mgmt-interface"):
1092 db_interface["type"] = "mgmt"
1093
tierno66eba6e2017-11-10 17:09:18 +01001094 if iface.get("external-connection-point-ref"):
1095 try:
1096 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1097 db_interface["external_name"] = get_str(cp, "name", 255)
1098 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1099 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1100 cp_name2db_interface[db_interface["external_name"]] = db_interface
1101 for cp_descriptor in vnfd_descriptor["connection-point"]:
1102 if cp_descriptor["name"] == db_interface["external_name"]:
1103 break
1104 else:
1105 raise KeyError()
1106
1107 if vdu_id in vdu_id2cp_name:
1108 vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU
1109 else:
1110 vdu_id2cp_name[vdu_id] = db_interface["external_name"]
1111
1112 # port security
1113 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
1114 db_interface["port_security"] = 0
1115 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
1116 db_interface["port_security"] = 1
1117 except KeyError:
1118 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1119 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1120 " at connection-point".format(
1121 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1122 cp=iface.get("vnfd-connection-point-ref")),
1123 HTTP_Bad_Request)
1124 elif iface.get("internal-connection-point-ref"):
1125 try:
tierno41a69812018-02-16 14:34:33 +01001126 for icp_descriptor in vdu_descriptor["internal-connection-point"]:
1127 if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")):
1128 break
1129 else:
1130 raise KeyError("does not exist at vdu:internal-connection-point")
1131 icp = None
1132 icp_vld = None
tierno66eba6e2017-11-10 17:09:18 +01001133 for vld in vnfd.get("internal-vld").itervalues():
1134 for cp in vld.get("internal-connection-point").itervalues():
1135 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
tierno41a69812018-02-16 14:34:33 +01001136 if icp:
1137 raise KeyError("is referenced by more than one 'internal-vld'")
1138 icp = cp
1139 icp_vld = vld
1140 if not icp:
1141 raise KeyError("is not referenced by any 'internal-vld'")
1142
1143 db_interface["net_id"] = net_id2uuid[icp_vld.get("id")]
1144 if str(icp_descriptor.get("port-security-enabled")).lower() == "false":
1145 db_interface["port_security"] = 0
1146 elif str(icp_descriptor.get("port-security-enabled")).lower() == "true":
1147 db_interface["port_security"] = 1
1148 if icp.get("ip-address"):
1149 if not icp_vld.get("ip-profile-ref"):
1150 raise NfvoException
1151 db_interface["ip_address"] = str(icp.get("ip-address"))
1152 except KeyError as e:
tierno66eba6e2017-11-10 17:09:18 +01001153 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
tierno41a69812018-02-16 14:34:33 +01001154 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1155 " {msg}".format(
tierno66eba6e2017-11-10 17:09:18 +01001156 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
tierno41a69812018-02-16 14:34:33 +01001157 cp=iface.get("internal-connection-point-ref"), msg=str(e)),
tierno66eba6e2017-11-10 17:09:18 +01001158 HTTP_Bad_Request)
tierno55d234c2018-07-04 18:29:21 +02001159 if iface.get("position"):
1160 db_interface["created_at"] = int(iface.get("position")) * 50
tierno41a69812018-02-16 14:34:33 +01001161 if iface.get("mac-address"):
1162 db_interface["mac"] = str(iface.get("mac-address"))
tierno66eba6e2017-11-10 17:09:18 +01001163 db_interfaces.append(db_interface)
1164
tiernof1ba57e2017-09-07 12:23:19 +02001165 # table flavors
1166 db_flavor = {
1167 "name": get_str(vdu, "name", 250) + "-flv",
1168 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
1169 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
garciadeblas79d1a1a2017-12-11 16:07:07 +01001170 "disk": int(vdu["vm-flavor"].get("storage-gb", 0)),
tiernof1ba57e2017-09-07 12:23:19 +02001171 }
tiernocf596692017-11-20 15:47:51 +01001172 # TODO revise the case of several numa-node-policy node
tiernof1ba57e2017-09-07 12:23:19 +02001173 extended = {}
1174 numa = {}
1175 if devices:
1176 extended["devices"] = devices
tierno66eba6e2017-11-10 17:09:18 +01001177 if flavor_epa_interfaces:
1178 numa["interfaces"] = flavor_epa_interfaces
tiernof1ba57e2017-09-07 12:23:19 +02001179 if vdu.get("guest-epa"): # TODO or dedicated_int:
1180 epa_vcpu_set = False
1181 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1182 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
1183 if numa_node_policy.get("node"):
tiernocf596692017-11-20 15:47:51 +01001184 numa_node = numa_node_policy["node"].values()[0]
tiernof1ba57e2017-09-07 12:23:19 +02001185 if numa_node.get("num-cores"):
1186 numa["cores"] = numa_node["num-cores"]
1187 epa_vcpu_set = True
1188 if numa_node.get("paired-threads"):
1189 if numa_node["paired-threads"].get("num-paired-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001190 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001191 epa_vcpu_set = True
tierno39dddcc2017-10-05 18:48:06 +02001192 if len(numa_node["paired-threads"].get("paired-thread-ids")):
tiernof1ba57e2017-09-07 12:23:19 +02001193 numa["paired-threads-id"] = []
tierno39dddcc2017-10-05 18:48:06 +02001194 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
tiernof1ba57e2017-09-07 12:23:19 +02001195 numa["paired-threads-id"].append(
1196 (str(pair["thread-a"]), str(pair["thread-b"]))
1197 )
1198 if numa_node.get("num-threads"):
tierno39dddcc2017-10-05 18:48:06 +02001199 numa["threads"] = int(numa_node["num-threads"])
tiernof1ba57e2017-09-07 12:23:19 +02001200 epa_vcpu_set = True
1201 if numa_node.get("memory-mb"):
1202 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
1203 if vdu["guest-epa"].get("mempage-size"):
1204 if vdu["guest-epa"]["mempage-size"] != "SMALL":
1205 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
1206 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
1207 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1208 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
1209 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1210 numa["cores"] = max(db_flavor["vcpus"], 1)
1211 else:
1212 numa["threads"] = max(db_flavor["vcpus"], 1)
1213 if numa:
1214 extended["numas"] = [numa]
1215 if extended:
1216 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
1217 db_flavor["extended"] = extended_text
1218 # look if flavor exist
garciadeblas79d1a1a2017-12-11 16:07:07 +01001219 temp_flavor_dict = {'disk': db_flavor.get('disk', 0),
tiernof1ba57e2017-09-07 12:23:19 +02001220 'ram': db_flavor.get('ram'),
1221 'vcpus': db_flavor.get('vcpus'),
1222 'extended': db_flavor.get('extended')
1223 }
1224 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
1225 if existing_flavors:
1226 flavor_uuid = existing_flavors[0]["uuid"]
1227 else:
1228 flavor_uuid = str(uuid4())
1229 uuid_list.append(flavor_uuid)
1230 db_flavor["uuid"] = flavor_uuid
1231 db_flavors.append(db_flavor)
1232 db_vm["flavor_id"] = flavor_uuid
1233
tiernof1ba57e2017-09-07 12:23:19 +02001234 # VNF affinity and antiaffinity
1235 for pg in vnfd.get("placement-groups").itervalues():
1236 pg_name = get_str(pg, "name", 255)
1237 for vdu in pg.get("member-vdus").itervalues():
1238 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1239 if vdu_id not in vdu_id2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02001240 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1241 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001242 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
tiernob2880eb2017-10-04 15:04:53 +02001243 HTTP_Bad_Request)
tiernob6990792018-11-13 10:37:42 +01001244 if vdu_id2db_table_index[vdu_id]:
1245 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
tiernof1ba57e2017-09-07 12:23:19 +02001246 # TODO consider the case of isolation and not colocation
1247 # if pg.get("strategy") == "ISOLATION":
1248
1249 # VNF mgmt configuration
1250 mgmt_access = {}
1251 if vnfd["mgmt-interface"].get("vdu-id"):
tierno66eba6e2017-11-10 17:09:18 +01001252 mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255)
1253 if mgmt_vdu_id not in vdu_id2uuid:
tiernob2880eb2017-10-04 15:04:53 +02001254 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1255 "'{vdu}'. Reference to a non-existing vdu".format(
tierno66eba6e2017-11-10 17:09:18 +01001256 vnf=vnfd_id, vdu=mgmt_vdu_id),
tiernob2880eb2017-10-04 15:04:53 +02001257 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001258 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
tierno66eba6e2017-11-10 17:09:18 +01001259 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1260 if vdu_id2cp_name.get(mgmt_vdu_id):
tiernob6990792018-11-13 10:37:42 +01001261 if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]:
1262 cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
tierno66eba6e2017-11-10 17:09:18 +01001263
tiernof1ba57e2017-09-07 12:23:19 +02001264 if vnfd["mgmt-interface"].get("ip-address"):
1265 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1266 if vnfd["mgmt-interface"].get("cp"):
1267 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
tiernob6990792018-11-13 10:37:42 +01001268 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
tiernob2880eb2017-10-04 15:04:53 +02001269 "Reference to a non-existing connection-point".format(
tierno66eba6e2017-11-10 17:09:18 +01001270 vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
tiernob2880eb2017-10-04 15:04:53 +02001271 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001272 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1273 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
tiernoe2ff1ce2017-11-02 17:01:10 +01001274 # mark this interface as of type mgmt
tiernob6990792018-11-13 10:37:42 +01001275 if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]:
1276 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
tiernoe2ff1ce2017-11-02 17:01:10 +01001277
tiernoa9550202017-09-22 13:31:35 +02001278 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
tiernof1ba57e2017-09-07 12:23:19 +02001279 "default-user", 64)
gcalvinoe580c7d2017-09-22 14:09:51 +02001280
tiernof1ba57e2017-09-07 12:23:19 +02001281 if default_user:
1282 mgmt_access["default_user"] = default_user
gcalvinoe580c7d2017-09-22 14:09:51 +02001283 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1284 "required", 6)
1285 if required:
1286 mgmt_access["required"] = required
1287
tiernof1ba57e2017-09-07 12:23:19 +02001288 if mgmt_access:
1289 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1290
1291 db_vnfs.append(db_vnf)
1292 db_tables=[
1293 {"vnfs": db_vnfs},
1294 {"nets": db_nets},
1295 {"images": db_images},
1296 {"flavors": db_flavors},
tierno41a69812018-02-16 14:34:33 +01001297 {"ip_profiles": db_ip_profiles},
tiernof1ba57e2017-09-07 12:23:19 +02001298 {"vms": db_vms},
1299 {"interfaces": db_interfaces},
1300 ]
1301
1302 logger.debug("create_vnf Deployment done vnfDict: %s",
1303 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1304 mydb.new_rows(db_tables, uuid_list)
1305 return vnfd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02001306 except NfvoException:
1307 raise
tiernof1ba57e2017-09-07 12:23:19 +02001308 except Exception as e:
1309 logger.error("Exception {}".format(e))
1310 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1311
1312
tiernob8569aa2018-08-24 11:34:54 +02001313@deprecated("Use new_vnfd_v3")
tierno7edb6752016-03-21 17:37:52 +01001314def new_vnf(mydb, tenant_id, vnf_descriptor):
1315 global global_config
tierno42026a02017-02-10 15:13:40 +01001316
tierno7edb6752016-03-21 17:37:52 +01001317 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001318 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +01001319 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001320 vims = {}
tierno7edb6752016-03-21 17:37:52 +01001321 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001322 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001323 if "tenant_id" in vnf_descriptor["vnf"]:
1324 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001325 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1326 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001327 else:
1328 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1329 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001330 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001331 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001332
1333 # Step 4. Review the descriptor and add missing fields
1334 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +02001335 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +01001336 vnf_name = vnf_descriptor['vnf']['name']
1337 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1338 if "physical" in vnf_descriptor['vnf']:
1339 del vnf_descriptor['vnf']['physical']
1340 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001341
tierno42026a02017-02-10 15:13:40 +01001342 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001343 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1344 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001345
tierno7edb6752016-03-21 17:37:52 +01001346 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1347 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1348 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +01001349 try:
tiernof97fd272016-07-11 14:32:37 +02001350 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001351 for vnfc in vnf_descriptor['vnf']['VNFC']:
1352 VNFCitem={}
1353 VNFCitem["name"] = vnfc['name']
mirabal29356312017-07-27 12:21:22 +02001354 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
tierno7edb6752016-03-21 17:37:52 +01001355 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001356
tiernof97fd272016-07-11 14:32:37 +02001357 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001358
tierno7edb6752016-03-21 17:37:52 +01001359 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001360 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
tierno7edb6752016-03-21 17:37:52 +01001361 myflavorDict["description"] = VNFCitem["description"]
1362 myflavorDict["ram"] = vnfc.get("ram", 0)
1363 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001364 myflavorDict["disk"] = vnfc.get("disk", 0)
tierno7edb6752016-03-21 17:37:52 +01001365 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001366
tierno7edb6752016-03-21 17:37:52 +01001367 devices = vnfc.get("devices")
1368 if devices != None:
1369 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001370
tierno7edb6752016-03-21 17:37:52 +01001371 # TODO:
1372 # 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
tierno42026a02017-02-10 15:13:40 +01001373 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1374
tierno7edb6752016-03-21 17:37:52 +01001375 # Previous code has been commented
1376 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1377 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1378 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1379 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1380 #else:
1381 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1382 # if result2:
1383 # print "Error creating flavor: unknown processor model. Rollback successful."
1384 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1385 # else:
1386 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1387 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001388
tierno7edb6752016-03-21 17:37:52 +01001389 if 'numas' in vnfc and len(vnfc['numas'])>0:
1390 myflavorDict['extended']['numas'] = vnfc['numas']
1391
1392 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001393
tierno7edb6752016-03-21 17:37:52 +01001394 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001395 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +01001396
tiernof97fd272016-07-11 14:32:37 +02001397 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +01001398 VNFCitem["flavor_id"] = flavor_id
1399 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001400
tiernof97fd272016-07-11 14:32:37 +02001401 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001402 # Step 6.3 New images are created in the VIM
1403 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001404 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +01001405 #In case this integration is made, the VNFCDict might become a VNFClist.
1406 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +02001407 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001408 image_dict={}
1409 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1410 image_dict['universal_name']=vnfc.get('image name')
1411 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1412 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001413 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001414 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +01001415 image_metadata_dict = vnfc.get('image metadata', None)
1416 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001417 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +01001418 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1419 image_dict['metadata']=image_metadata_str
1420 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +02001421 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1422 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +01001423 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001424 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001425 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001426 if vnfc.get("boot-data"):
1427 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +01001428
tierno42026a02017-02-10 15:13:40 +01001429
tiernof97fd272016-07-11 14:32:37 +02001430 # Step 7. Storing the VNF descriptor in the repository
1431 if "descriptor" not in vnf_descriptor["vnf"]:
1432 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001433
tiernof97fd272016-07-11 14:32:37 +02001434 # Step 8. Adding the VNF to the NFVO DB
1435 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1436 return vnf_id
1437 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +01001438 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +02001439 if isinstance(e, db_base_Exception):
1440 error_text = "Exception at database"
1441 elif isinstance(e, KeyError):
1442 error_text = "KeyError exception "
1443 e.http_code = HTTP_Internal_Server_Error
1444 else:
1445 error_text = "Exception at VIM"
1446 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1447 #logger.error("start_scenario %s", error_text)
1448 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01001449
tiernob3d36742017-03-03 23:51:05 +01001450
tiernob8569aa2018-08-24 11:34:54 +02001451@deprecated("Use new_vnfd_v3")
garciadeblas9f8456e2016-09-05 05:02:59 +02001452def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1453 global global_config
tierno42026a02017-02-10 15:13:40 +01001454
garciadeblas9f8456e2016-09-05 05:02:59 +02001455 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001456 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +02001457 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001458 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +02001459 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001460 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001461 if "tenant_id" in vnf_descriptor["vnf"]:
1462 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1463 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1464 HTTP_Unauthorized)
1465 else:
1466 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1467 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001468 if global_config["auto_push_VNF_to_VIMs"]:
tiernocbb52052018-05-31 18:57:30 +02001469 vims = get_vim(mydb, tenant_id, ignore_errors=True)
garciadeblas9f8456e2016-09-05 05:02:59 +02001470
1471 # Step 4. Review the descriptor and add missing fields
1472 #print vnf_descriptor
1473 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1474 vnf_name = vnf_descriptor['vnf']['name']
1475 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1476 if "physical" in vnf_descriptor['vnf']:
1477 del vnf_descriptor['vnf']['physical']
1478 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001479
tierno42026a02017-02-10 15:13:40 +01001480 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +02001481 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1482 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001483
garciadeblas9f8456e2016-09-05 05:02:59 +02001484 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1485 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1486 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1487 try:
1488 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1489 for vnfc in vnf_descriptor['vnf']['VNFC']:
1490 VNFCitem={}
1491 VNFCitem["name"] = vnfc['name']
1492 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001493
garciadeblas9f8456e2016-09-05 05:02:59 +02001494 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001495
garciadeblas9f8456e2016-09-05 05:02:59 +02001496 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001497 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
garciadeblas9f8456e2016-09-05 05:02:59 +02001498 myflavorDict["description"] = VNFCitem["description"]
1499 myflavorDict["ram"] = vnfc.get("ram", 0)
1500 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
garciadeblas79d1a1a2017-12-11 16:07:07 +01001501 myflavorDict["disk"] = vnfc.get("disk", 0)
garciadeblas9f8456e2016-09-05 05:02:59 +02001502 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001503
garciadeblas9f8456e2016-09-05 05:02:59 +02001504 devices = vnfc.get("devices")
1505 if devices != None:
1506 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001507
garciadeblas9f8456e2016-09-05 05:02:59 +02001508 # TODO:
1509 # 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
tierno42026a02017-02-10 15:13:40 +01001510 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1511
garciadeblas9f8456e2016-09-05 05:02:59 +02001512 # Previous code has been commented
1513 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1514 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1515 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1516 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1517 #else:
1518 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1519 # if result2:
1520 # print "Error creating flavor: unknown processor model. Rollback successful."
1521 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1522 # else:
1523 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1524 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001525
garciadeblas9f8456e2016-09-05 05:02:59 +02001526 if 'numas' in vnfc and len(vnfc['numas'])>0:
1527 myflavorDict['extended']['numas'] = vnfc['numas']
1528
1529 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001530
garciadeblas9f8456e2016-09-05 05:02:59 +02001531 # Step 6.2 New flavors are created in the VIM
1532 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1533
1534 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1535 VNFCitem["flavor_id"] = flavor_id
1536 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001537
garciadeblas9f8456e2016-09-05 05:02:59 +02001538 logger.debug("Creating new images in the VIM for each VNFC")
1539 # Step 6.3 New images are created in the VIM
1540 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001541 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +02001542 #In case this integration is made, the VNFCDict might become a VNFClist.
1543 for vnfc in vnf_descriptor['vnf']['VNFC']:
1544 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001545 image_dict={}
1546 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1547 image_dict['universal_name']=vnfc.get('image name')
1548 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1549 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001550 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001551 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +02001552 image_metadata_dict = vnfc.get('image metadata', None)
1553 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001554 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +02001555 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1556 image_dict['metadata']=image_metadata_str
1557 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1558 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1559 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1560 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001561 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001562 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001563 if vnfc.get("boot-data"):
1564 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +02001565
garciadeblas9f8456e2016-09-05 05:02:59 +02001566 # Step 7. Storing the VNF descriptor in the repository
1567 if "descriptor" not in vnf_descriptor["vnf"]:
1568 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001569
garciadeblas9f8456e2016-09-05 05:02:59 +02001570 # Step 8. Adding the VNF to the NFVO DB
1571 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1572 return vnf_id
1573 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1574 _, message = rollback(mydb, vims, rollback_list)
1575 if isinstance(e, db_base_Exception):
1576 error_text = "Exception at database"
1577 elif isinstance(e, KeyError):
1578 error_text = "KeyError exception "
1579 e.http_code = HTTP_Internal_Server_Error
1580 else:
1581 error_text = "Exception at VIM"
1582 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1583 #logger.error("start_scenario %s", error_text)
1584 raise NfvoException(error_text, e.http_code)
1585
tiernob3d36742017-03-03 23:51:05 +01001586
tierno7edb6752016-03-21 17:37:52 +01001587def get_vnf_id(mydb, tenant_id, vnf_id):
1588 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +01001589 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001590 #obtain data
1591 where_or = {}
1592 if tenant_id != "any":
1593 where_or["tenant_id"] = tenant_id
1594 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001595 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1596
tiernof1ba57e2017-09-07 12:23:19 +02001597 vnf_id = vnf["uuid"]
1598 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +02001599 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +01001600 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1601 data={'vnf' : filtered_content}
1602 #GET VM
tiernof97fd272016-07-11 14:32:37 +02001603 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tiernof1ba57e2017-09-07 12:23:19 +02001604 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1605 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +01001606 WHERE={'vnfs.uuid': vnf_id} )
gcalvinobfa2fd92018-11-13 18:47:28 +01001607 if len(content) != 0:
gcalvino319b8a52018-11-05 15:33:23 +01001608 #raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno36c0b172017-01-12 18:32:28 +01001609 # change boot_data into boot-data
gcalvino319b8a52018-11-05 15:33:23 +01001610 for vm in content:
1611 if vm.get("boot_data"):
1612 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1613 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +01001614
gcalvinobfa2fd92018-11-13 18:47:28 +01001615 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001616 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001617
tierno7edb6752016-03-21 17:37:52 +01001618 #GET NET
tierno42026a02017-02-10 15:13:40 +01001619 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001620 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1621 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001622 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001623
1624 #GET ip-profile for each net
1625 for net in data['vnf']['nets']:
1626 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1627 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1628 WHERE={'net_id': net["uuid"]} )
1629 if len(ipprofiles)==1:
1630 net["ip_profile"] = ipprofiles[0]
1631 elif len(ipprofiles)>1:
1632 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001633
1634
garciadeblas9f8456e2016-09-05 05:02:59 +02001635 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
tierno42026a02017-02-10 15:13:40 +01001636
garciadeblas9f8456e2016-09-05 05:02:59 +02001637 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001638 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
tierno7edb6752016-03-21 17:37:52 +01001639 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1640 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno3fcfdb72017-10-24 07:48:24 +02001641 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001642 #print content
tiernof97fd272016-07-11 14:32:37 +02001643 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001644
tiernof97fd272016-07-11 14:32:37 +02001645 return data
tierno7edb6752016-03-21 17:37:52 +01001646
1647
1648def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1649 # Check tenant exist
1650 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001651 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001652 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernocbb52052018-05-31 18:57:30 +02001653 vims = get_vim(mydb, tenant_id, ignore_errors=True)
tierno7edb6752016-03-21 17:37:52 +01001654 else:
1655 vims={}
1656
1657 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1658 where_or = {}
1659 if tenant_id != "any":
1660 where_or["tenant_id"] = tenant_id
1661 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001662 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001663 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001664
tierno7edb6752016-03-21 17:37:52 +01001665 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001666 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001667 if len(flavorList)==0:
1668 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001669
tiernof97fd272016-07-11 14:32:37 +02001670 imageList = get_imagelist(mydb, vnf_id)
1671 if len(imageList)==0:
1672 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001673
tiernof97fd272016-07-11 14:32:37 +02001674 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1675 if deleted == 0:
1676 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno42026a02017-02-10 15:13:40 +01001677
tierno7edb6752016-03-21 17:37:52 +01001678 undeletedItems = []
1679 for flavor in flavorList:
1680 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001681 try:
1682 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1683 if len(c) > 0:
1684 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1685 continue
1686 #flavor not used, must be deleted
1687 #delelte at VIM
tierno96ebf002017-12-13 10:55:38 +01001688 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor})
tierno7edb6752016-03-21 17:37:52 +01001689 for flavor_vim in c:
tierno96ebf002017-12-13 10:55:38 +01001690 if not flavor_vim['created']: # skip this flavor because not created by openmano
tierno7edb6752016-03-21 17:37:52 +01001691 continue
tierno96ebf002017-12-13 10:55:38 +01001692 # look for vim
1693 myvim = None
1694 for vim in vims.values():
1695 if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]:
1696 myvim = vim
1697 break
1698 if not myvim:
tierno7edb6752016-03-21 17:37:52 +01001699 continue
tiernoae4a8d12016-07-08 12:30:39 +02001700 try:
1701 myvim.delete_flavor(flavor_vim["vim_id"])
tierno96ebf002017-12-13 10:55:38 +01001702 except vimconn.vimconnNotFoundException:
1703 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"],
1704 flavor_vim["datacenter_vim_id"] )
tiernoae4a8d12016-07-08 12:30:39 +02001705 except vimconn.vimconnException as e:
1706 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
tierno96ebf002017-12-13 10:55:38 +01001707 flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e))
1708 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"],
1709 flavor_vim["datacenter_vim_id"]))
1710 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
tiernof97fd272016-07-11 14:32:37 +02001711 mydb.delete_row_by_id('flavors', flavor)
1712 except db_base_Exception as e:
1713 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno96ebf002017-12-13 10:55:38 +01001714 undeletedItems.append("flavor {}".format(flavor))
tiernof97fd272016-07-11 14:32:37 +02001715
tierno42026a02017-02-10 15:13:40 +01001716
tierno7edb6752016-03-21 17:37:52 +01001717 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001718 try:
1719 #check if image is used by other vnf
tierno16e3dd42018-04-24 12:52:40 +02001720 c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}])
tiernof97fd272016-07-11 14:32:37 +02001721 if len(c) > 0:
1722 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1723 continue
1724 #image not used, must be deleted
1725 #delelte at VIM
1726 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001727 for image_vim in c:
tierno868220c2017-09-26 00:11:05 +02001728 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +01001729 continue
1730 if image_vim['created']=='false': #skip this image because not created by openmano
1731 continue
1732 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001733 try:
1734 myvim.delete_image(image_vim["vim_id"])
1735 except vimconn.vimconnNotFoundException as e:
1736 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1737 except vimconn.vimconnException as e:
1738 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1739 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1740 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001741 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1742 mydb.delete_row_by_id('images', image)
1743 except db_base_Exception as e:
1744 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001745 undeletedItems.append("image %s" % image)
1746
tiernof97fd272016-07-11 14:32:37 +02001747 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001748 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001749 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001750
tiernob3d36742017-03-03 23:51:05 +01001751
tiernob8569aa2018-08-24 11:34:54 +02001752@deprecated("Not used")
tierno7edb6752016-03-21 17:37:52 +01001753def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1754 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1755 if result < 0:
1756 return result, vims
1757 elif result == 0:
1758 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
1759 myvim = vims.values()[0]
1760 result,servers = myvim.get_hosts_info()
1761 if result < 0:
1762 return result, servers
1763 topology = {'name':myvim['name'] , 'servers': servers}
1764 return result, topology
1765
tiernob3d36742017-03-03 23:51:05 +01001766
tierno7edb6752016-03-21 17:37:52 +01001767def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001768 vims = get_vim(mydb, nfvo_tenant_id)
1769 if len(vims) == 0:
1770 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
1771 elif len(vims)>1:
1772 #print "nfvo.datacenter_action() error. Several datacenters found"
1773 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001774 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001775 try:
1776 hosts = myvim.get_hosts()
1777 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001778
tiernof97fd272016-07-11 14:32:37 +02001779 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1780 for host in hosts:
1781 server={'name':host['name'], 'vms':[]}
1782 for vm in host['instances']:
1783 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001784 try:
tiernof97fd272016-07-11 14:32:37 +02001785 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1786 WHERE={'vim_vm_id':vm['id']} )
1787 if len(c) == 0:
1788 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1789 continue
1790 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001791
tiernof97fd272016-07-11 14:32:37 +02001792 except db_base_Exception as e:
1793 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1794 datacenter['Datacenters'][0]['servers'].append(server)
1795 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001796
tiernof97fd272016-07-11 14:32:37 +02001797 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1798 return datacenter
1799 except vimconn.vimconnException as e:
1800 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001801
tiernob3d36742017-03-03 23:51:05 +01001802
tiernob8569aa2018-08-24 11:34:54 +02001803@deprecated("Use new_nsd_v3")
tierno7edb6752016-03-21 17:37:52 +01001804def new_scenario(mydb, tenant_id, topo):
1805
1806# result, vims = get_vim(mydb, tenant_id)
1807# if result < 0:
1808# return result, vims
1809#1: parse input
1810 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001811 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001812 if "tenant_id" in topo:
1813 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001814 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1815 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001816 else:
1817 tenant_id=None
1818
tierno42026a02017-02-10 15:13:40 +01001819#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001820 vnfs={}
1821 other_nets={} #external_networks, bridge_networks and data_networkds
1822 nodes = topo['topology']['nodes']
1823 for k in nodes.keys():
1824 if nodes[k]['type'] == 'VNF':
1825 vnfs[k] = nodes[k]
1826 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001827 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001828 other_nets[k] = nodes[k]
1829 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001830 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001831 other_nets[k] = nodes[k]
1832 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001833
tierno7edb6752016-03-21 17:37:52 +01001834
1835#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1836 for name,vnf in vnfs.items():
tierno3fcfdb72017-10-24 07:48:24 +02001837 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01001838 error_text = ""
1839 error_pos = "'topology':'nodes':'" + name + "'"
1840 if 'vnf_id' in vnf:
1841 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001842 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001843 if 'VNF model' in vnf:
1844 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001845 where['name'] = vnf['VNF model']
tierno3fcfdb72017-10-24 07:48:24 +02001846 if len(where) == 1:
tiernof97fd272016-07-11 14:32:37 +02001847 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001848
tiernocea279c2016-07-18 12:36:49 +02001849 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1850 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02001851 WHERE=where)
tiernof97fd272016-07-11 14:32:37 +02001852 if len(vnf_db)==0:
1853 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1854 elif len(vnf_db)>1:
1855 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001856 vnf['uuid']=vnf_db[0]['uuid']
1857 vnf['description']=vnf_db[0]['description']
1858 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001859 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1860 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
tierno3fcfdb72017-10-24 07:48:24 +02001861 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001862 for ext_iface in ext_ifaces:
1863 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1864
1865#1.4 get list of connections
1866 conections = topo['topology']['connections']
1867 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001868 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001869 for k in conections.keys():
1870 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1871 ifaces_list = conections[k]['nodes'].items()
1872 elif type(conections[k]['nodes'])==list: #list with dictionary
1873 ifaces_list=[]
1874 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1875 for k2 in conection_pair_list:
1876 ifaces_list += k2
1877
1878 con_type = conections[k].get("type", "link")
1879 if con_type != "link":
1880 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001881 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001882 other_nets[k] = {'external': False}
1883 if conections[k].get("graph"):
1884 other_nets[k]["graph"] = conections[k]["graph"]
1885 ifaces_list.append( (k, None) )
1886
tierno42026a02017-02-10 15:13:40 +01001887
tierno7edb6752016-03-21 17:37:52 +01001888 if con_type == "external_network":
1889 other_nets[k]['external'] = True
1890 if conections[k].get("model"):
1891 other_nets[k]["model"] = conections[k]["model"]
1892 else:
1893 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001894 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001895 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001896
tiernoefd80c92016-09-16 14:17:46 +02001897 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001898 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)
1899 #print set(ifaces_list)
1900 #check valid VNF and iface names
1901 for iface in ifaces_list:
1902 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001903 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1904 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001905 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001906 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1907 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001908
1909#1.5 unify connections from the pair list to a consolidated list
1910 index=0
1911 while index < len(conections_list):
1912 index2 = index+1
1913 while index2 < len(conections_list):
1914 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1915 conections_list[index] |= conections_list[index2]
1916 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001917 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001918 else:
1919 index2 += 1
1920 conections_list[index] = list(conections_list[index]) # from set to list again
1921 index += 1
1922 #for k in conections_list:
1923 # print k
tierno42026a02017-02-10 15:13:40 +01001924
tierno7edb6752016-03-21 17:37:52 +01001925
1926
1927#1.6 Delete non external nets
1928# for k in other_nets.keys():
1929# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1930# for con in conections_list:
1931# delete_indexes=[]
1932# for index in range(0,len(con)):
1933# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1934# for index in delete_indexes:
1935# del con[index]
1936# del other_nets[k]
1937#1.7: Check external_ports are present at database table datacenter_nets
1938 for k,net in other_nets.items():
1939 error_pos = "'topology':'nodes':'" + k + "'"
1940 if net['external']==False:
1941 if 'name' not in net:
1942 net['name']=k
1943 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001944 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001945 if net['model']=='bridge_net':
1946 net['type']='bridge';
1947 elif net['model']=='dataplane_net':
1948 net['type']='data';
1949 else:
tiernof97fd272016-07-11 14:32:37 +02001950 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001951 else: #external
1952#IF we do not want to check that external network exist at datacenter
1953 pass
tierno42026a02017-02-10 15:13:40 +01001954#ELSE
tierno7edb6752016-03-21 17:37:52 +01001955# error_text = ""
1956# WHERE_={}
1957# if 'net_id' in net:
1958# error_text += " 'net_id' " + net['net_id']
1959# WHERE_['uuid'] = net['net_id']
1960# if 'model' in net:
1961# error_text += " 'model' " + net['model']
1962# WHERE_['name'] = net['model']
1963# if len(WHERE_) == 0:
1964# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1965# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1966# FROM='datacenter_nets', WHERE=WHERE_ )
1967# if r<0:
1968# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1969# elif r==0:
1970# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1971# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1972# elif r>1:
tierno42026a02017-02-10 15:13:40 +01001973# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1974# return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
tierno7edb6752016-03-21 17:37:52 +01001975# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01001976#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001977 net_list={}
1978 net_nb=0 #Number of nets
1979 for con in conections_list:
1980 #check if this is connected to a external net
1981 other_net_index=-1
1982 #print
1983 #print "con", con
1984 for index in range(0,len(con)):
1985 #check if this is connected to a external net
1986 for net_key in other_nets.keys():
1987 if con[index][0]==net_key:
1988 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01001989 error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key)
tiernof97fd272016-07-11 14:32:37 +02001990 #print "nfvo.new_scenario " + error_text
1991 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001992 else:
1993 other_net_index = index
1994 net_target = net_key
1995 break
1996 #print "other_net_index", other_net_index
1997 try:
1998 if other_net_index>=0:
1999 del con[other_net_index]
2000#IF we do not want to check that external network exist at datacenter
2001 if other_nets[net_target]['external'] :
2002 if "name" not in other_nets[net_target]:
2003 other_nets[net_target]['name'] = other_nets[net_target]['model']
2004 if other_nets[net_target]["type"] == "external_network":
2005 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
2006 other_nets[net_target]["type"] = "data"
2007 else:
2008 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01002009#ELSE
tierno7edb6752016-03-21 17:37:52 +01002010# if other_nets[net_target]['external'] :
2011# 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
2012# if type_=='data' and other_nets[net_target]['type']=="ptp":
2013# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2014# print "nfvo.new_scenario " + error_text
2015# return -HTTP_Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01002016#ENDIF
tierno7edb6752016-03-21 17:37:52 +01002017 for iface in con:
2018 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2019 else:
2020 #create a net
2021 net_type_bridge=False
2022 net_type_data=False
2023 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01002024 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02002025 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01002026 'external':False}
tierno7edb6752016-03-21 17:37:52 +01002027 for iface in con:
2028 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2029 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
2030 if iface_type=='mgmt' or iface_type=='bridge':
2031 net_type_bridge = True
2032 else:
2033 net_type_data = True
2034 if net_type_bridge and net_type_data:
2035 error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02002036 #print "nfvo.new_scenario " + error_text
2037 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002038 elif net_type_bridge:
2039 type_='bridge'
2040 else:
2041 type_='data' if len(con)>2 else 'ptp'
2042 net_list[net_target]['type'] = type_
2043 net_nb+=1
2044 except Exception:
2045 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02002046 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01002047 #raise e
tiernof97fd272016-07-11 14:32:37 +02002048 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002049
2050#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01002051 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02002052 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01002053 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01002054 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02002055 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01002056 add_mgmt_net = False
2057 for vnf in vnfs.values():
2058 for iface in vnf['ifaces'].values():
2059 if iface['type']=='mgmt' and 'net_key' not in iface:
2060 #iface not connected
2061 iface['net_key'] = 'mgmt'
2062 add_mgmt_net = True
2063 if add_mgmt_net and 'mgmt' not in net_list:
2064 net_list['mgmt']=mgmt_net[0]
2065 net_list['mgmt']['external']=True
2066 net_list['mgmt']['graph']={'visible':False}
2067
2068 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02002069 #print
2070 #print 'net_list', net_list
2071 #print
2072 #print 'vnfs', vnfs
2073 #print
tierno7edb6752016-03-21 17:37:52 +01002074
2075#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02002076 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02002077 'tenant_id':tenant_id, 'name':topo['name'],
2078 'description':topo.get('description',topo['name']),
2079 'public': topo.get('public', False)
2080 })
tierno42026a02017-02-10 15:13:40 +01002081
tiernof97fd272016-07-11 14:32:37 +02002082 return c
tierno7edb6752016-03-21 17:37:52 +01002083
tiernob3d36742017-03-03 23:51:05 +01002084
tiernob8569aa2018-08-24 11:34:54 +02002085@deprecated("Use new_nsd_v3")
tierno5bb59dc2017-02-13 14:53:54 +01002086def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
2087 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02002088 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01002089 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01002090 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002091 if "tenant_id" in scenario:
2092 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01002093 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02002094 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2095 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01002096 else:
2097 tenant_id=None
2098
tierno5bb59dc2017-02-13 14:53:54 +01002099 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01002100 for name,vnf in scenario["vnfs"].iteritems():
tierno3fcfdb72017-10-24 07:48:24 +02002101 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01002102 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02002103 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01002104 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002105 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02002106 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02002107 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01002108 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02002109 where['name'] = vnf['vnf_name']
tierno3fcfdb72017-10-24 07:48:24 +02002110 if len(where) == 1:
garciadeblas71781ea2016-09-19 14:41:59 +02002111 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01002112 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02002113 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02002114 WHERE=where)
tierno5bb59dc2017-02-13 14:53:54 +01002115 if len(vnf_db) == 0:
tiernof97fd272016-07-11 14:32:37 +02002116 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01002117 elif len(vnf_db) > 1:
tiernof97fd272016-07-11 14:32:37 +02002118 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01002119 vnf['uuid'] = vnf_db[0]['uuid']
2120 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01002121 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01002122 # get external interfaces
2123 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2124 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
tierno3fcfdb72017-10-24 07:48:24 +02002125 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01002126 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01002127 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
2128 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01002129
tierno5bb59dc2017-02-13 14:53:54 +01002130 # 2: Insert net_key and ip_address at every vnf interface
2131 for net_name, net in scenario["networks"].items():
2132 net_type_bridge = False
2133 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01002134 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01002135 if version == "0.2":
2136 temp_dict = iface_dict
2137 ip_address = None
2138 elif version == "0.3":
2139 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2140 ip_address = iface_dict.get('ip_address', None)
2141 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01002142 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01002143 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2144 net_name, vnf)
2145 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002146 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002147 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01002148 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2149 .format(net_name, iface)
2150 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002151 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002152 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01002153 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2154 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2155 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002156 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002157 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01002158 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01002159 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01002160 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01002161 net_type_bridge = True
2162 else:
2163 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01002164
tierno7edb6752016-03-21 17:37:52 +01002165 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01002166 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2167 .format(net_name)
2168 # logger.debug("nfvo.new_scenario " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002169 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002170 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01002171 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01002172 else:
tierno5bb59dc2017-02-13 14:53:54 +01002173 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2174
2175 if net.get("implementation"): # for v0.3
2176 if type_ == "bridge" and net["implementation"] == "underlay":
2177 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2178 "'network':'{}'".format(net_name)
2179 # logger.debug(error_text)
2180 raise NfvoException(error_text, HTTP_Bad_Request)
2181 elif type_ != "bridge" and net["implementation"] == "overlay":
2182 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2183 "'network':'{}'".format(net_name)
2184 # logger.debug(error_text)
2185 raise NfvoException(error_text, HTTP_Bad_Request)
2186 net.pop("implementation")
2187 if "type" in net and version == "0.3": # for v0.3
2188 if type_ == "data" and net["type"] == "e-line":
2189 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2190 "'e-line' at 'network':'{}'".format(net_name)
2191 # logger.debug(error_text)
2192 raise NfvoException(error_text, HTTP_Bad_Request)
2193 elif type_ == "ptp" and net["type"] == "e-lan":
2194 type_ = "data"
2195
tierno7edb6752016-03-21 17:37:52 +01002196 net['type'] = type_
2197 net['name'] = net_name
2198 net['external'] = net.get('external', False)
2199
tierno5bb59dc2017-02-13 14:53:54 +01002200 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01002201 scenario["nets"] = scenario["networks"]
2202 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01002203 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02002204 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01002205
tiernob3d36742017-03-03 23:51:05 +01002206
tiernof1ba57e2017-09-07 12:23:19 +02002207def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2208 """
2209 Parses an OSM IM nsd_catalog and insert at DB
2210 :param mydb:
2211 :param tenant_id:
2212 :param nsd_descriptor:
Igor D.Ccaadc442017-11-06 12:48:48 +00002213 :return: The list of created NSD ids
tiernof1ba57e2017-09-07 12:23:19 +02002214 """
2215 try:
2216 mynsd = nsd_catalog.nsd()
tiernoa9550202017-09-22 13:31:35 +02002217 try:
2218 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd)
2219 except Exception as e:
tiernob2880eb2017-10-04 15:04:53 +02002220 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002221 db_scenarios = []
2222 db_sce_nets = []
2223 db_sce_vnfs = []
2224 db_sce_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00002225 db_sce_vnffgs = []
2226 db_sce_rsps = []
2227 db_sce_rsp_hops = []
2228 db_sce_classifiers = []
2229 db_sce_classifier_matches = []
tiernof1ba57e2017-09-07 12:23:19 +02002230 db_ip_profiles = []
2231 db_ip_profiles_index = 0
2232 uuid_list = []
2233 nsd_uuid_list = []
tiernob2880eb2017-10-04 15:04:53 +02002234 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2235 nsd = nsd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +02002236
Igor D.Ccaadc442017-11-06 12:48:48 +00002237 # table scenarios
tiernof1ba57e2017-09-07 12:23:19 +02002238 scenario_uuid = str(uuid4())
2239 uuid_list.append(scenario_uuid)
2240 nsd_uuid_list.append(scenario_uuid)
2241 db_scenario = {
2242 "uuid": scenario_uuid,
2243 "osm_id": get_str(nsd, "id", 255),
2244 "name": get_str(nsd, "name", 255),
2245 "description": get_str(nsd, "description", 255),
2246 "tenant_id": tenant_id,
2247 "vendor": get_str(nsd, "vendor", 255),
2248 "short_name": get_str(nsd, "short-name", 255),
2249 "descriptor": str(nsd_descriptor)[:60000],
2250 }
2251 db_scenarios.append(db_scenario)
2252
2253 # table sce_vnfs (constituent-vnfd)
2254 vnf_index2scevnf_uuid = {}
2255 vnf_index2vnf_uuid = {}
2256 for vnf in nsd.get("constituent-vnfd").itervalues():
2257 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2258 'tenant_id': tenant_id})
2259 if not existing_vnf:
tiernob2880eb2017-10-04 15:04:53 +02002260 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2261 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2262 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
2263 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002264 sce_vnf_uuid = str(uuid4())
2265 uuid_list.append(sce_vnf_uuid)
2266 db_sce_vnf = {
2267 "uuid": sce_vnf_uuid,
2268 "scenario_id": scenario_uuid,
tierno92c36fd2018-05-04 12:21:10 +02002269 # "name": get_str(vnf, "member-vnf-index", 255),
2270 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
tiernof1ba57e2017-09-07 12:23:19 +02002271 "vnf_id": existing_vnf[0]["uuid"],
tierno16e3dd42018-04-24 12:52:40 +02002272 "member_vnf_index": str(vnf["member-vnf-index"]),
tiernof1ba57e2017-09-07 12:23:19 +02002273 # TODO 'start-by-default': True
2274 }
tierno16e3dd42018-04-24 12:52:40 +02002275 vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid
2276 vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
tiernof1ba57e2017-09-07 12:23:19 +02002277 db_sce_vnfs.append(db_sce_vnf)
2278
2279 # table ip_profiles (ip-profiles)
2280 ip_profile_name2db_table_index = {}
2281 for ip_profile in nsd.get("ip-profiles").itervalues():
2282 db_ip_profile = {
2283 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2284 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2285 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2286 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2287 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2288 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2289 }
2290 dns_list = []
2291 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2292 dns_list.append(str(dns.get("address")))
2293 db_ip_profile["dns_address"] = ";".join(dns_list)
2294 if ip_profile["ip-profile-params"].get('security-group'):
2295 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2296 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2297 db_ip_profiles_index += 1
2298 db_ip_profiles.append(db_ip_profile)
2299
2300 # table sce_nets (internal-vld)
2301 for vld in nsd.get("vld").itervalues():
2302 sce_net_uuid = str(uuid4())
2303 uuid_list.append(sce_net_uuid)
2304 db_sce_net = {
2305 "uuid": sce_net_uuid,
2306 "name": get_str(vld, "name", 255),
2307 "scenario_id": scenario_uuid,
2308 # "type": #TODO
2309 "multipoint": not vld.get("type") == "ELINE",
tierno1df468d2018-07-06 14:25:16 +02002310 "osm_id": get_str(vld, "id", 255),
tiernof1ba57e2017-09-07 12:23:19 +02002311 # "external": #TODO
2312 "description": get_str(vld, "description", 255),
2313 }
2314 # guess type of network
2315 if vld.get("mgmt-network"):
2316 db_sce_net["type"] = "bridge"
2317 db_sce_net["external"] = True
2318 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2319 db_sce_net["type"] = "data"
2320 else:
tierno66eba6e2017-11-10 17:09:18 +01002321 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2322 db_sce_net["type"] = None
tiernof1ba57e2017-09-07 12:23:19 +02002323 db_sce_nets.append(db_sce_net)
2324
2325 # ip-profile, link db_ip_profile with db_sce_net
2326 if vld.get("ip-profile-ref"):
2327 ip_profile_name = vld.get("ip-profile-ref")
2328 if ip_profile_name not in ip_profile_name2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02002329 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2330 " Reference to a non-existing 'ip_profiles'".format(
2331 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
2332 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002333 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
tierno8f79ea12018-05-03 17:37:40 +02002334 elif vld.get("vim-network-name"):
2335 db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255)
tiernof1ba57e2017-09-07 12:23:19 +02002336
2337 # table sce_interfaces (vld:vnfd-connection-point-ref)
2338 for iface in vld.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002339 vnf_index = str(iface['member-vnf-index-ref'])
tiernof1ba57e2017-09-07 12:23:19 +02002340 # check correct parameters
2341 if vnf_index not in vnf_index2vnf_uuid:
tiernob2880eb2017-10-04 15:04:53 +02002342 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2343 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2344 "'nsd':'constituent-vnfd'".format(
2345 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
2346 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002347
tierno66eba6e2017-11-10 17:09:18 +01002348 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'),
tiernof1ba57e2017-09-07 12:23:19 +02002349 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2350 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2351 'external_name': get_str(iface, "vnfd-connection-point-ref",
2352 255)})
2353 if not existing_ifaces:
tiernob2880eb2017-10-04 15:04:53 +02002354 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2355 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2356 "connection-point name at VNFD '{}'".format(
2357 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2358 str(iface.get("vnfd-id-ref"))[:255]),
2359 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002360 interface_uuid = existing_ifaces[0]["uuid"]
tierno66eba6e2017-11-10 17:09:18 +01002361 if existing_ifaces[0]["iface_type"] == "data" and not db_sce_net["type"]:
2362 db_sce_net["type"] = "data"
tiernof1ba57e2017-09-07 12:23:19 +02002363 sce_interface_uuid = str(uuid4())
2364 uuid_list.append(sce_net_uuid)
tierno41a69812018-02-16 14:34:33 +01002365 iface_ip_address = None
2366 if iface.get("ip-address"):
2367 iface_ip_address = str(iface.get("ip-address"))
tiernof1ba57e2017-09-07 12:23:19 +02002368 db_sce_interface = {
2369 "uuid": sce_interface_uuid,
2370 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2371 "sce_net_id": sce_net_uuid,
2372 "interface_id": interface_uuid,
tierno41a69812018-02-16 14:34:33 +01002373 "ip_address": iface_ip_address,
tiernof1ba57e2017-09-07 12:23:19 +02002374 }
2375 db_sce_interfaces.append(db_sce_interface)
tierno66eba6e2017-11-10 17:09:18 +01002376 if not db_sce_net["type"]:
2377 db_sce_net["type"] = "bridge"
tiernof1ba57e2017-09-07 12:23:19 +02002378
Igor D.Ccaadc442017-11-06 12:48:48 +00002379 # table sce_vnffgs (vnffgd)
2380 for vnffg in nsd.get("vnffgd").itervalues():
2381 sce_vnffg_uuid = str(uuid4())
2382 uuid_list.append(sce_vnffg_uuid)
2383 db_sce_vnffg = {
2384 "uuid": sce_vnffg_uuid,
2385 "name": get_str(vnffg, "name", 255),
2386 "scenario_id": scenario_uuid,
2387 "vendor": get_str(vnffg, "vendor", 255),
2388 "description": get_str(vld, "description", 255),
2389 }
2390 db_sce_vnffgs.append(db_sce_vnffg)
2391
2392 # deal with rsps
2393 db_sce_rsps = []
2394 for rsp in vnffg.get("rsp").itervalues():
2395 sce_rsp_uuid = str(uuid4())
2396 uuid_list.append(sce_rsp_uuid)
2397 db_sce_rsp = {
2398 "uuid": sce_rsp_uuid,
2399 "name": get_str(rsp, "name", 255),
2400 "sce_vnffg_id": sce_vnffg_uuid,
2401 "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code
2402 }
2403 db_sce_rsps.append(db_sce_rsp)
2404 db_sce_rsp_hops = []
2405 for iface in rsp.get("vnfd-connection-point-ref").itervalues():
tierno16e3dd42018-04-24 12:52:40 +02002406 vnf_index = str(iface['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002407 if_order = int(iface['order'])
2408 # check correct parameters
2409 if vnf_index not in vnf_index2vnf_uuid:
2410 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2411 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2412 "'nsd':'constituent-vnfd'".format(
2413 str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
2414 HTTP_Bad_Request)
2415
2416 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2417 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2418 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2419 'external_name': get_str(iface, "vnfd-connection-point-ref",
2420 255)})
2421 if not existing_ifaces:
2422 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2423 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2424 "connection-point name at VNFD '{}'".format(
2425 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2426 str(iface.get("vnfd-id-ref"))[:255]),
2427 HTTP_Bad_Request)
2428 interface_uuid = existing_ifaces[0]["uuid"]
2429 sce_rsp_hop_uuid = str(uuid4())
2430 uuid_list.append(sce_rsp_hop_uuid)
2431 db_sce_rsp_hop = {
2432 "uuid": sce_rsp_hop_uuid,
2433 "if_order": if_order,
2434 "interface_id": interface_uuid,
2435 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2436 "sce_rsp_id": sce_rsp_uuid,
2437 }
2438 db_sce_rsp_hops.append(db_sce_rsp_hop)
2439
2440 # deal with classifiers
2441 db_sce_classifiers = []
2442 for classifier in vnffg.get("classifier").itervalues():
2443 sce_classifier_uuid = str(uuid4())
2444 uuid_list.append(sce_classifier_uuid)
2445
2446 # source VNF
tierno16e3dd42018-04-24 12:52:40 +02002447 vnf_index = str(classifier['member-vnf-index-ref'])
Igor D.Ccaadc442017-11-06 12:48:48 +00002448 if vnf_index not in vnf_index2vnf_uuid:
2449 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2450 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2451 "'nsd':'constituent-vnfd'".format(
2452 str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])),
2453 HTTP_Bad_Request)
2454 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2455 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2456 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2457 'external_name': get_str(classifier, "vnfd-connection-point-ref",
2458 255)})
2459 if not existing_ifaces:
2460 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2461 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2462 "connection-point name at VNFD '{}'".format(
2463 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2464 str(iface.get("vnfd-id-ref"))[:255]),
2465 HTTP_Bad_Request)
2466 interface_uuid = existing_ifaces[0]["uuid"]
2467
2468 db_sce_classifier = {
2469 "uuid": sce_classifier_uuid,
2470 "name": get_str(classifier, "name", 255),
2471 "sce_vnffg_id": sce_vnffg_uuid,
2472 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2473 "interface_id": interface_uuid,
2474 }
2475 rsp_id = get_str(classifier, "rsp-id-ref", 255)
2476 rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None)
2477 db_sce_classifier["sce_rsp_id"] = rsp["uuid"]
2478 db_sce_classifiers.append(db_sce_classifier)
2479
2480 db_sce_classifier_matches = []
2481 for match in classifier.get("match-attributes").itervalues():
2482 sce_classifier_match_uuid = str(uuid4())
2483 uuid_list.append(sce_classifier_match_uuid)
2484 db_sce_classifier_match = {
2485 "uuid": sce_classifier_match_uuid,
2486 "ip_proto": get_str(match, "ip-proto", 2),
2487 "source_ip": get_str(match, "source-ip-address", 16),
2488 "destination_ip": get_str(match, "destination-ip-address", 16),
2489 "source_port": get_str(match, "source-port", 5),
2490 "destination_port": get_str(match, "destination-port", 5),
2491 "sce_classifier_id": sce_classifier_uuid,
2492 }
2493 db_sce_classifier_matches.append(db_sce_classifier_match)
2494 # TODO: vnf/cp keys
2495
2496 # remove unneeded id's in sce_rsps
2497 for rsp in db_sce_rsps:
2498 rsp.pop('id')
2499
tiernof1ba57e2017-09-07 12:23:19 +02002500 db_tables = [
2501 {"scenarios": db_scenarios},
2502 {"sce_nets": db_sce_nets},
2503 {"ip_profiles": db_ip_profiles},
2504 {"sce_vnfs": db_sce_vnfs},
2505 {"sce_interfaces": db_sce_interfaces},
Igor D.Ccaadc442017-11-06 12:48:48 +00002506 {"sce_vnffgs": db_sce_vnffgs},
2507 {"sce_rsps": db_sce_rsps},
2508 {"sce_rsp_hops": db_sce_rsp_hops},
2509 {"sce_classifiers": db_sce_classifiers},
2510 {"sce_classifier_matches": db_sce_classifier_matches},
tiernof1ba57e2017-09-07 12:23:19 +02002511 ]
2512
Igor D.Ccaadc442017-11-06 12:48:48 +00002513 logger.debug("new_nsd_v3 done: %s",
tiernof1ba57e2017-09-07 12:23:19 +02002514 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2515 mydb.new_rows(db_tables, uuid_list)
2516 return nsd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02002517 except NfvoException:
2518 raise
tiernof1ba57e2017-09-07 12:23:19 +02002519 except Exception as e:
2520 logger.error("Exception {}".format(e))
2521 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2522
2523
tierno7edb6752016-03-21 17:37:52 +01002524def edit_scenario(mydb, tenant_id, scenario_id, data):
2525 data["uuid"] = scenario_id
2526 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02002527 c = mydb.edit_scenario( data )
2528 return c
tierno7edb6752016-03-21 17:37:52 +01002529
tiernob3d36742017-03-03 23:51:05 +01002530
tiernob8569aa2018-08-24 11:34:54 +02002531@deprecated("Use create_instance")
tierno7edb6752016-03-21 17:37:52 +01002532def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
tiernoae4a8d12016-07-08 12:30:39 +02002533 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002534 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2535 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02002536 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01002537 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00002538
tierno7edb6752016-03-21 17:37:52 +01002539 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02002540 try:
2541 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tierno868220c2017-09-26 00:11:05 +02002542 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00002543 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02002544 scenarioDict['datacenter_id'] = datacenter_id
2545 #print '================scenarioDict======================='
2546 #print json.dumps(scenarioDict, indent=4)
2547 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01002548
tiernoae4a8d12016-07-08 12:30:39 +02002549 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2550 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002551
tiernoae4a8d12016-07-08 12:30:39 +02002552 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2553 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01002554
tiernoae4a8d12016-07-08 12:30:39 +02002555 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2556 for sce_net in scenarioDict['nets']:
2557 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01002558
tiernoae4a8d12016-07-08 12:30:39 +02002559 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01002560 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02002561 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01002562 myNetDict = {}
2563 myNetDict["name"] = myNetName
2564 myNetDict["type"] = myNetType
2565 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002566 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01002567 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02002568 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02002569 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02002570 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02002571 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002572 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2573 sce_net['vim_id'] = network_id
2574 auxNetDict['scenario'][sce_net['uuid']] = network_id
2575 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002576 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02002577 else:
2578 if sce_net['vim_id'] == None:
2579 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2580 _, message = rollback(mydb, vims, rollbackList)
2581 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02002582 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02002583 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2584 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01002585
tiernoae4a8d12016-07-08 12:30:39 +02002586 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2587 #For each vnf net, we create it and we add it to instanceNetlist.
mirabal29356312017-07-27 12:21:22 +02002588
tiernoae4a8d12016-07-08 12:30:39 +02002589 for sce_vnf in scenarioDict['vnfs']:
2590 for net in sce_vnf['nets']:
2591 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01002592
tiernoae4a8d12016-07-08 12:30:39 +02002593 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2594 myNetName = myNetName[0:255] #limit length
2595 myNetType = net['type']
2596 myNetDict = {}
2597 myNetDict["name"] = myNetName
2598 myNetDict["type"] = myNetType
2599 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002600 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02002601 #print myNetDict
2602 #TODO:
2603 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02002604 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002605 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2606 net['vim_id'] = network_id
2607 if sce_vnf['uuid'] not in auxNetDict:
2608 auxNetDict[sce_vnf['uuid']] = {}
2609 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2610 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002611 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002612
tiernoae4a8d12016-07-08 12:30:39 +02002613 #print "auxNetDict:"
2614 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002615
tiernoae4a8d12016-07-08 12:30:39 +02002616 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2617 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2618 i = 0
2619 for sce_vnf in scenarioDict['vnfs']:
tierno5a3273c2017-08-29 11:43:46 +02002620 vnf_availability_zones = []
mirabal29356312017-07-27 12:21:22 +02002621 for vm in sce_vnf['vms']:
2622 vm_av = vm.get('availability_zone')
tierno5a3273c2017-08-29 11:43:46 +02002623 if vm_av and vm_av not in vnf_availability_zones:
2624 vnf_availability_zones.append(vm_av)
2625
2626 # check if there is enough availability zones available at vim level.
2627 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2628 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2629 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
2630
tiernoae4a8d12016-07-08 12:30:39 +02002631 for vm in sce_vnf['vms']:
2632 i += 1
2633 myVMDict = {}
2634 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01002635 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02002636 #myVMDict['description'] = vm['description']
2637 myVMDict['description'] = myVMDict['name'][0:99]
2638 if not startvms:
2639 myVMDict['start'] = "no"
2640 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2641 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01002642
tiernoae4a8d12016-07-08 12:30:39 +02002643 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002644 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01002645 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002646 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002647
tiernoae4a8d12016-07-08 12:30:39 +02002648 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002649 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002650 if flavor_dict['extended']!=None:
2651 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01002652 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002653 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01002654
2655
tiernoae4a8d12016-07-08 12:30:39 +02002656 myVMDict['imageRef'] = vm['vim_image_id']
2657 myVMDict['flavorRef'] = vm['vim_flavor_id']
2658 myVMDict['networks'] = []
2659 for iface in vm['interfaces']:
2660 netDict = {}
2661 if iface['type']=="data":
2662 netDict['type'] = iface['model']
2663 elif "model" in iface and iface["model"]!=None:
2664 netDict['model']=iface['model']
2665 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2666 #discover type of interface looking at flavor
2667 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2668 for flavor_iface in numa.get('interfaces',[]):
2669 if flavor_iface.get('name') == iface['internal_name']:
2670 if flavor_iface['dedicated'] == 'yes':
2671 netDict['type']="PF" #passthrough
2672 elif flavor_iface['dedicated'] == 'no':
2673 netDict['type']="VF" #siov
2674 elif flavor_iface['dedicated'] == 'yes:sriov':
2675 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2676 netDict["mac_address"] = flavor_iface.get("mac_address")
2677 break;
2678 netDict["use"]=iface['type']
2679 if netDict["use"]=="data" and not netDict.get("type"):
2680 #print "netDict", netDict
2681 #print "iface", iface
2682 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'])
2683 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02002684 raise NfvoException(e_text + "After database migration some information is not available. \
2685 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02002686 else:
tiernof97fd272016-07-11 14:32:37 +02002687 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02002688 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2689 netDict["type"]="virtual"
2690 if "vpci" in iface and iface["vpci"] is not None:
2691 netDict['vpci'] = iface['vpci']
2692 if "mac" in iface and iface["mac"] is not None:
2693 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002694 if "port-security" in iface and iface["port-security"] is not None:
2695 netDict['port_security'] = iface['port-security']
2696 if "floating-ip" in iface and iface["floating-ip"] is not None:
2697 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02002698 netDict['name'] = iface['internal_name']
2699 if iface['net_id'] is None:
2700 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002701 #print iface
2702 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02002703 if vnf_iface['interface_id']==iface['uuid']:
2704 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2705 break
2706 else:
2707 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2708 #skip bridge ifaces not connected to any net
2709 #if 'net_id' not in netDict or netDict['net_id']==None:
2710 # continue
2711 myVMDict['networks'].append(netDict)
2712 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2713 #print myVMDict['name']
2714 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2715 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2716 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
mirabal29356312017-07-27 12:21:22 +02002717
2718 if 'availability_zone' in myVMDict:
tierno5a3273c2017-08-29 11:43:46 +02002719 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
mirabal29356312017-07-27 12:21:22 +02002720 else:
tierno5a3273c2017-08-29 11:43:46 +02002721 av_index = None
mirabal29356312017-07-27 12:21:22 +02002722
tierno98e909c2017-10-14 13:27:03 +02002723 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
mirabal29356312017-07-27 12:21:22 +02002724 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
tierno5a3273c2017-08-29 11:43:46 +02002725 availability_zone_index=av_index,
2726 availability_zone_list=vnf_availability_zones)
tiernoae4a8d12016-07-08 12:30:39 +02002727 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2728 vm['vim_id'] = vm_id
2729 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2730 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2731 for net in myVMDict['networks']:
2732 if "vim_id" in net:
2733 for iface in vm['interfaces']:
2734 if net["name"]==iface["internal_name"]:
2735 iface["vim_id"]=net["vim_id"]
2736 break
tierno42026a02017-02-10 15:13:40 +01002737
tiernoae4a8d12016-07-08 12:30:39 +02002738 logger.debug("start scenario Deployment done")
2739 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2740 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02002741 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2742 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01002743
tiernof97fd272016-07-11 14:32:37 +02002744 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02002745 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002746 if isinstance(e, db_base_Exception):
2747 error_text = "Exception at database"
2748 else:
2749 error_text = "Exception at VIM"
2750 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2751 #logger.error("start_scenario %s", error_text)
2752 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002753
tierno36c0b172017-01-12 18:32:28 +01002754def unify_cloud_config(cloud_config_preserve, cloud_config):
tierno40e1bce2017-08-09 09:12:04 +02002755 """ join the cloud config information into cloud_config_preserve.
tierno36c0b172017-01-12 18:32:28 +01002756 In case of conflict cloud_config_preserve preserves
tierno40e1bce2017-08-09 09:12:04 +02002757 None is allowed
2758 """
tierno36c0b172017-01-12 18:32:28 +01002759 if not cloud_config_preserve and not cloud_config:
2760 return None
2761
2762 new_cloud_config = {"key-pairs":[], "users":[]}
2763 # key-pairs
2764 if cloud_config_preserve:
2765 for key in cloud_config_preserve.get("key-pairs", () ):
2766 if key not in new_cloud_config["key-pairs"]:
2767 new_cloud_config["key-pairs"].append(key)
2768 if cloud_config:
2769 for key in cloud_config.get("key-pairs", () ):
2770 if key not in new_cloud_config["key-pairs"]:
2771 new_cloud_config["key-pairs"].append(key)
2772 if not new_cloud_config["key-pairs"]:
2773 del new_cloud_config["key-pairs"]
2774
2775 # users
2776 if cloud_config:
2777 new_cloud_config["users"] += cloud_config.get("users", () )
2778 if cloud_config_preserve:
2779 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02002780 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01002781 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02002782 for index0 in range(0,len(users)):
2783 if index0 in index_to_delete:
2784 continue
2785 for index1 in range(index0+1,len(users)):
2786 if index1 in index_to_delete:
2787 continue
2788 if users[index0]["name"] == users[index1]["name"]:
2789 index_to_delete.append(index1)
2790 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01002791 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02002792 users[index0]["key-pairs"] = [key]
2793 elif key not in users[index0]["key-pairs"]:
2794 users[index0]["key-pairs"].append(key)
2795 index_to_delete.sort(reverse=True)
2796 for index in index_to_delete:
2797 del users[index]
tierno36c0b172017-01-12 18:32:28 +01002798 if not new_cloud_config["users"]:
2799 del new_cloud_config["users"]
2800
2801 #boot-data-drive
2802 if cloud_config and cloud_config.get("boot-data-drive") != None:
2803 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2804 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2805 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2806
2807 # user-data
tierno40e1bce2017-08-09 09:12:04 +02002808 new_cloud_config["user-data"] = []
2809 if cloud_config and cloud_config.get("user-data"):
2810 if isinstance(cloud_config["user-data"], list):
2811 new_cloud_config["user-data"] += cloud_config["user-data"]
2812 else:
2813 new_cloud_config["user-data"].append(cloud_config["user-data"])
2814 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2815 if isinstance(cloud_config_preserve["user-data"], list):
2816 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2817 else:
2818 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2819 if not new_cloud_config["user-data"]:
2820 del new_cloud_config["user-data"]
tierno36c0b172017-01-12 18:32:28 +01002821
2822 # config files
2823 new_cloud_config["config-files"] = []
2824 if cloud_config and cloud_config.get("config-files") != None:
2825 new_cloud_config["config-files"] += cloud_config["config-files"]
2826 if cloud_config_preserve:
2827 for file in cloud_config_preserve.get("config-files", ()):
2828 for index in range(0, len(new_cloud_config["config-files"])):
2829 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2830 new_cloud_config["config-files"][index] = file
2831 break
2832 else:
2833 new_cloud_config["config-files"].append(file)
2834 if not new_cloud_config["config-files"]:
2835 del new_cloud_config["config-files"]
2836 return new_cloud_config
2837
2838
tierno867ffe92017-03-27 12:50:34 +02002839def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01002840 datacenter_id = None
2841 datacenter_name = None
2842 thread = None
tierno867ffe92017-03-27 12:50:34 +02002843 try:
2844 if datacenter_tenant_id:
2845 thread_id = datacenter_tenant_id
2846 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01002847 else:
tierno867ffe92017-03-27 12:50:34 +02002848 where_={"td.nfvo_tenant_id": tenant_id}
2849 if datacenter_id_name:
2850 if utils.check_valid_uuid(datacenter_id_name):
2851 datacenter_id = datacenter_id_name
2852 where_["dt.datacenter_id"] = datacenter_id
2853 else:
2854 datacenter_name = datacenter_id_name
2855 where_["d.name"] = datacenter_name
2856 if datacenter_tenant_id:
2857 where_["dt.uuid"] = datacenter_tenant_id
2858 datacenters = mydb.get_rows(
2859 SELECT=("dt.uuid as datacenter_tenant_id",),
2860 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2861 "join datacenters as d on d.uuid=dt.datacenter_id",
2862 WHERE=where_)
2863 if len(datacenters) > 1:
2864 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2865 elif datacenters:
2866 thread_id = datacenters[0]["datacenter_tenant_id"]
2867 thread = vim_threads["running"].get(thread_id)
2868 if not thread:
2869 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2870 return thread_id, thread
2871 except db_base_Exception as e:
2872 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02002873
tiernof5755962017-07-13 15:44:34 +02002874
tiernoa15c4b92017-10-05 12:41:44 +02002875def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2876 WHERE_dict={}
2877 if utils.check_valid_uuid(datacenter_id_name):
2878 WHERE_dict['d.uuid'] = datacenter_id_name
2879 else:
2880 WHERE_dict['d.name'] = datacenter_id_name
2881
2882 if tenant_id:
2883 WHERE_dict['nfvo_tenant_id'] = tenant_id
2884 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2885 " dt on td.datacenter_tenant_id=dt.uuid"
2886 else:
2887 from_ = 'datacenters as d'
tiernod3750b32018-07-20 15:33:08 +02002888 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid, d.name as name",), WHERE=WHERE_dict )
tiernoa15c4b92017-10-05 12:41:44 +02002889 if len(vimaccounts) == 0:
2890 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2891 elif len(vimaccounts)>1:
2892 #print "nfvo.datacenter_action() error. Several datacenters found"
2893 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tiernod3750b32018-07-20 15:33:08 +02002894 return vimaccounts[0]["uuid"], vimaccounts[0]["name"]
tiernoa15c4b92017-10-05 12:41:44 +02002895
2896
tiernoa2793912016-10-04 08:15:08 +00002897def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02002898 datacenter_id = None
2899 datacenter_name = None
2900 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01002901 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02002902 datacenter_id = datacenter_id_name
2903 else:
2904 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00002905 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02002906 if len(vims) == 0:
2907 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2908 elif len(vims)>1:
2909 #print "nfvo.datacenter_action() error. Several datacenters found"
2910 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2911 return vims.keys()[0], vims.values()[0]
2912
tiernob3d36742017-03-03 23:51:05 +01002913
garciadeblas9f8456e2016-09-05 05:02:59 +02002914def update(d, u):
2915 '''Takes dict d and updates it with the values in dict u.'''
2916 '''It merges all depth levels'''
2917 for k, v in u.iteritems():
2918 if isinstance(v, collections.Mapping):
2919 r = update(d.get(k, {}), v)
2920 d[k] = r
2921 else:
2922 d[k] = u[k]
2923 return d
2924
tierno16e3dd42018-04-24 12:52:40 +02002925
tierno7edb6752016-03-21 17:37:52 +01002926def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01002927 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2928 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01002929 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01002930
tierno868220c2017-09-26 00:11:05 +02002931 # find main datacenter
tiernobe41e222016-09-02 15:16:13 +02002932 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02002933 myvim_threads_id = {}
tierno7edb6752016-03-21 17:37:52 +01002934 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02002935 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
2936 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02002937 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
gcalvinoe580c7d2017-09-22 14:09:51 +02002938 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
tierno868220c2017-09-26 00:11:05 +02002939 # myvim_tenant = myvim['tenant_id']
tierno16e3dd42018-04-24 12:52:40 +02002940 rollbackList = []
tierno42026a02017-02-10 15:13:40 +01002941
tierno868220c2017-09-26 00:11:05 +02002942 # print "Checking that the scenario exists and getting the scenario dictionary"
tierno7fe82642018-11-26 14:14:51 +00002943 if isinstance(scenario, str):
2944 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
2945 datacenter_id=default_datacenter_id)
2946 else:
2947 scenarioDict = scenario
2948 scenarioDict["uuid"] = None
tierno42026a02017-02-10 15:13:40 +01002949
tierno868220c2017-09-26 00:11:05 +02002950 # logger.debug(">>>>>> Dictionaries before merging")
2951 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2952 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01002953
tierno868220c2017-09-26 00:11:05 +02002954 db_instance_vnfs = []
2955 db_instance_vms = []
2956 db_instance_interfaces = []
Igor D.Ccaadc442017-11-06 12:48:48 +00002957 db_instance_sfis = []
2958 db_instance_sfs = []
2959 db_instance_classifications = []
2960 db_instance_sfps = []
tierno868220c2017-09-26 00:11:05 +02002961 db_ip_profiles = []
2962 db_vim_actions = []
tierno8e690322017-08-10 15:58:50 +02002963 uuid_list = []
tierno868220c2017-09-26 00:11:05 +02002964 task_index = 0
tierno8e690322017-08-10 15:58:50 +02002965 instance_name = instance_dict["name"]
2966 instance_uuid = str(uuid4())
2967 uuid_list.append(instance_uuid)
2968 db_instance_scenario = {
2969 "uuid": instance_uuid,
2970 "name": instance_name,
2971 "tenant_id": tenant_id,
2972 "scenario_id": scenarioDict['uuid'],
2973 "datacenter_id": default_datacenter_id,
2974 # filled bellow 'datacenter_tenant_id'
2975 "description": instance_dict.get("description"),
2976 }
tierno8e690322017-08-10 15:58:50 +02002977 if scenarioDict.get("cloud-config"):
2978 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
2979 default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02002980 instance_action_id = get_task_id()
2981 db_instance_action = {
2982 "uuid": instance_action_id, # same uuid for the instance and the action on create
2983 "tenant_id": tenant_id,
2984 "instance_id": instance_uuid,
2985 "description": "CREATE",
2986 }
garciadeblas9f8456e2016-09-05 05:02:59 +02002987
tierno868220c2017-09-26 00:11:05 +02002988 # Auxiliary dictionaries from x to y
tierno8e690322017-08-10 15:58:50 +02002989 sce_net2instance = {}
tierno868220c2017-09-26 00:11:05 +02002990 net2task_id = {'scenario': {}}
tierno42026a02017-02-10 15:13:40 +01002991
tierno1df468d2018-07-06 14:25:16 +02002992 def ip_profile_IM2RO(ip_profile_im):
2993 # translate from input format to database format
2994 ip_profile_ro = {}
2995 if 'subnet-address' in ip_profile_im:
2996 ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address']
2997 if 'ip-version' in ip_profile_im:
2998 ip_profile_ro['ip_version'] = ip_profile_im['ip-version']
2999 if 'gateway-address' in ip_profile_im:
3000 ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address']
3001 if 'dns-address' in ip_profile_im:
3002 ip_profile_ro['dns_address'] = ip_profile_im['dns-address']
3003 if isinstance(ip_profile_ro['dns_address'], (list, tuple)):
3004 ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address'])
3005 if 'dhcp' in ip_profile_im:
3006 ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address')
3007 ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True)
3008 ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count')
3009 return ip_profile_ro
3010
tierno868220c2017-09-26 00:11:05 +02003011 # logger.debug("Creating instance from scenario-dict:\n%s",
3012 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01003013 try:
tiernob3d36742017-03-03 23:51:05 +01003014 # 0 check correct parameters
tierno868220c2017-09-26 00:11:05 +02003015 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003016 for scenario_net in scenarioDict['nets']:
tierno1df468d2018-07-06 14:25:16 +02003017 if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"):
tierno7edb6752016-03-21 17:37:52 +01003018 break
tierno1df468d2018-07-06 14:25:16 +02003019 else:
3020 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name),
tierno868220c2017-09-26 00:11:05 +02003021 HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003022 if "sites" not in net_instance_desc:
3023 net_instance_desc["sites"] = [ {} ]
3024 site_without_datacenter_field = False
3025 for site in net_instance_desc["sites"]:
3026 if site.get("datacenter"):
tiernod3750b32018-07-20 15:33:08 +02003027 site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003028 if site["datacenter"] not in myvims:
tierno868220c2017-09-26 00:11:05 +02003029 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02003030 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
3031 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003032 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
3033 site["datacenter"] = d # change name to id
tiernobe41e222016-09-02 15:16:13 +02003034 else:
3035 if site_without_datacenter_field:
tierno868220c2017-09-26 00:11:05 +02003036 raise NfvoException("Found more than one entries without datacenter field at "
3037 "instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003038 site_without_datacenter_field = True
tierno868220c2017-09-26 00:11:05 +02003039 site["datacenter"] = default_datacenter_id # change name to id
tierno42026a02017-02-10 15:13:40 +01003040
tiernobe41e222016-09-02 15:16:13 +02003041 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01003042 for scenario_vnf in scenarioDict['vnfs']:
tierno1df468d2018-07-06 14:25:16 +02003043 if vnf_name == scenario_vnf['member_vnf_index'] or vnf_name == scenario_vnf['uuid'] or vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01003044 break
tierno1df468d2018-07-06 14:25:16 +02003045 else:
tierno92c36fd2018-05-04 12:21:10 +02003046 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02003047 if "datacenter" in vnf_instance_desc:
tierno868220c2017-09-26 00:11:05 +02003048 # Add this datacenter to myvims
tiernod3750b32018-07-20 15:33:08 +02003049 vnf_instance_desc["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02003050 if vnf_instance_desc["datacenter"] not in myvims:
3051 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
3052 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02003053 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernoa2793912016-10-04 08:15:08 +00003054 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01003055
tierno1df468d2018-07-06 14:25:16 +02003056 for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems():
3057 for scenario_net in scenario_vnf['nets']:
3058 if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]:
3059 break
3060 else:
3061 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), HTTP_Bad_Request)
3062 if net_instance_desc.get("vim-network-name"):
3063 scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"]
3064 if net_instance_desc.get("name"):
3065 scenario_net["name"] = net_instance_desc["name"]
3066 if 'ip-profile' in net_instance_desc:
3067 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
3068 if 'ip_profile' not in scenario_net:
3069 scenario_net['ip_profile'] = ipprofile_db
3070 else:
3071 update(scenario_net['ip_profile'], ipprofile_db)
3072
3073 for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems():
3074 for scenario_vm in scenario_vnf['vms']:
3075 if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]:
3076 break
3077 else:
3078 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), HTTP_Bad_Request)
3079 scenario_vm["instance_parameters"] = vdu_instance_desc
3080 for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems():
3081 for scenario_interface in scenario_vm['interfaces']:
3082 if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]:
3083 scenario_interface.update(iface_instance_desc)
3084 break
3085 else:
3086 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), HTTP_Bad_Request)
3087
tierno868220c2017-09-26 00:11:05 +02003088 # 0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01003089 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02003090
tierno868220c2017-09-26 00:11:05 +02003091 # 0.2 merge instance information into scenario
3092 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3093 # However, this is not possible yet.
tierno41a69812018-02-16 14:34:33 +01003094 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
garciadeblas9f8456e2016-09-05 05:02:59 +02003095 for scenario_net in scenarioDict['nets']:
3096 if net_name == scenario_net["name"]:
3097 if 'ip-profile' in net_instance_desc:
tierno1df468d2018-07-06 14:25:16 +02003098 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
garciadeblasedca7b32016-09-29 14:01:52 +00003099 if 'ip_profile' not in scenario_net:
tierno455612d2017-05-30 16:40:10 +02003100 scenario_net['ip_profile'] = ipprofile_db
garciadeblasedca7b32016-09-29 14:01:52 +00003101 else:
tierno455612d2017-05-30 16:40:10 +02003102 update(scenario_net['ip_profile'], ipprofile_db)
tierno41a69812018-02-16 14:34:33 +01003103 for interface in net_instance_desc.get('interfaces', ()):
garciadeblas9f8456e2016-09-05 05:02:59 +02003104 if 'ip_address' in interface:
3105 for vnf in scenarioDict['vnfs']:
3106 if interface['vnf'] == vnf['name']:
3107 for vnf_interface in vnf['interfaces']:
3108 if interface['vnf_interface'] == vnf_interface['external_name']:
tierno41a69812018-02-16 14:34:33 +01003109 vnf_interface['ip_address'] = interface['ip_address']
garciadeblas9f8456e2016-09-05 05:02:59 +02003110
tierno868220c2017-09-26 00:11:05 +02003111 # logger.debug(">>>>>>>> Merged dictionary")
3112 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3113 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02003114
tiernob3d36742017-03-03 23:51:05 +01003115 # 1. Creating new nets (sce_nets) in the VIM"
tierno8f79ea12018-05-03 17:37:40 +02003116 number_mgmt_networks = 0
tierno8e690322017-08-10 15:58:50 +02003117 db_instance_nets = []
tierno7edb6752016-03-21 17:37:52 +01003118 for sce_net in scenarioDict['nets']:
tierno7fe82642018-11-26 14:14:51 +00003119 sce_net_uuid = sce_net.get('uuid', sce_net["name"])
tierno1df468d2018-07-06 14:25:16 +02003120 # get involved datacenters where this network need to be created
3121 involved_datacenters = []
tierno7fe82642018-11-26 14:14:51 +00003122 for sce_vnf in scenarioDict.get("vnfs", ()):
tierno1df468d2018-07-06 14:25:16 +02003123 vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id)
3124 if vnf_datacenter in involved_datacenters:
3125 continue
3126 if sce_vnf.get("interfaces"):
3127 for sce_vnf_ifaces in sce_vnf["interfaces"]:
3128 if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]:
3129 involved_datacenters.append(vnf_datacenter)
3130 break
gcalvinod6fac4d2018-11-05 10:42:06 +01003131 if not involved_datacenters:
3132 involved_datacenters.append(default_datacenter_id)
tierno1df468d2018-07-06 14:25:16 +02003133
3134 descriptor_net = {}
3135 if instance_dict.get("networks") and instance_dict["networks"].get(sce_net["name"]):
3136 descriptor_net = instance_dict["networks"][sce_net["name"]]
tiernobe41e222016-09-02 15:16:13 +02003137 net_name = descriptor_net.get("vim-network-name")
tierno7fe82642018-11-26 14:14:51 +00003138 # add datacenters from instantiation parameters
3139 if descriptor_net.get("sites"):
3140 for site in descriptor_net["sites"]:
3141 if site.get("datacenter") and site["datacenter"] not in involved_datacenters:
3142 involved_datacenters.append(site["datacenter"])
3143 sce_net2instance[sce_net_uuid] = {}
3144 net2task_id['scenario'][sce_net_uuid] = {}
tiernobe41e222016-09-02 15:16:13 +02003145
tierno1df468d2018-07-06 14:25:16 +02003146 if sce_net["external"]:
3147 number_mgmt_networks += 1
3148
3149 for datacenter_id in involved_datacenters:
3150 netmap_use = None
3151 netmap_create = None
3152 if descriptor_net.get("sites"):
3153 for site in descriptor_net["sites"]:
3154 if site.get("datacenter") == datacenter_id:
3155 netmap_use = site.get("netmap-use")
3156 netmap_create = site.get("netmap-create")
3157 break
3158
3159 vim = myvims[datacenter_id]
3160 myvim_thread_id = myvim_threads_id[datacenter_id]
3161
tiernobe41e222016-09-02 15:16:13 +02003162 net_type = sce_net['type']
tiernob6990792018-11-13 10:37:42 +01003163 net_vim_name = None
tierno868220c2017-09-26 00:11:05 +02003164 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
tierno42026a02017-02-10 15:13:40 +01003165
tiernof1ba57e2017-09-07 12:23:19 +02003166 if not net_name:
3167 if sce_net["external"]:
3168 net_name = sce_net["name"]
3169 else:
tierno1df468d2018-07-06 14:25:16 +02003170 net_name = "{}-{}".format(instance_name, sce_net["name"])
tiernof1ba57e2017-09-07 12:23:19 +02003171 net_name = net_name[:255] # limit length
3172
tierno1df468d2018-07-06 14:25:16 +02003173 if netmap_use or netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003174 create_network = False
3175 lookfor_network = False
tierno1df468d2018-07-06 14:25:16 +02003176 if netmap_use:
tiernof1ba57e2017-09-07 12:23:19 +02003177 lookfor_network = True
tierno1df468d2018-07-06 14:25:16 +02003178 if utils.check_valid_uuid(netmap_use):
3179 lookfor_filter["id"] = netmap_use
tiernof1ba57e2017-09-07 12:23:19 +02003180 else:
tierno1df468d2018-07-06 14:25:16 +02003181 lookfor_filter["name"] = netmap_use
3182 if netmap_create:
tiernof1ba57e2017-09-07 12:23:19 +02003183 create_network = True
3184 net_vim_name = net_name
tierno1df468d2018-07-06 14:25:16 +02003185 if isinstance(netmap_create, str):
3186 net_vim_name = netmap_create
tierno8f79ea12018-05-03 17:37:40 +02003187 elif sce_net.get("vim_network_name"):
3188 create_network = False
3189 lookfor_network = True
3190 lookfor_filter["name"] = sce_net.get("vim_network_name")
tiernof1ba57e2017-09-07 12:23:19 +02003191 elif sce_net["external"]:
tierno1df468d2018-07-06 14:25:16 +02003192 if sce_net['vim_id'] is not None:
tierno868220c2017-09-26 00:11:05 +02003193 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
tiernobe41e222016-09-02 15:16:13 +02003194 create_network = False
3195 lookfor_network = True
3196 lookfor_filter["id"] = sce_net['vim_id']
tierno8f79ea12018-05-03 17:37:40 +02003197 elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"):
3198 if number_mgmt_networks > 1:
3199 raise NfvoException("Found several VLD of type mgmt. "
3200 "You must concrete what vim-network must be use for each one",
3201 HTTP_Bad_Request)
3202 create_network = False
3203 lookfor_network = True
3204 if vim["config"].get("management_network_id"):
3205 lookfor_filter["id"] = vim["config"]["management_network_id"]
3206 else:
3207 lookfor_filter["name"] = vim["config"]["management_network_name"]
tiernobe41e222016-09-02 15:16:13 +02003208 else:
tierno868220c2017-09-26 00:11:05 +02003209 # There is not a netmap, look at datacenter for a net with this name and create if not found
tiernobe41e222016-09-02 15:16:13 +02003210 create_network = True
3211 lookfor_network = True
3212 lookfor_filter["name"] = sce_net["name"]
3213 net_vim_name = sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01003214 else:
tiernobe41e222016-09-02 15:16:13 +02003215 net_vim_name = net_name
3216 create_network = True
3217 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01003218
tiernof1450872017-10-17 23:15:08 +02003219 task_extra = {}
3220 if create_network:
3221 task_action = "CREATE"
3222 task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None))
3223 if lookfor_network:
3224 task_extra["find"] = (lookfor_filter,)
tierno868220c2017-09-26 00:11:05 +02003225 elif lookfor_network:
3226 task_action = "FIND"
tiernof1450872017-10-17 23:15:08 +02003227 task_extra["params"] = (lookfor_filter,)
tierno42026a02017-02-10 15:13:40 +01003228
tierno8e690322017-08-10 15:58:50 +02003229 # fill database content
3230 net_uuid = str(uuid4())
3231 uuid_list.append(net_uuid)
tierno7fe82642018-11-26 14:14:51 +00003232 sce_net2instance[sce_net_uuid][datacenter_id] = net_uuid
tierno8e690322017-08-10 15:58:50 +02003233 db_net = {
3234 "uuid": net_uuid,
tierno868220c2017-09-26 00:11:05 +02003235 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003236 "vim_name": net_vim_name,
tierno8e690322017-08-10 15:58:50 +02003237 "instance_scenario_id": instance_uuid,
tierno7fe82642018-11-26 14:14:51 +00003238 "sce_net_id": sce_net.get("uuid"),
tierno8e690322017-08-10 15:58:50 +02003239 "created": create_network,
3240 'datacenter_id': datacenter_id,
3241 'datacenter_tenant_id': myvim_thread_id,
tiernod2836fc2018-05-30 15:03:27 +02003242 'status': 'BUILD' # if create_network else "ACTIVE"
tierno8e690322017-08-10 15:58:50 +02003243 }
3244 db_instance_nets.append(db_net)
tierno868220c2017-09-26 00:11:05 +02003245 db_vim_action = {
3246 "instance_action_id": instance_action_id,
3247 "status": "SCHEDULED",
3248 "task_index": task_index,
3249 "datacenter_vim_id": myvim_thread_id,
3250 "action": task_action,
3251 "item": "instance_nets",
3252 "item_id": net_uuid,
tiernof1450872017-10-17 23:15:08 +02003253 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02003254 }
tierno7fe82642018-11-26 14:14:51 +00003255 net2task_id['scenario'][sce_net_uuid][datacenter_id] = task_index
tierno868220c2017-09-26 00:11:05 +02003256 task_index += 1
3257 db_vim_actions.append(db_vim_action)
3258
tierno8e690322017-08-10 15:58:50 +02003259 if 'ip_profile' in sce_net:
3260 db_ip_profile={
3261 'instance_net_id': net_uuid,
3262 'ip_version': sce_net['ip_profile']['ip_version'],
3263 'subnet_address': sce_net['ip_profile']['subnet_address'],
3264 'gateway_address': sce_net['ip_profile']['gateway_address'],
3265 'dns_address': sce_net['ip_profile']['dns_address'],
3266 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
3267 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
3268 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
3269 }
3270 db_ip_profiles.append(db_ip_profile)
3271
tierno16e3dd42018-04-24 12:52:40 +02003272 # Create VNFs
3273 vnf_params = {
3274 "default_datacenter_id": default_datacenter_id,
3275 "myvim_threads_id": myvim_threads_id,
3276 "instance_uuid": instance_uuid,
3277 "instance_name": instance_name,
3278 "instance_action_id": instance_action_id,
3279 "myvims": myvims,
3280 "cloud_config": cloud_config,
3281 "RO_pub_key": tenant[0].get('RO_pub_key'),
tierno67881db2018-10-24 18:46:03 +02003282 "instance_parameters": instance_dict,
tierno16e3dd42018-04-24 12:52:40 +02003283 }
3284 vnf_params_out = {
3285 "task_index": task_index,
3286 "uuid_list": uuid_list,
3287 "db_instance_nets": db_instance_nets,
3288 "db_vim_actions": db_vim_actions,
3289 "db_ip_profiles": db_ip_profiles,
3290 "db_instance_vnfs": db_instance_vnfs,
3291 "db_instance_vms": db_instance_vms,
3292 "db_instance_interfaces": db_instance_interfaces,
3293 "net2task_id": net2task_id,
3294 "sce_net2instance": sce_net2instance,
3295 }
tierno55d234c2018-07-04 18:29:21 +02003296 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
tierno7fe82642018-11-26 14:14:51 +00003297 for sce_vnf in scenarioDict.get('vnfs', ()): # sce_vnf_list:
tierno16e3dd42018-04-24 12:52:40 +02003298 instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList)
3299 task_index = vnf_params_out["task_index"]
3300 uuid_list = vnf_params_out["uuid_list"]
mirabal29356312017-07-27 12:21:22 +02003301
tierno16e3dd42018-04-24 12:52:40 +02003302 # Create VNFFGs
3303 # task_depends_on = []
tierno7fe82642018-11-26 14:14:51 +00003304 for vnffg in scenarioDict.get('vnffgs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003305 for rsp in vnffg['rsps']:
3306 sfs_created = []
3307 for cp in rsp['connection_points']:
3308 count = mydb.get_rows(
3309 SELECT=('vms.count'),
3310 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h on interfaces.uuid=h.interface_id",
3311 WHERE={'h.uuid': cp['uuid']})[0]['count']
3312 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None)
3313 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3314 dependencies = []
3315 for instance_vm in instance_vms:
3316 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3317 if action:
3318 dependencies.append(action['task_index'])
3319 # TODO: throw exception if count != len(instance_vms)
3320 # TODO: and action shouldn't ever be None
3321 sfis_created = []
3322 for i in range(count):
3323 # create sfis
3324 sfi_uuid = str(uuid4())
3325 uuid_list.append(sfi_uuid)
3326 db_sfi = {
3327 "uuid": sfi_uuid,
3328 "instance_scenario_id": instance_uuid,
3329 'sce_rsp_hop_id': cp['uuid'],
3330 'datacenter_id': datacenter_id,
3331 'datacenter_tenant_id': myvim_thread_id,
3332 "vim_sfi_id": None, # vim thread will populate
3333 }
3334 db_instance_sfis.append(db_sfi)
3335 db_vim_action = {
3336 "instance_action_id": instance_action_id,
3337 "task_index": task_index,
3338 "datacenter_vim_id": myvim_thread_id,
3339 "action": "CREATE",
3340 "status": "SCHEDULED",
3341 "item": "instance_sfis",
3342 "item_id": sfi_uuid,
3343 "extra": yaml.safe_dump({"params": "", "depends_on": [dependencies[i]]},
3344 default_flow_style=True, width=256)
3345 }
3346 sfis_created.append(task_index)
3347 task_index += 1
3348 db_vim_actions.append(db_vim_action)
3349 # create sfs
3350 sf_uuid = str(uuid4())
3351 uuid_list.append(sf_uuid)
3352 db_sf = {
3353 "uuid": sf_uuid,
3354 "instance_scenario_id": instance_uuid,
3355 'sce_rsp_hop_id': cp['uuid'],
3356 'datacenter_id': datacenter_id,
3357 'datacenter_tenant_id': myvim_thread_id,
3358 "vim_sf_id": None, # vim thread will populate
3359 }
3360 db_instance_sfs.append(db_sf)
3361 db_vim_action = {
3362 "instance_action_id": instance_action_id,
3363 "task_index": task_index,
3364 "datacenter_vim_id": myvim_thread_id,
3365 "action": "CREATE",
3366 "status": "SCHEDULED",
3367 "item": "instance_sfs",
3368 "item_id": sf_uuid,
3369 "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created},
3370 default_flow_style=True, width=256)
3371 }
3372 sfs_created.append(task_index)
3373 task_index += 1
3374 db_vim_actions.append(db_vim_action)
3375 classifier = rsp['classifier']
3376
3377 # TODO the following ~13 lines can be reused for the sfi case
3378 count = mydb.get_rows(
3379 SELECT=('vms.count'),
3380 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3381 WHERE={'c.uuid': classifier['uuid']})[0]['count']
3382 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None)
3383 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3384 dependencies = []
3385 for instance_vm in instance_vms:
3386 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3387 if action:
3388 dependencies.append(action['task_index'])
3389 # TODO: throw exception if count != len(instance_vms)
3390 # TODO: and action shouldn't ever be None
3391 classifications_created = []
3392 for i in range(count):
3393 for match in classifier['matches']:
3394 # create classifications
3395 classification_uuid = str(uuid4())
3396 uuid_list.append(classification_uuid)
3397 db_classification = {
3398 "uuid": classification_uuid,
3399 "instance_scenario_id": instance_uuid,
3400 'sce_classifier_match_id': match['uuid'],
3401 'datacenter_id': datacenter_id,
3402 'datacenter_tenant_id': myvim_thread_id,
3403 "vim_classification_id": None, # vim thread will populate
3404 }
3405 db_instance_classifications.append(db_classification)
3406 classification_params = {
3407 "ip_proto": match["ip_proto"],
3408 "source_ip": match["source_ip"],
3409 "destination_ip": match["destination_ip"],
3410 "source_port": match["source_port"],
3411 "destination_port": match["destination_port"]
3412 }
3413 db_vim_action = {
3414 "instance_action_id": instance_action_id,
3415 "task_index": task_index,
3416 "datacenter_vim_id": myvim_thread_id,
3417 "action": "CREATE",
3418 "status": "SCHEDULED",
3419 "item": "instance_classifications",
3420 "item_id": classification_uuid,
3421 "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]},
3422 default_flow_style=True, width=256)
3423 }
3424 classifications_created.append(task_index)
3425 task_index += 1
3426 db_vim_actions.append(db_vim_action)
3427
3428 # create sfps
3429 sfp_uuid = str(uuid4())
3430 uuid_list.append(sfp_uuid)
3431 db_sfp = {
3432 "uuid": sfp_uuid,
3433 "instance_scenario_id": instance_uuid,
3434 'sce_rsp_id': rsp['uuid'],
3435 'datacenter_id': datacenter_id,
3436 'datacenter_tenant_id': myvim_thread_id,
3437 "vim_sfp_id": None, # vim thread will populate
3438 }
3439 db_instance_sfps.append(db_sfp)
3440 db_vim_action = {
3441 "instance_action_id": instance_action_id,
3442 "task_index": task_index,
3443 "datacenter_vim_id": myvim_thread_id,
3444 "action": "CREATE",
3445 "status": "SCHEDULED",
3446 "item": "instance_sfps",
3447 "item_id": sfp_uuid,
3448 "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created},
3449 default_flow_style=True, width=256)
3450 }
3451 task_index += 1
3452 db_vim_actions.append(db_vim_action)
3453
tierno867ffe92017-03-27 12:50:34 +02003454 scenarioDict["datacenter2tenant"] = myvim_threads_id
tierno8e690322017-08-10 15:58:50 +02003455
tierno868220c2017-09-26 00:11:05 +02003456 db_instance_action["number_tasks"] = task_index
tierno8e690322017-08-10 15:58:50 +02003457 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3458 db_instance_scenario['datacenter_id'] = default_datacenter_id
3459 db_tables=[
3460 {"instance_scenarios": db_instance_scenario},
3461 {"instance_vnfs": db_instance_vnfs},
3462 {"instance_nets": db_instance_nets},
3463 {"ip_profiles": db_ip_profiles},
3464 {"instance_vms": db_instance_vms},
3465 {"instance_interfaces": db_instance_interfaces},
tierno868220c2017-09-26 00:11:05 +02003466 {"instance_actions": db_instance_action},
Igor D.Ccaadc442017-11-06 12:48:48 +00003467 {"instance_sfis": db_instance_sfis},
3468 {"instance_sfs": db_instance_sfs},
3469 {"instance_classifications": db_instance_classifications},
3470 {"instance_sfps": db_instance_sfps},
tierno868220c2017-09-26 00:11:05 +02003471 {"vim_actions": db_vim_actions}
tierno8e690322017-08-10 15:58:50 +02003472 ]
3473
tierno868220c2017-09-26 00:11:05 +02003474 logger.debug("create_instance done DB tables: %s",
tierno8e690322017-08-10 15:58:50 +02003475 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3476 mydb.new_rows(db_tables, uuid_list)
tierno868220c2017-09-26 00:11:05 +02003477 for myvim_thread_id in myvim_threads_id.values():
3478 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
tierno867ffe92017-03-27 12:50:34 +02003479
tierno868220c2017-09-26 00:11:05 +02003480 returned_instance = mydb.get_instance_scenario(instance_uuid)
3481 returned_instance["action_id"] = instance_action_id
3482 return returned_instance
3483 except (NfvoException, vimconn.vimconnException, db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02003484 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02003485 if isinstance(e, db_base_Exception):
3486 error_text = "database Exception"
3487 elif isinstance(e, vimconn.vimconnException):
3488 error_text = "VIM Exception"
3489 else:
3490 error_text = "Exception"
3491 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
tierno868220c2017-09-26 00:11:05 +02003492 # logger.error("create_instance: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02003493 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01003494
tiernob3d36742017-03-03 23:51:05 +01003495
tierno16e3dd42018-04-24 12:52:40 +02003496def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
3497 default_datacenter_id = params["default_datacenter_id"]
3498 myvim_threads_id = params["myvim_threads_id"]
3499 instance_uuid = params["instance_uuid"]
3500 instance_name = params["instance_name"]
3501 instance_action_id = params["instance_action_id"]
3502 myvims = params["myvims"]
3503 cloud_config = params["cloud_config"]
3504 RO_pub_key = params["RO_pub_key"]
3505
3506 task_index = params_out["task_index"]
3507 uuid_list = params_out["uuid_list"]
3508 db_instance_nets = params_out["db_instance_nets"]
3509 db_vim_actions = params_out["db_vim_actions"]
3510 db_ip_profiles = params_out["db_ip_profiles"]
3511 db_instance_vnfs = params_out["db_instance_vnfs"]
3512 db_instance_vms = params_out["db_instance_vms"]
3513 db_instance_interfaces = params_out["db_instance_interfaces"]
3514 net2task_id = params_out["net2task_id"]
3515 sce_net2instance = params_out["sce_net2instance"]
3516
3517 vnf_net2instance = {}
3518
3519 # 2. Creating new nets (vnf internal nets) in the VIM"
3520 # For each vnf net, we create it and we add it to instanceNetlist.
3521 if sce_vnf.get("datacenter"):
3522 datacenter_id = sce_vnf["datacenter"]
3523 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3524 else:
3525 datacenter_id = default_datacenter_id
3526 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3527 for net in sce_vnf['nets']:
3528 # TODO revis
3529 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3530 # net_name = descriptor_net.get("name")
3531 net_name = None
3532 if not net_name:
tierno1df468d2018-07-06 14:25:16 +02003533 net_name = "{}-{}".format(instance_name, net["name"])
tierno16e3dd42018-04-24 12:52:40 +02003534 net_name = net_name[:255] # limit length
3535 net_type = net['type']
3536
3537 if sce_vnf['uuid'] not in vnf_net2instance:
3538 vnf_net2instance[sce_vnf['uuid']] = {}
3539 if sce_vnf['uuid'] not in net2task_id:
3540 net2task_id[sce_vnf['uuid']] = {}
3541 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
3542
3543 # fill database content
3544 net_uuid = str(uuid4())
3545 uuid_list.append(net_uuid)
3546 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
3547 db_net = {
3548 "uuid": net_uuid,
3549 'vim_net_id': None,
tiernob6990792018-11-13 10:37:42 +01003550 "vim_name": net_name,
tierno16e3dd42018-04-24 12:52:40 +02003551 "instance_scenario_id": instance_uuid,
3552 "net_id": net["uuid"],
3553 "created": True,
3554 'datacenter_id': datacenter_id,
3555 'datacenter_tenant_id': myvim_thread_id,
3556 }
3557 db_instance_nets.append(db_net)
3558
tierno1df468d2018-07-06 14:25:16 +02003559 if net.get("vim-network-name"):
3560 lookfor_filter = {"name": net["vim-network-name"]}
3561 task_action = "FIND"
3562 task_extra = {"params": (lookfor_filter,)}
3563 else:
3564 task_action = "CREATE"
3565 task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))}
3566
tierno16e3dd42018-04-24 12:52:40 +02003567 db_vim_action = {
3568 "instance_action_id": instance_action_id,
3569 "task_index": task_index,
3570 "datacenter_vim_id": myvim_thread_id,
3571 "status": "SCHEDULED",
tierno1df468d2018-07-06 14:25:16 +02003572 "action": task_action,
tierno16e3dd42018-04-24 12:52:40 +02003573 "item": "instance_nets",
3574 "item_id": net_uuid,
tierno1df468d2018-07-06 14:25:16 +02003575 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno16e3dd42018-04-24 12:52:40 +02003576 }
3577 task_index += 1
3578 db_vim_actions.append(db_vim_action)
3579
3580 if 'ip_profile' in net:
3581 db_ip_profile = {
3582 'instance_net_id': net_uuid,
3583 'ip_version': net['ip_profile']['ip_version'],
3584 'subnet_address': net['ip_profile']['subnet_address'],
3585 'gateway_address': net['ip_profile']['gateway_address'],
3586 'dns_address': net['ip_profile']['dns_address'],
3587 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
3588 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
3589 'dhcp_count': net['ip_profile']['dhcp_count'],
3590 }
3591 db_ip_profiles.append(db_ip_profile)
3592
3593 # print "vnf_net2instance:"
3594 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3595
3596 # 3. Creating new vm instances in the VIM
3597 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3598 ssh_access = None
3599 if sce_vnf.get('mgmt_access'):
3600 ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access')
3601 vnf_availability_zones = []
gcalvinod6fac4d2018-11-05 10:42:06 +01003602 for vm in sce_vnf.get('vms'):
tierno16e3dd42018-04-24 12:52:40 +02003603 vm_av = vm.get('availability_zone')
3604 if vm_av and vm_av not in vnf_availability_zones:
3605 vnf_availability_zones.append(vm_av)
3606
3607 # check if there is enough availability zones available at vim level.
3608 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
3609 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
3610 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
3611
3612 if sce_vnf.get("datacenter"):
3613 vim = myvims[sce_vnf["datacenter"]]
3614 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3615 datacenter_id = sce_vnf["datacenter"]
3616 else:
3617 vim = myvims[default_datacenter_id]
3618 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3619 datacenter_id = default_datacenter_id
3620 sce_vnf["datacenter_id"] = datacenter_id
3621 i = 0
3622
3623 vnf_uuid = str(uuid4())
3624 uuid_list.append(vnf_uuid)
3625 db_instance_vnf = {
3626 'uuid': vnf_uuid,
3627 'instance_scenario_id': instance_uuid,
3628 'vnf_id': sce_vnf['vnf_id'],
3629 'sce_vnf_id': sce_vnf['uuid'],
3630 'datacenter_id': datacenter_id,
3631 'datacenter_tenant_id': myvim_thread_id,
3632 }
3633 db_instance_vnfs.append(db_instance_vnf)
3634
3635 for vm in sce_vnf['vms']:
tiernob6990792018-11-13 10:37:42 +01003636 # skip PDUs
3637 if vm.get("pdu_type"):
3638 continue
3639
tierno16e3dd42018-04-24 12:52:40 +02003640 myVMDict = {}
tierno7f426e92018-06-28 15:21:32 +02003641 sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name']
3642 myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64])
tierno16e3dd42018-04-24 12:52:40 +02003643 myVMDict['description'] = myVMDict['name'][0:99]
3644 # if not startvms:
3645 # myVMDict['start'] = "no"
tierno1df468d2018-07-06 14:25:16 +02003646 if vm.get("instance_parameters") and vm["instance_parameters"].get("name"):
3647 myVMDict['name'] = vm["instance_parameters"].get("name")
tierno16e3dd42018-04-24 12:52:40 +02003648 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
3649 # create image at vim in case it not exist
3650 image_uuid = vm['image_id']
3651 if vm.get("image_list"):
3652 for alternative_image in vm["image_list"]:
tiernob6434212018-04-26 16:27:47 +02003653 if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]:
tierno16e3dd42018-04-24 12:52:40 +02003654 image_uuid = alternative_image['image_id']
3655 break
3656 image_dict = mydb.get_table_by_uuid_name("images", image_uuid)
3657 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
3658 vm['vim_image_id'] = image_id
3659
3660 # create flavor at vim in case it not exist
3661 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
3662 if flavor_dict['extended'] != None:
3663 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
3664 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
3665
3666 # Obtain information for additional disks
3667 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',),
3668 WHERE={'vim_id': flavor_id})
3669 if not extended_flavor_dict:
3670 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
tierno16e3dd42018-04-24 12:52:40 +02003671
3672 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3673 myVMDict['disks'] = None
3674 extended_info = extended_flavor_dict[0]['extended']
3675 if extended_info != None:
3676 extended_flavor_dict_yaml = yaml.load(extended_info)
3677 if 'disks' in extended_flavor_dict_yaml:
3678 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
tierno1df468d2018-07-06 14:25:16 +02003679 if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"):
3680 for disk in myVMDict['disks']:
3681 if disk.get("name") in vm["instance_parameters"]["devices"]:
3682 disk.update(vm["instance_parameters"]["devices"][disk.get("name")])
tierno16e3dd42018-04-24 12:52:40 +02003683
3684 vm['vim_flavor_id'] = flavor_id
3685 myVMDict['imageRef'] = vm['vim_image_id']
3686 myVMDict['flavorRef'] = vm['vim_flavor_id']
3687 myVMDict['availability_zone'] = vm.get('availability_zone')
3688 myVMDict['networks'] = []
3689 task_depends_on = []
3690 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno67881db2018-10-24 18:46:03 +02003691 is_management_vm = False
tierno16e3dd42018-04-24 12:52:40 +02003692 db_vm_ifaces = []
3693 for iface in vm['interfaces']:
3694 netDict = {}
3695 if iface['type'] == "data":
3696 netDict['type'] = iface['model']
3697 elif "model" in iface and iface["model"] != None:
3698 netDict['model'] = iface['model']
3699 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3700 # is obtained from iterface table model
3701 # discover type of interface looking at flavor
3702 for numa in flavor_dict.get('extended', {}).get('numas', []):
3703 for flavor_iface in numa.get('interfaces', []):
3704 if flavor_iface.get('name') == iface['internal_name']:
3705 if flavor_iface['dedicated'] == 'yes':
3706 netDict['type'] = "PF" # passthrough
3707 elif flavor_iface['dedicated'] == 'no':
3708 netDict['type'] = "VF" # siov
3709 elif flavor_iface['dedicated'] == 'yes:sriov':
3710 netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF
3711 netDict["mac_address"] = flavor_iface.get("mac_address")
3712 break
3713 netDict["use"] = iface['type']
3714 if netDict["use"] == "data" and not netDict.get("type"):
3715 # print "netDict", netDict
3716 # print "iface", iface
3717 e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3718 sce_vnf['name'], vm['name'], iface['internal_name'])
3719 if flavor_dict.get('extended') == None:
3720 raise NfvoException(e_text + "After database migration some information is not available. \
3721 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
3722 else:
3723 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tierno67881db2018-10-24 18:46:03 +02003724 if netDict["use"] == "mgmt":
3725 is_management_vm = True
3726 netDict["type"] = "virtual"
3727 if netDict["use"] == "bridge":
tierno16e3dd42018-04-24 12:52:40 +02003728 netDict["type"] = "virtual"
3729 if iface.get("vpci"):
3730 netDict['vpci'] = iface['vpci']
3731 if iface.get("mac"):
3732 netDict['mac_address'] = iface['mac']
tierno6082b7d2018-08-31 11:24:08 +00003733 if iface.get("mac_address"):
3734 netDict['mac_address'] = iface['mac_address']
tierno16e3dd42018-04-24 12:52:40 +02003735 if iface.get("ip_address"):
3736 netDict['ip_address'] = iface['ip_address']
3737 if iface.get("port-security") is not None:
3738 netDict['port_security'] = iface['port-security']
3739 if iface.get("floating-ip") is not None:
3740 netDict['floating_ip'] = iface['floating-ip']
3741 netDict['name'] = iface['internal_name']
3742 if iface['net_id'] is None:
3743 for vnf_iface in sce_vnf["interfaces"]:
3744 # print iface
3745 # print vnf_iface
3746 if vnf_iface['interface_id'] == iface['uuid']:
3747 netDict['net_id'] = "TASK-{}".format(
3748 net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3749 instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id]
3750 task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3751 break
3752 else:
3753 netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']])
3754 instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']]
3755 task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']])
3756 # skip bridge ifaces not connected to any net
3757 if 'net_id' not in netDict or netDict['net_id'] == None:
3758 continue
3759 myVMDict['networks'].append(netDict)
3760 db_vm_iface = {
3761 # "uuid"
3762 # 'instance_vm_id': instance_vm_uuid,
3763 "instance_net_id": instance_net_id,
3764 'interface_id': iface['uuid'],
3765 # 'vim_interface_id': ,
3766 'type': 'external' if iface['external_name'] is not None else 'internal',
3767 'ip_address': iface.get('ip_address'),
3768 'mac_address': iface.get('mac'),
3769 'floating_ip': int(iface.get('floating-ip', False)),
3770 'port_security': int(iface.get('port-security', True))
3771 }
3772 db_vm_ifaces.append(db_vm_iface)
3773 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3774 # print myVMDict['name']
3775 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3776 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3777 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3778
3779 # We add the RO key to cloud_config if vnf will need ssh access
3780 cloud_config_vm = cloud_config
tierno67881db2018-10-24 18:46:03 +02003781 if is_management_vm and params["instance_parameters"].get("mgmt_keys"):
3782 cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]},
3783 cloud_config_vm)
3784
3785 if vm.get("instance_parameters") and vm["instance_parameters"].get("mgmt_keys"):
3786 cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]},
3787 cloud_config_vm)
3788 # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
3789 # RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
3790 # cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
tierno16e3dd42018-04-24 12:52:40 +02003791 if vm.get("boot_data"):
3792 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm)
3793
3794 if myVMDict.get('availability_zone'):
3795 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
3796 else:
3797 av_index = None
3798 for vm_index in range(0, vm.get('count', 1)):
tiernofc5f80b2018-05-29 16:00:43 +02003799 vm_name = myVMDict['name'] + "-" + str(vm_index+1)
3800 task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None),
tierno16e3dd42018-04-24 12:52:40 +02003801 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
3802 myVMDict['disks'], av_index, vnf_availability_zones)
3803 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3804 for net in myVMDict['networks']:
3805 if "vim_id" in net:
3806 for iface in vm['interfaces']:
3807 if net["name"] == iface["internal_name"]:
3808 iface["vim_id"] = net["vim_id"]
3809 break
3810 vm_uuid = str(uuid4())
3811 uuid_list.append(vm_uuid)
3812 db_vm = {
3813 "uuid": vm_uuid,
3814 'instance_vnf_id': vnf_uuid,
3815 # TODO delete "vim_vm_id": vm_id,
3816 "vm_id": vm["uuid"],
tiernofc5f80b2018-05-29 16:00:43 +02003817 "vim_name": vm_name,
tierno16e3dd42018-04-24 12:52:40 +02003818 # "status":
3819 }
3820 db_instance_vms.append(db_vm)
3821
3822 iface_index = 0
3823 for db_vm_iface in db_vm_ifaces:
3824 iface_uuid = str(uuid4())
3825 uuid_list.append(iface_uuid)
3826 db_vm_iface_instance = {
3827 "uuid": iface_uuid,
3828 "instance_vm_id": vm_uuid
3829 }
3830 db_vm_iface_instance.update(db_vm_iface)
3831 if db_vm_iface_instance.get("ip_address"): # increment ip_address
3832 ip = db_vm_iface_instance.get("ip_address")
3833 i = ip.rfind(".")
3834 if i > 0:
3835 try:
3836 i += 1
3837 ip = ip[i:] + str(int(ip[:i]) + 1)
3838 db_vm_iface_instance["ip_address"] = ip
3839 except:
3840 db_vm_iface_instance["ip_address"] = None
3841 db_instance_interfaces.append(db_vm_iface_instance)
3842 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
3843 iface_index += 1
3844
3845 db_vim_action = {
3846 "instance_action_id": instance_action_id,
3847 "task_index": task_index,
3848 "datacenter_vim_id": myvim_thread_id,
3849 "action": "CREATE",
3850 "status": "SCHEDULED",
3851 "item": "instance_vms",
3852 "item_id": vm_uuid,
3853 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
3854 default_flow_style=True, width=256)
3855 }
3856 task_index += 1
3857 db_vim_actions.append(db_vim_action)
3858 params_out["task_index"] = task_index
3859 params_out["uuid_list"] = uuid_list
3860
3861
tierno7edb6752016-03-21 17:37:52 +01003862def delete_instance(mydb, tenant_id, instance_id):
tierno868220c2017-09-26 00:11:05 +02003863 # print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02003864 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tierno868220c2017-09-26 00:11:05 +02003865 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01003866 tenant_id = instanceDict["tenant_id"]
tierno868220c2017-09-26 00:11:05 +02003867 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno868220c2017-09-26 00:11:05 +02003868 # 1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02003869 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01003870
tierno868220c2017-09-26 00:11:05 +02003871 # 2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00003872 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01003873 myvims = {}
3874 myvim_threads = {}
tierno868220c2017-09-26 00:11:05 +02003875 vimthread_affected = {}
tierno3fcfdb72017-10-24 07:48:24 +02003876 net2vm_dependencies = {}
tierno7edb6752016-03-21 17:37:52 +01003877
tierno868220c2017-09-26 00:11:05 +02003878 task_index = 0
3879 instance_action_id = get_task_id()
3880 db_vim_actions = []
3881 db_instance_action = {
3882 "uuid": instance_action_id, # same uuid for the instance and the action on create
3883 "tenant_id": tenant_id,
3884 "instance_id": instance_id,
3885 "description": "DELETE",
3886 # "number_tasks": 0 # filled bellow
3887 }
3888
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01003889 # 2.1 deleting VNFFGs
tierno69b590e2018-03-13 18:52:23 +01003890 for sfp in instanceDict.get('sfps', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003891 vimthread_affected[sfp["datacenter_tenant_id"]] = None
3892 datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"])
3893 if datacenter_key not in myvims:
3894 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01003895 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00003896 except NfvoException as e:
3897 logger.error(str(e))
3898 myvim_thread = None
3899 myvim_threads[datacenter_key] = myvim_thread
3900 vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"],
3901 datacenter_tenant_id=sfp["datacenter_tenant_id"])
3902 if len(vims) == 0:
3903 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"]))
3904 myvims[datacenter_key] = None
3905 else:
3906 myvims[datacenter_key] = vims.values()[0]
3907 myvim = myvims[datacenter_key]
3908 myvim_thread = myvim_threads[datacenter_key]
3909
3910 if not myvim:
3911 error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"])
3912 continue
3913 extra = {"params": (sfp['vim_sfp_id'])}
3914 db_vim_action = {
3915 "instance_action_id": instance_action_id,
3916 "task_index": task_index,
3917 "datacenter_vim_id": sfp["datacenter_tenant_id"],
3918 "action": "DELETE",
3919 "status": "SCHEDULED",
3920 "item": "instance_sfps",
3921 "item_id": sfp["uuid"],
3922 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3923 }
3924 task_index += 1
3925 db_vim_actions.append(db_vim_action)
3926
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01003927 for classification in instanceDict['classifications']:
3928 vimthread_affected[classification["datacenter_tenant_id"]] = None
3929 datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"])
3930 if datacenter_key not in myvims:
3931 try:
3932 _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"])
3933 except NfvoException as e:
3934 logger.error(str(e))
3935 myvim_thread = None
3936 myvim_threads[datacenter_key] = myvim_thread
3937 vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"],
3938 datacenter_tenant_id=classification["datacenter_tenant_id"])
3939 if len(vims) == 0:
3940 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"],
3941 classification["datacenter_tenant_id"]))
3942 myvims[datacenter_key] = None
3943 else:
3944 myvims[datacenter_key] = vims.values()[0]
3945 myvim = myvims[datacenter_key]
3946 myvim_thread = myvim_threads[datacenter_key]
3947
3948 if not myvim:
3949 error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'],
3950 classification["datacenter_id"])
3951 continue
3952 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
3953 extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on}
3954 db_vim_action = {
3955 "instance_action_id": instance_action_id,
3956 "task_index": task_index,
3957 "datacenter_vim_id": classification["datacenter_tenant_id"],
3958 "action": "DELETE",
3959 "status": "SCHEDULED",
3960 "item": "instance_classifications",
3961 "item_id": classification["uuid"],
3962 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3963 }
3964 task_index += 1
3965 db_vim_actions.append(db_vim_action)
3966
tierno69b590e2018-03-13 18:52:23 +01003967 for sf in instanceDict.get('sfs', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00003968 vimthread_affected[sf["datacenter_tenant_id"]] = None
3969 datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"])
3970 if datacenter_key not in myvims:
3971 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01003972 _, myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00003973 except NfvoException as e:
3974 logger.error(str(e))
3975 myvim_thread = None
3976 myvim_threads[datacenter_key] = myvim_thread
3977 vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"],
3978 datacenter_tenant_id=sf["datacenter_tenant_id"])
3979 if len(vims) == 0:
3980 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"]))
3981 myvims[datacenter_key] = None
3982 else:
3983 myvims[datacenter_key] = vims.values()[0]
3984 myvim = myvims[datacenter_key]
3985 myvim_thread = myvim_threads[datacenter_key]
3986
3987 if not myvim:
3988 error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"])
3989 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01003990 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
3991 extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00003992 db_vim_action = {
3993 "instance_action_id": instance_action_id,
3994 "task_index": task_index,
3995 "datacenter_vim_id": sf["datacenter_tenant_id"],
3996 "action": "DELETE",
3997 "status": "SCHEDULED",
3998 "item": "instance_sfs",
3999 "item_id": sf["uuid"],
4000 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4001 }
4002 task_index += 1
4003 db_vim_actions.append(db_vim_action)
4004
tierno69b590e2018-03-13 18:52:23 +01004005 for sfi in instanceDict.get('sfis', ()):
Igor D.Ccaadc442017-11-06 12:48:48 +00004006 vimthread_affected[sfi["datacenter_tenant_id"]] = None
4007 datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"])
4008 if datacenter_key not in myvims:
4009 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004010 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004011 except NfvoException as e:
4012 logger.error(str(e))
4013 myvim_thread = None
4014 myvim_threads[datacenter_key] = myvim_thread
4015 vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"],
4016 datacenter_tenant_id=sfi["datacenter_tenant_id"])
4017 if len(vims) == 0:
4018 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"]))
4019 myvims[datacenter_key] = None
4020 else:
4021 myvims[datacenter_key] = vims.values()[0]
4022 myvim = myvims[datacenter_key]
4023 myvim_thread = myvim_threads[datacenter_key]
4024
4025 if not myvim:
4026 error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"])
4027 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004028 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"]
4029 extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on}
Igor D.Ccaadc442017-11-06 12:48:48 +00004030 db_vim_action = {
4031 "instance_action_id": instance_action_id,
4032 "task_index": task_index,
4033 "datacenter_vim_id": sfi["datacenter_tenant_id"],
4034 "action": "DELETE",
4035 "status": "SCHEDULED",
4036 "item": "instance_sfis",
4037 "item_id": sfi["uuid"],
4038 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4039 }
4040 task_index += 1
4041 db_vim_actions.append(db_vim_action)
4042
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004043 # 2.2 deleting VMs
4044 # vm_fail_list=[]
gcalvinod6fac4d2018-11-05 10:42:06 +01004045 for sce_vnf in instanceDict.get('vnfs', ()):
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004046 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4047 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
Igor D.Ccaadc442017-11-06 12:48:48 +00004048 if datacenter_key not in myvims:
4049 try:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004050 _, myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004051 except NfvoException as e:
4052 logger.error(str(e))
4053 myvim_thread = None
4054 myvim_threads[datacenter_key] = myvim_thread
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004055 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
4056 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004057 if len(vims) == 0:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004058 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
4059 sce_vnf["datacenter_tenant_id"]))
4060 myvims[datacenter_key] = None
4061 else:
4062 myvims[datacenter_key] = vims.values()[0]
4063 myvim = myvims[datacenter_key]
4064 myvim_thread = myvim_threads[datacenter_key]
4065
4066 for vm in sce_vnf['vms']:
4067 if not myvim:
4068 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
4069 continue
4070 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4071 db_vim_action = {
4072 "instance_action_id": instance_action_id,
4073 "task_index": task_index,
4074 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
4075 "action": "DELETE",
4076 "status": "SCHEDULED",
4077 "item": "instance_vms",
4078 "item_id": vm["uuid"],
4079 "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies},
4080 default_flow_style=True, width=256)
4081 }
4082 db_vim_actions.append(db_vim_action)
4083 for interface in vm["interfaces"]:
4084 if not interface.get("instance_net_id"):
4085 continue
4086 if interface["instance_net_id"] not in net2vm_dependencies:
4087 net2vm_dependencies[interface["instance_net_id"]] = []
4088 net2vm_dependencies[interface["instance_net_id"]].append(task_index)
4089 task_index += 1
4090
4091 # 2.3 deleting NETS
4092 # net_fail_list=[]
4093 for net in instanceDict['nets']:
4094 vimthread_affected[net["datacenter_tenant_id"]] = None
4095 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4096 if datacenter_key not in myvims:
4097 try:
gcalvinod6fac4d2018-11-05 10:42:06 +01004098 _,myvim_thread = get_vim_thread(mydb, tenant_id, net["datacenter_id"], net["datacenter_tenant_id"])
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004099 except NfvoException as e:
4100 logger.error(str(e))
4101 myvim_thread = None
4102 myvim_threads[datacenter_key] = myvim_thread
4103 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
4104 datacenter_tenant_id=net["datacenter_tenant_id"])
4105 if len(vims) == 0:
4106 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
Igor D.Ccaadc442017-11-06 12:48:48 +00004107 myvims[datacenter_key] = None
4108 else:
4109 myvims[datacenter_key] = vims.values()[0]
4110 myvim = myvims[datacenter_key]
4111 myvim_thread = myvim_threads[datacenter_key]
4112
4113 if not myvim:
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004114 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
Igor D.Ccaadc442017-11-06 12:48:48 +00004115 continue
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004116 extra = {"params": (net['vim_net_id'], net['sdn_net_id'])}
4117 if net2vm_dependencies.get(net["uuid"]):
4118 extra["depends_on"] = net2vm_dependencies[net["uuid"]]
4119 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4120 if len(sfi_dependencies) > 0:
4121 if "depends_on" in extra:
4122 extra["depends_on"] += sfi_dependencies
4123 else:
4124 extra["depends_on"] = sfi_dependencies
Igor D.Ccaadc442017-11-06 12:48:48 +00004125 db_vim_action = {
4126 "instance_action_id": instance_action_id,
4127 "task_index": task_index,
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004128 "datacenter_vim_id": net["datacenter_tenant_id"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004129 "action": "DELETE",
4130 "status": "SCHEDULED",
Eduardo Sousaab24d8b2018-10-17 17:10:04 +01004131 "item": "instance_nets",
4132 "item_id": net["uuid"],
Igor D.Ccaadc442017-11-06 12:48:48 +00004133 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4134 }
4135 task_index += 1
4136 db_vim_actions.append(db_vim_action)
4137
tierno868220c2017-09-26 00:11:05 +02004138 db_instance_action["number_tasks"] = task_index
4139 db_tables = [
4140 {"instance_actions": db_instance_action},
4141 {"vim_actions": db_vim_actions}
4142 ]
4143
4144 logger.debug("delete_instance done DB tables: %s",
4145 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4146 mydb.new_rows(db_tables, ())
4147 for myvim_thread_id in vimthread_affected.keys():
4148 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
4149
tiernob3d36742017-03-03 23:51:05 +01004150 if len(error_msg) > 0:
tierno868220c2017-09-26 00:11:05 +02004151 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4152 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
tierno7edb6752016-03-21 17:37:52 +01004153 else:
tierno868220c2017-09-26 00:11:05 +02004154 return "action_id={} instance {} deleted".format(instance_action_id, message)
tierno7edb6752016-03-21 17:37:52 +01004155
tierno7f426e92018-06-28 15:21:32 +02004156def get_instance_id(mydb, tenant_id, instance_id):
4157 global ovim
4158 #check valid tenant_id
4159 check_tenant(mydb, tenant_id)
4160 #obtain data
4161
4162 instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True)
4163 for net in instance_dict["nets"]:
4164 if net.get("sdn_net_id"):
4165 net_sdn = ovim.show_network(net["sdn_net_id"])
4166 net["sdn_info"] = {
4167 "admin_state_up": net_sdn.get("admin_state_up"),
4168 "flows": net_sdn.get("flows"),
4169 "last_error": net_sdn.get("last_error"),
4170 "ports": net_sdn.get("ports"),
4171 "type": net_sdn.get("type"),
4172 "status": net_sdn.get("status"),
4173 "vlan": net_sdn.get("vlan"),
4174 }
4175 return instance_dict
tiernob3d36742017-03-03 23:51:05 +01004176
tiernob8569aa2018-08-24 11:34:54 +02004177@deprecated("Instance is automatically refreshed by vim_threads")
tierno7edb6752016-03-21 17:37:52 +01004178def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
4179 '''Refreshes a scenario instance. It modifies instanceDict'''
4180 '''Returns:
4181 - 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
4182 - error_msg
4183 '''
tierno867ffe92017-03-27 12:50:34 +02004184 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4185 # #print "nfvo.refresh_instance begins"
4186 # #print json.dumps(instanceDict, indent=4)
4187 #
4188 # #print "Getting the VIM URL and the VIM tenant_id"
4189 # myvims={}
4190 #
4191 # # 1. Getting VIM vm and net list
4192 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4193 # vms_notupdated=[]
4194 # vm_list = {}
4195 # for sce_vnf in instanceDict['vnfs']:
4196 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4197 # if datacenter_key not in vm_list:
4198 # vm_list[datacenter_key] = []
4199 # if datacenter_key not in myvims:
4200 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4201 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4202 # if len(vims) == 0:
4203 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4204 # myvims[datacenter_key] = None
4205 # else:
4206 # myvims[datacenter_key] = vims.values()[0]
4207 # for vm in sce_vnf['vms']:
4208 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4209 # vms_notupdated.append(vm["uuid"])
4210 #
4211 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4212 # nets_notupdated=[]
4213 # net_list = {}
4214 # for net in instanceDict['nets']:
4215 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4216 # if datacenter_key not in net_list:
4217 # net_list[datacenter_key] = []
4218 # if datacenter_key not in myvims:
4219 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4220 # datacenter_tenant_id=net["datacenter_tenant_id"])
4221 # if len(vims) == 0:
4222 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4223 # myvims[datacenter_key] = None
4224 # else:
4225 # myvims[datacenter_key] = vims.values()[0]
4226 #
4227 # net_list[datacenter_key].append(net['vim_net_id'])
4228 # nets_notupdated.append(net["uuid"])
4229 #
4230 # # 1. Getting the status of all VMs
4231 # vm_dict={}
4232 # for datacenter_key in myvims:
4233 # if not vm_list.get(datacenter_key):
4234 # continue
4235 # failed = True
4236 # failed_message=""
4237 # if not myvims[datacenter_key]:
4238 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4239 # else:
4240 # try:
4241 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4242 # failed = False
4243 # except vimconn.vimconnException as e:
4244 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4245 # failed_message = str(e)
4246 # if failed:
4247 # for vm in vm_list[datacenter_key]:
4248 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4249 #
4250 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4251 # for sce_vnf in instanceDict['vnfs']:
4252 # for vm in sce_vnf['vms']:
4253 # vm_id = vm['vim_vm_id']
4254 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4255 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4256 # has_mgmt_iface = False
4257 # for iface in vm["interfaces"]:
4258 # if iface["type"]=="mgmt":
4259 # has_mgmt_iface = True
4260 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4261 # vm_dict[vm_id]['status'] = "ACTIVE"
4262 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4263 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4264 # 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'):
4265 # vm['status'] = vm_dict[vm_id]['status']
4266 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4267 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4268 # # 2.1. Update in openmano DB the VMs whose status changed
4269 # try:
4270 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4271 # vms_notupdated.remove(vm["uuid"])
4272 # if updates>0:
4273 # vms_updated.append(vm["uuid"])
4274 # except db_base_Exception as e:
4275 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4276 # # 2.2. Update in openmano DB the interface VMs
4277 # for interface in interfaces:
4278 # #translate from vim_net_id to instance_net_id
4279 # network_id_list=[]
4280 # for net in instanceDict['nets']:
4281 # if net["vim_net_id"] == interface["vim_net_id"]:
4282 # network_id_list.append(net["uuid"])
4283 # if not network_id_list:
4284 # continue
4285 # del interface["vim_net_id"]
4286 # try:
4287 # for network_id in network_id_list:
4288 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4289 # except db_base_Exception as e:
4290 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4291 #
4292 # # 3. Getting the status of all nets
4293 # net_dict = {}
4294 # for datacenter_key in myvims:
4295 # if not net_list.get(datacenter_key):
4296 # continue
4297 # failed = True
4298 # failed_message = ""
4299 # if not myvims[datacenter_key]:
4300 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4301 # else:
4302 # try:
4303 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4304 # failed = False
4305 # except vimconn.vimconnException as e:
4306 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4307 # failed_message = str(e)
4308 # if failed:
4309 # for net in net_list[datacenter_key]:
4310 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4311 #
4312 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4313 # # TODO: update nets inside a vnf
4314 # for net in instanceDict['nets']:
4315 # net_id = net['vim_net_id']
4316 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4317 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4318 # 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'):
4319 # net['status'] = net_dict[net_id]['status']
4320 # net['error_msg'] = net_dict[net_id].get('error_msg')
4321 # net['vim_info'] = net_dict[net_id].get('vim_info')
4322 # # 5.1. Update in openmano DB the nets whose status changed
4323 # try:
4324 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4325 # nets_notupdated.remove(net["uuid"])
4326 # if updated>0:
4327 # nets_updated.append(net["uuid"])
4328 # except db_base_Exception as e:
4329 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4330 #
4331 # # Returns appropriate output
4332 # #print "nfvo.refresh_instance finishes"
4333 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4334 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01004335 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02004336 # if len(vms_notupdated)+len(nets_notupdated)>0:
4337 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4338 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
tierno42026a02017-02-10 15:13:40 +01004339
tiernoae4a8d12016-07-08 12:30:39 +02004340 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01004341
4342def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02004343 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02004344 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01004345 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4346
tiernoae4a8d12016-07-08 12:30:39 +02004347 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02004348 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
4349 if len(vims) == 0:
4350 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004351 myvim = vims.values()[0]
tiernofc5f80b2018-05-29 16:00:43 +02004352 vm_result = {}
4353 vm_error = 0
4354 vm_ok = 0
tierno42026a02017-02-10 15:13:40 +01004355
tiernofc5f80b2018-05-29 16:00:43 +02004356 myvim_threads_id = {}
4357 if action_dict.get("vdu-scaling"):
4358 db_instance_vms = []
4359 db_vim_actions = []
4360 db_instance_interfaces = []
4361 instance_action_id = get_task_id()
4362 db_instance_action = {
4363 "uuid": instance_action_id, # same uuid for the instance and the action on create
4364 "tenant_id": nfvo_tenant,
4365 "instance_id": instance_id,
4366 "description": "SCALE",
4367 }
4368 vm_result["instance_action_id"] = instance_action_id
tierno67881db2018-10-24 18:46:03 +02004369 vm_result["created"] = []
4370 vm_result["deleted"] = []
tiernofc5f80b2018-05-29 16:00:43 +02004371 task_index = 0
4372 for vdu in action_dict["vdu-scaling"]:
tierno868220c2017-09-26 00:11:05 +02004373 vdu_id = vdu.get("vdu-id")
tiernofc5f80b2018-05-29 16:00:43 +02004374 osm_vdu_id = vdu.get("osm_vdu_id")
4375 member_vnf_index = vdu.get("member-vnf-index")
tierno868220c2017-09-26 00:11:05 +02004376 vdu_count = vdu.get("count", 1)
tiernofc5f80b2018-05-29 16:00:43 +02004377 if vdu_id:
tierno67881db2018-10-24 18:46:03 +02004378 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004379 FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4380 WHERE={"vms.uuid": vdu_id},
4381 ORDER_BY="vms.created_at"
4382 )
tierno67881db2018-10-24 18:46:03 +02004383 if not target_vms:
tiernofc5f80b2018-05-29 16:00:43 +02004384 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), HTTP_Not_Found)
4385 else:
4386 if not osm_vdu_id and not member_vnf_index:
tiernoa43bd9e2018-11-26 09:28:58 +00004387 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
tierno67881db2018-10-24 18:46:03 +02004388 target_vms = mydb.get_rows(
tiernofc5f80b2018-05-29 16:00:43 +02004389 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4390 FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4391 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4392 " join vms on ivms.vm_id=vms.uuid",
tiernoa43bd9e2018-11-26 09:28:58 +00004393 WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index,
4394 "ivnfs.instance_scenario_id": instance_id},
tiernofc5f80b2018-05-29 16:00:43 +02004395 ORDER_BY="ivms.created_at"
4396 )
tierno67881db2018-10-24 18:46:03 +02004397 if not target_vms:
tiernofc5f80b2018-05-29 16:00:43 +02004398 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), HTTP_Not_Found)
tierno67881db2018-10-24 18:46:03 +02004399 vdu_id = target_vms[-1]["uuid"]
4400 target_vm = target_vms[-1]
tiernofc5f80b2018-05-29 16:00:43 +02004401 datacenter = target_vm["datacenter_id"]
4402 myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter)
tiernofc5f80b2018-05-29 16:00:43 +02004403
tierno67881db2018-10-24 18:46:03 +02004404 if vdu["type"] == "delete":
4405 for index in range(0, vdu_count):
4406 target_vm = target_vms[-1-index]
4407 vdu_id = target_vm["uuid"]
4408 # look for nm
4409 vm_interfaces = None
4410 for sce_vnf in instanceDict['vnfs']:
4411 for vm in sce_vnf['vms']:
4412 if vm["uuid"] == vdu_id:
4413 vm_interfaces = vm["interfaces"]
4414 break
4415
4416 db_vim_action = {
4417 "instance_action_id": instance_action_id,
4418 "task_index": task_index,
4419 "datacenter_vim_id": target_vm["datacenter_tenant_id"],
4420 "action": "DELETE",
4421 "status": "SCHEDULED",
4422 "item": "instance_vms",
4423 "item_id": vdu_id,
4424 "extra": yaml.safe_dump({"params": vm_interfaces},
4425 default_flow_style=True, width=256)
4426 }
4427 task_index += 1
4428 db_vim_actions.append(db_vim_action)
4429 vm_result["deleted"].append(vdu_id)
4430 # delete from database
4431 db_instance_vms.append({"TO-DELETE": vdu_id})
tiernofc5f80b2018-05-29 16:00:43 +02004432
4433 else: # vdu["type"] == "create":
4434 iface2iface = {}
4435 where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"}
4436
4437 vim_action_to_clone = mydb.get_rows(FROM="vim_actions", WHERE=where)
4438 if not vim_action_to_clone:
4439 raise NfvoException("Cannot find the vim_action at database with {}".format(where), HTTP_Internal_Server_Error)
4440 vim_action_to_clone = vim_action_to_clone[0]
4441 extra = yaml.safe_load(vim_action_to_clone["extra"])
4442
4443 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4444 # TODO do the same for flavor and image when available
4445 task_depends_on = []
4446 task_params = extra["params"]
4447 task_params_networks = deepcopy(task_params[5])
4448 for iface in task_params[5]:
4449 if iface["net_id"].startswith("TASK-"):
4450 if "." not in iface["net_id"]:
4451 task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"],
4452 iface["net_id"][5:]))
4453 iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"],
4454 iface["net_id"][5:])
4455 else:
4456 task_depends_on.append(iface["net_id"][5:])
4457 if "mac_address" in iface:
4458 del iface["mac_address"]
4459
4460 vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]})
4461 for index in range(0, vdu_count):
4462 vm_uuid = str(uuid4())
4463 vm_name = target_vm.get('vim_name')
4464 try:
4465 suffix = vm_name.rfind("-")
tierno67881db2018-10-24 18:46:03 +02004466 vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:]))
tiernofc5f80b2018-05-29 16:00:43 +02004467 except Exception:
4468 pass
4469 db_instance_vm = {
4470 "uuid": vm_uuid,
4471 'instance_vnf_id': target_vm['instance_vnf_id'],
4472 'vm_id': target_vm['vm_id'],
4473 'vim_name': vm_name
4474 }
4475 db_instance_vms.append(db_instance_vm)
4476
4477 for vm_iface in vm_ifaces_to_clone:
4478 iface_uuid = str(uuid4())
4479 iface2iface[vm_iface["uuid"]] = iface_uuid
4480 db_vm_iface = {
4481 "uuid": iface_uuid,
4482 'instance_vm_id': vm_uuid,
4483 "instance_net_id": vm_iface["instance_net_id"],
4484 'interface_id': vm_iface['interface_id'],
4485 'type': vm_iface['type'],
4486 'floating_ip': vm_iface['floating_ip'],
4487 'port_security': vm_iface['port_security']
4488 }
4489 db_instance_interfaces.append(db_vm_iface)
4490 task_params_copy = deepcopy(task_params)
4491 for iface in task_params_copy[5]:
4492 iface["uuid"] = iface2iface[iface["uuid"]]
4493 # increment ip_address
4494 if "ip_address" in iface:
4495 ip = iface.get("ip_address")
4496 i = ip.rfind(".")
4497 if i > 0:
4498 try:
4499 i += 1
4500 ip = ip[i:] + str(int(ip[:i]) + 1)
4501 iface["ip_address"] = ip
4502 except:
4503 iface["ip_address"] = None
4504 if vm_name:
4505 task_params_copy[0] = vm_name
4506 db_vim_action = {
4507 "instance_action_id": instance_action_id,
4508 "task_index": task_index,
4509 "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"],
4510 "action": "CREATE",
4511 "status": "SCHEDULED",
4512 "item": "instance_vms",
4513 "item_id": vm_uuid,
4514 # ALF
4515 # ALF
4516 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4517 # ALF
4518 # ALF
4519 "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256)
4520 }
4521 task_index += 1
4522 db_vim_actions.append(db_vim_action)
tierno67881db2018-10-24 18:46:03 +02004523 vm_result["created"].append(vm_uuid)
tiernofc5f80b2018-05-29 16:00:43 +02004524
4525 db_instance_action["number_tasks"] = task_index
4526 db_tables = [
4527 {"instance_vms": db_instance_vms},
4528 {"instance_interfaces": db_instance_interfaces},
4529 {"instance_actions": db_instance_action},
4530 # TODO revise sfps
4531 # {"instance_sfis": db_instance_sfis},
4532 # {"instance_sfs": db_instance_sfs},
4533 # {"instance_classifications": db_instance_classifications},
4534 # {"instance_sfps": db_instance_sfps},
4535 {"vim_actions": db_vim_actions}
4536 ]
4537 logger.debug("create_vdu done DB tables: %s",
4538 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4539 mydb.new_rows(db_tables, [])
4540 for myvim_thread in myvim_threads_id.values():
4541 vim_threads["running"][myvim_thread].insert_task(db_vim_actions)
4542
4543 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004544
4545 input_vnfs = action_dict.pop("vnfs", [])
4546 input_vms = action_dict.pop("vms", [])
tierno92c36fd2018-05-04 12:21:10 +02004547 action_over_all = True if not input_vnfs and not input_vms else False
tierno7edb6752016-03-21 17:37:52 +01004548 for sce_vnf in instanceDict['vnfs']:
4549 for vm in sce_vnf['vms']:
tierno92c36fd2018-05-04 12:21:10 +02004550 if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
4551 sce_vnf['member_vnf_index'] not in input_vnfs and \
4552 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
4553 continue
tiernoae4a8d12016-07-08 12:30:39 +02004554 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004555 if "add_public_key" in action_dict:
4556 mgmt_access = {}
4557 if sce_vnf.get('mgmt_access'):
4558 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
4559 ssh_access = mgmt_access['config-access']['ssh-access']
4560 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
tierno42026a02017-02-10 15:13:40 +01004561 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02004562 if ssh_access['required'] and ssh_access['default-user']:
4563 if 'ip_address' in vm:
4564 mgmt_ip = vm['ip_address'].split(';')
4565 password = mgmt_access['config-access'].get('password')
4566 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
4567 myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'],
4568 action_dict['add_public_key'],
4569 password=password, ro_key=priv_RO_key)
4570 else:
4571 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4572 HTTP_Internal_Server_Error)
4573 except KeyError:
4574 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4575 HTTP_Internal_Server_Error)
4576 else:
4577 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4578 HTTP_Internal_Server_Error)
4579 else:
4580 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
4581 if "console" in action_dict:
4582 if not global_config["http_console_proxy"]:
tierno20fc2a22016-08-19 17:02:35 +02004583 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4584 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4585 protocol=data["protocol"],
gcalvinoe580c7d2017-09-22 14:09:51 +02004586 ip = data["server"],
4587 port = data["port"],
tierno20fc2a22016-08-19 17:02:35 +02004588 suffix = data["suffix"]),
4589 "name":vm['name']
4590 }
4591 vm_ok +=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004592 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
4593 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
4594 "description": "this console is only reachable by local interface",
4595 "name":vm['name']
4596 }
tierno20fc2a22016-08-19 17:02:35 +02004597 vm_error+=1
gcalvinoe580c7d2017-09-22 14:09:51 +02004598 else:
4599 #print "console data", data
4600 try:
4601 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
4602 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4603 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4604 protocol=data["protocol"],
4605 ip = global_config["http_console_host"],
4606 port = console_thread.port,
4607 suffix = data["suffix"]),
4608 "name":vm['name']
4609 }
4610 vm_ok +=1
4611 except NfvoException as e:
4612 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4613 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02004614
gcalvinoe580c7d2017-09-22 14:09:51 +02004615 else:
4616 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
4617 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02004618 except vimconn.vimconnException as e:
4619 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4620 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01004621
4622 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02004623 return vm_result
tierno7edb6752016-03-21 17:37:52 +01004624 else:
tierno351863c2016-07-23 01:46:03 +02004625 return vm_result
tierno42026a02017-02-10 15:13:40 +01004626
tierno868220c2017-09-26 00:11:05 +02004627def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
tierno16e3dd42018-04-24 12:52:40 +02004628 filter = {}
tierno868220c2017-09-26 00:11:05 +02004629 if nfvo_tenant and nfvo_tenant != "any":
4630 filter["tenant_id"] = nfvo_tenant
4631 if instance_id and instance_id != "any":
4632 filter["instance_id"] = instance_id
4633 if action_id:
4634 filter["uuid"] = action_id
4635 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
tierno16e3dd42018-04-24 12:52:40 +02004636 if action_id:
4637 if not rows:
4638 raise NfvoException("Not found any action with this criteria", HTTP_Not_Found)
4639 vim_actions = mydb.get_rows(FROM="vim_actions", WHERE={"instance_action_id": action_id})
4640 rows[0]["vim_actions"] = vim_actions
tiernofc5f80b2018-05-29 16:00:43 +02004641 return {"actions": rows}
tierno868220c2017-09-26 00:11:05 +02004642
tiernob3d36742017-03-03 23:51:05 +01004643
tierno7edb6752016-03-21 17:37:52 +01004644def create_or_use_console_proxy_thread(console_server, console_port):
4645 #look for a non-used port
4646 console_thread_key = console_server + ":" + str(console_port)
4647 if console_thread_key in global_config["console_thread"]:
4648 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02004649 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01004650
tierno7edb6752016-03-21 17:37:52 +01004651 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02004652 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01004653 if port in global_config["console_ports"]:
4654 continue
4655 try:
4656 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
4657 clithread.start()
4658 global_config["console_thread"][console_thread_key] = clithread
4659 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02004660 return clithread
tierno7edb6752016-03-21 17:37:52 +01004661 except cli.ConsoleProxyExceptionPortUsed as e:
4662 #port used, try with onoher
4663 continue
4664 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02004665 raise NfvoException(str(e), HTTP_Bad_Request)
4666 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01004667
tiernob3d36742017-03-03 23:51:05 +01004668
tierno7edb6752016-03-21 17:37:52 +01004669def check_tenant(mydb, tenant_id):
4670 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02004671 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
4672 if not tenant:
4673 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
4674 return
tierno7edb6752016-03-21 17:37:52 +01004675
4676def new_tenant(mydb, tenant_dict):
tierno7edb6752016-03-21 17:37:52 +01004677
gcalvinoe580c7d2017-09-22 14:09:51 +02004678 tenant_uuid = str(uuid4())
4679 tenant_dict['uuid'] = tenant_uuid
4680 try:
4681 pub_key, priv_key = create_RO_keypair(tenant_uuid)
4682 tenant_dict['RO_pub_key'] = pub_key
4683 tenant_dict['encrypted_RO_priv_key'] = priv_key
gcalvinoc62cfa52017-10-05 18:21:25 +02004684 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
gcalvinoe580c7d2017-09-22 14:09:51 +02004685 except db_base_Exception as e:
tierno9c5c8322018-03-23 15:44:03 +01004686 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), e.http_code)
gcalvinoe580c7d2017-09-22 14:09:51 +02004687 return tenant_uuid
tiernob3d36742017-03-03 23:51:05 +01004688
tierno7edb6752016-03-21 17:37:52 +01004689def delete_tenant(mydb, tenant):
4690 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01004691
tiernof97fd272016-07-11 14:32:37 +02004692 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
4693 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
4694 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01004695
tiernob3d36742017-03-03 23:51:05 +01004696
tierno7edb6752016-03-21 17:37:52 +01004697def new_datacenter(mydb, datacenter_descriptor):
tierno1c848c02018-05-21 16:40:33 +02004698 sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004699 if "config" in datacenter_descriptor:
tiernoedf3f4f2018-05-17 23:02:47 +02004700 sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None)
4701 datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True,
4702 width=256)
4703 # Check that datacenter-type is correct
tierno3ae39742016-09-07 12:17:51 +02004704 datacenter_type = datacenter_descriptor.get("type", "openvim");
tiernoedf3f4f2018-05-17 23:02:47 +02004705 # module_info = None
tierno3ae39742016-09-07 12:17:51 +02004706 try:
4707 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02004708 pkg = __import__("osm_ro." + module)
tiernoedf3f4f2018-05-17 23:02:47 +02004709 # vim_conn = getattr(pkg, module)
tierno361275f2017-04-25 16:24:34 +02004710 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02004711 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02004712 # if module_info and module_info[0]:
4713 # file.close(module_info[0])
tiernoedf3f4f2018-05-17 23:02:47 +02004714 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type,
4715 module),
4716 HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01004717
gcalvinoc62cfa52017-10-05 18:21:25 +02004718 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
tiernoedf3f4f2018-05-17 23:02:47 +02004719 if sdn_port_mapping:
4720 try:
4721 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping)
4722 except Exception as e:
4723 mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback
4724 raise e
tiernof97fd272016-07-11 14:32:37 +02004725 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004726
tiernob3d36742017-03-03 23:51:05 +01004727
tierno7edb6752016-03-21 17:37:52 +01004728def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
tierno8fe7a492017-07-11 13:50:04 +02004729 # obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02004730 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno8fe7a492017-07-11 13:50:04 +02004731
4732 # edit data
tiernof97fd272016-07-11 14:32:37 +02004733 datacenter_id = datacenter['uuid']
tiernod72182f2018-08-29 10:56:13 +02004734 where = {'uuid': datacenter['uuid']}
tierno8fe7a492017-07-11 13:50:04 +02004735 remove_port_mapping = False
tiernoedf3f4f2018-05-17 23:02:47 +02004736 new_sdn_port_mapping = None
tierno7edb6752016-03-21 17:37:52 +01004737 if "config" in datacenter_descriptor:
tierno8fe7a492017-07-11 13:50:04 +02004738 if datacenter_descriptor['config'] != None:
tierno7edb6752016-03-21 17:37:52 +01004739 try:
4740 new_config_dict = datacenter_descriptor["config"]
tiernoedf3f4f2018-05-17 23:02:47 +02004741 if "sdn-port-mapping" in new_config_dict:
4742 remove_port_mapping = True
4743 new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping")
tiernod72182f2018-08-29 10:56:13 +02004744 # delete null fields
4745 to_delete = []
tierno7edb6752016-03-21 17:37:52 +01004746 for k in new_config_dict:
tiernod72182f2018-08-29 10:56:13 +02004747 if new_config_dict[k] is None:
tierno7edb6752016-03-21 17:37:52 +01004748 to_delete.append(k)
tierno8fe7a492017-07-11 13:50:04 +02004749 if k == 'sdn-controller':
4750 remove_port_mapping = True
tierno42026a02017-02-10 15:13:40 +01004751
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004752 config_text = datacenter.get("config")
4753 if not config_text:
4754 config_text = '{}'
4755 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01004756 config_dict.update(new_config_dict)
tiernod72182f2018-08-29 10:56:13 +02004757 # delete null fields
tierno7edb6752016-03-21 17:37:52 +01004758 for k in to_delete:
4759 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02004760 except Exception as e:
4761 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno8fe7a492017-07-11 13:50:04 +02004762 if config_dict:
4763 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
4764 else:
4765 datacenter_descriptor["config"] = None
4766 if remove_port_mapping:
4767 try:
4768 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
4769 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02004770 raise NfvoException("Error deleting datacenter-port-mapping " + str(e), HTTP_Conflict)
tierno8fe7a492017-07-11 13:50:04 +02004771
tiernof97fd272016-07-11 14:32:37 +02004772 mydb.update_rows('datacenters', datacenter_descriptor, where)
tiernoedf3f4f2018-05-17 23:02:47 +02004773 if new_sdn_port_mapping:
4774 try:
4775 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping)
4776 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02004777 # Rollback
4778 mydb.update_rows('datacenters', datacenter, where)
4779 raise NfvoException("Error adding datacenter-port-mapping " + str(e), HTTP_Conflict)
tiernof97fd272016-07-11 14:32:37 +02004780 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01004781
tiernob3d36742017-03-03 23:51:05 +01004782
tierno7edb6752016-03-21 17:37:52 +01004783def delete_datacenter(mydb, datacenter):
4784 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02004785 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
4786 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
tierno8fe7a492017-07-11 13:50:04 +02004787 try:
4788 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
4789 except ovimException as e:
tiernod72182f2018-08-29 10:56:13 +02004790 raise NfvoException("Error deleting datacenter-port-mapping " + str(e))
tiernof97fd272016-07-11 14:32:37 +02004791 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01004792
tiernob3d36742017-03-03 23:51:05 +01004793
tiernod3750b32018-07-20 15:33:08 +02004794def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None,
4795 vim_username=None, vim_password=None, config=None):
tierno9c22f2d2017-10-09 16:23:55 +02004796 # get datacenter info
tierno0ea2a7e2017-10-18 00:06:26 +02004797 try:
tiernod3750b32018-07-20 15:33:08 +02004798 if not datacenter_id:
4799 if not vim_id:
4800 raise NfvoException("You must provide 'vim_id", http_code=HTTP_Bad_Request)
4801 datacenter_id = vim_id
4802 datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id)
tierno7edb6752016-03-21 17:37:52 +01004803
tiernod3750b32018-07-20 15:33:08 +02004804 create_vim_tenant = True if not vim_tenant and not vim_tenant_name else False
tierno42026a02017-02-10 15:13:40 +01004805
tierno0ea2a7e2017-10-18 00:06:26 +02004806 # get nfvo_tenant info
4807 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
4808 if vim_tenant_name==None:
4809 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01004810
tierno0ea2a7e2017-10-18 00:06:26 +02004811 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernod3750b32018-07-20 15:33:08 +02004812 # #check that this association does not exist before
4813 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4814 # if len(tenants_datacenters)>0:
4815 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01004816
tierno0ea2a7e2017-10-18 00:06:26 +02004817 vim_tenant_id_exist_atdb=False
4818 if not create_vim_tenant:
4819 where_={"datacenter_id": datacenter_id}
tiernod3750b32018-07-20 15:33:08 +02004820 if vim_tenant!=None:
4821 where_["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02004822 if vim_tenant_name!=None:
4823 where_["vim_tenant_name"] = vim_tenant_name
4824 #check if vim_tenant_id is already at database
4825 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
4826 if len(datacenter_tenants_dict)>=1:
4827 datacenter_tenants_dict = datacenter_tenants_dict[0]
4828 vim_tenant_id_exist_atdb=True
4829 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
4830 else: #result=0
4831 datacenter_tenants_dict = {}
4832 #insert at table datacenter_tenants
tiernod3750b32018-07-20 15:33:08 +02004833 else: #if vim_tenant==None:
tierno0ea2a7e2017-10-18 00:06:26 +02004834 #create tenant at VIM if not provided
4835 try:
4836 _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username,
4837 vim_passwd=vim_password)
4838 datacenter_name = myvim["name"]
tiernod3750b32018-07-20 15:33:08 +02004839 vim_tenant = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
tierno0ea2a7e2017-10-18 00:06:26 +02004840 except vimconn.vimconnException as e:
tiernod3750b32018-07-20 15:33:08 +02004841 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant, str(e)), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01004842 datacenter_tenants_dict = {}
tierno0ea2a7e2017-10-18 00:06:26 +02004843 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01004844
tierno0ea2a7e2017-10-18 00:06:26 +02004845 #fill datacenter_tenants table
4846 if not vim_tenant_id_exist_atdb:
tiernod3750b32018-07-20 15:33:08 +02004847 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant
tierno0ea2a7e2017-10-18 00:06:26 +02004848 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
4849 datacenter_tenants_dict["user"] = vim_username
4850 datacenter_tenants_dict["passwd"] = vim_password
4851 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tiernod3750b32018-07-20 15:33:08 +02004852 if name:
4853 datacenter_tenants_dict["name"] = name
4854 else:
4855 datacenter_tenants_dict["name"] = datacenter_name
tierno0ea2a7e2017-10-18 00:06:26 +02004856 if config:
4857 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
4858 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
4859 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01004860
tierno0ea2a7e2017-10-18 00:06:26 +02004861 #fill tenants_datacenters table
4862 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
4863 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
4864 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
tiernod3750b32018-07-20 15:33:08 +02004865
tierno0ea2a7e2017-10-18 00:06:26 +02004866 # create thread
tierno0ea2a7e2017-10-18 00:06:26 +02004867 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
tiernod3750b32018-07-20 15:33:08 +02004868 new_thread = vim_thread.vim_thread(task_lock, thread_name, datacenter_name, datacenter_tenant_id,
tierno0ea2a7e2017-10-18 00:06:26 +02004869 db=db, db_lock=db_lock, ovim=ovim)
4870 new_thread.start()
4871 thread_id = datacenter_tenants_dict["uuid"]
4872 vim_threads["running"][thread_id] = new_thread
tiernod3750b32018-07-20 15:33:08 +02004873 return thread_id
tierno0ea2a7e2017-10-18 00:06:26 +02004874 except vimconn.vimconnException as e:
4875 raise NfvoException(str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01004876
tierno99314902017-04-26 13:23:09 +02004877
tiernod3750b32018-07-20 15:33:08 +02004878def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None,
4879 vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004880
tiernod3750b32018-07-20 15:33:08 +02004881 # get vim_account; check is valid for this tenant
4882 from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
4883 where_ = {"td.nfvo_tenant_id": nfvo_tenant}
4884 if datacenter_tenant_id:
4885 where_["dt.uuid"] = datacenter_tenant_id
4886 if datacenter_id:
4887 where_["dt.datacenter_id"] = datacenter_id
4888 vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_)
4889 if not vim_accounts:
4890 raise NfvoException("vim_account not found for this tenant", http_code=HTTP_Not_Found)
4891 elif len(vim_accounts) > 1:
4892 raise NfvoException("found more than one vim_account for this tenant", http_code=HTTP_Conflict)
4893 datacenter_tenant_id = vim_accounts[0]["uuid"]
4894 original_config = vim_accounts[0]["config"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004895
tiernod3750b32018-07-20 15:33:08 +02004896 update_ = {}
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004897 if config:
tiernod3750b32018-07-20 15:33:08 +02004898 original_config_dict = yaml.load(original_config)
4899 original_config_dict.update(config)
4900 update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256)
4901 if name:
4902 update_['name'] = name
4903 if vim_tenant:
4904 update_['vim_tenant_id'] = vim_tenant
4905 if vim_tenant_name:
4906 update_['vim_tenant_name'] = vim_tenant_name
4907 if vim_username:
4908 update_['user'] = vim_username
4909 if vim_password:
4910 update_['passwd'] = vim_password
4911 if update_:
4912 mydb.update_rows("datacenter_tenants", UPDATE=update_, WHERE={"uuid": datacenter_tenant_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004913
tiernod3750b32018-07-20 15:33:08 +02004914 vim_threads["running"][datacenter_tenant_id].insert_task("reload")
4915 return datacenter_tenant_id
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004916
tiernod3750b32018-07-20 15:33:08 +02004917def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None):
tierno7edb6752016-03-21 17:37:52 +01004918 #get nfvo_tenant info
4919 if not tenant_id or tenant_id=="any":
4920 tenant_uuid = None
4921 else:
tiernof97fd272016-07-11 14:32:37 +02004922 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01004923 tenant_uuid = tenant_dict['uuid']
4924
4925 #check that this association exist before
tiernod3750b32018-07-20 15:33:08 +02004926 tenants_datacenter_dict = {}
4927 if datacenter:
4928 datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter)
4929 tenants_datacenter_dict["datacenter_id"] = datacenter_id
4930 elif vim_account_id:
4931 tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id
tierno7edb6752016-03-21 17:37:52 +01004932 if tenant_uuid:
4933 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02004934 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4935 if len(tenant_datacenter_list)==0 and tenant_uuid:
4936 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004937
4938 #delete this association
tiernof97fd272016-07-11 14:32:37 +02004939 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01004940
4941 #get vim_tenant info and deletes
4942 warning=''
4943 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02004944 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
4945 #try to delete vim:tenant
4946 try:
4947 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
4948 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01004949 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01004950 try:
tierno0ea2a7e2017-10-18 00:06:26 +02004951 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02004952 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
4953 except vimconn.vimconnException as e:
4954 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
4955 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02004956 except db_base_Exception as e:
4957 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01004958 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02004959 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tiernoa3572692018-05-14 13:09:33 +02004960 thread = vim_threads["running"].get(thread_id)
4961 if thread:
4962 thread.insert_task("exit")
4963 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02004964 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01004965
tiernob3d36742017-03-03 23:51:05 +01004966
tierno7edb6752016-03-21 17:37:52 +01004967def datacenter_action(mydb, tenant_id, datacenter, action_dict):
4968 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01004969 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004970 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004971
4972 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02004973 try:
tiernof97fd272016-07-11 14:32:37 +02004974 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02004975 #print content
4976 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02004977 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
4978 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01004979 #update nets Change from VIM format to NFVO format
4980 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02004981 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01004982 net_nfvo={'datacenter_id': datacenter_id}
4983 net_nfvo['name'] = net['name']
4984 #net_nfvo['description']= net['name']
4985 net_nfvo['vim_net_id'] = net['id']
4986 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4987 net_nfvo['shared'] = net['shared']
4988 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
4989 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02004990 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
4991 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
4992 return inserted
tierno7edb6752016-03-21 17:37:52 +01004993 elif 'net-edit' in action_dict:
4994 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02004995 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01004996 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01004997 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02004998 return result
tierno7edb6752016-03-21 17:37:52 +01004999 elif 'net-delete' in action_dict:
5000 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02005001 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01005002 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01005003 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02005004 return result
tierno7edb6752016-03-21 17:37:52 +01005005
5006 else:
tiernof97fd272016-07-11 14:32:37 +02005007 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01005008
tiernob3d36742017-03-03 23:51:05 +01005009
tierno7edb6752016-03-21 17:37:52 +01005010def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
5011 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005012 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005013
tierno42fcc3b2016-07-06 17:20:40 +02005014 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01005015 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01005016 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02005017 return result
tierno7edb6752016-03-21 17:37:52 +01005018
tiernob3d36742017-03-03 23:51:05 +01005019
tierno7edb6752016-03-21 17:37:52 +01005020def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
5021 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005022 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005023 filter_dict={}
5024 if action_dict:
5025 action_dict = action_dict["netmap"]
5026 if 'vim_id' in action_dict:
5027 filter_dict["id"] = action_dict['vim_id']
5028 if 'vim_name' in action_dict:
5029 filter_dict["name"] = action_dict['vim_name']
5030 else:
5031 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01005032
tiernoae4a8d12016-07-08 12:30:39 +02005033 try:
tiernof97fd272016-07-11 14:32:37 +02005034 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005035 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005036 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
5037 raise NfvoException(str(e), HTTP_Internal_Server_Error)
5038 if len(vim_nets)>1 and action_dict:
5039 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
5040 elif len(vim_nets)==0: # and action_dict:
5041 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01005042 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02005043 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01005044 net_nfvo={'datacenter_id': datacenter_id}
5045 if action_dict and "name" in action_dict:
5046 net_nfvo['name'] = action_dict['name']
5047 else:
5048 net_nfvo['name'] = net['name']
5049 #net_nfvo['description']= net['name']
5050 net_nfvo['vim_net_id'] = net['id']
5051 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5052 net_nfvo['shared'] = net['shared']
5053 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02005054 try:
5055 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01005056 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02005057 net_nfvo["uuid"] = net_id
5058 except db_base_Exception as e:
5059 if action_dict:
5060 raise
5061 else:
5062 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01005063 net_list.append(net_nfvo)
5064 return net_list
tierno7edb6752016-03-21 17:37:52 +01005065
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005066def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
5067 # obtain all network data
5068 try:
5069 if utils.check_valid_uuid(network_id):
5070 filter_dict = {"id": network_id}
5071 else:
5072 filter_dict = {"name": network_id}
5073
5074 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5075 network = myvim.get_network_list(filter_dict=filter_dict)
5076 except vimconn.vimconnException as e:
tiernof1ba57e2017-09-07 12:23:19 +02005077 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e)), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005078
5079 # ensure the network is defined
5080 if len(network) == 0:
5081 raise NfvoException("Network {} is not present in the system".format(network_id),
5082 HTTP_Bad_Request)
5083
5084 # ensure there is only one network with the provided name
5085 if len(network) > 1:
5086 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request)
5087
5088 # ensure it is a dataplane network
5089 if network[0]['type'] != 'data':
5090 return None
5091
5092 # ensure we use the id
5093 network_id = network[0]['id']
5094
5095 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5096 # and with instance_scenario_id==NULL
5097 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5098 search_dict = {'vim_net_id': network_id}
5099
5100 try:
5101 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5102 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
5103 except db_base_Exception as e:
5104 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005105 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005106
5107 sdn_net_counter = 0
5108 for net in result:
5109 if net['sdn_net_id'] != None:
5110 sdn_net_counter+=1
5111 sdn_net_id = net['sdn_net_id']
5112
5113 if sdn_net_counter == 0:
5114 return None
5115 elif sdn_net_counter == 1:
5116 return sdn_net_id
5117 else:
5118 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5119 network_id), HTTP_Internal_Server_Error)
5120
5121def get_sdn_controller_id(mydb, datacenter):
5122 # Obtain sdn controller id
5123 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
5124 if not config:
5125 return None
5126
5127 return yaml.load(config).get('sdn-controller')
5128
5129def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
5130 try:
5131 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5132 if not sdn_network_id:
5133 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error)
5134
5135 #Obtain sdn controller id
5136 controller_id = get_sdn_controller_id(mydb, datacenter)
5137 if not controller_id:
5138 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error)
5139
5140 #Obtain sdn controller info
5141 sdn_controller = ovim.show_of_controller(controller_id)
5142
5143 port_data = {
5144 'name': 'external_port',
5145 'net_id': sdn_network_id,
5146 'ofc_id': controller_id,
5147 'switch_dpid': sdn_controller['dpid'],
5148 'switch_port': descriptor['port']
5149 }
5150
5151 if 'vlan' in descriptor:
5152 port_data['vlan'] = descriptor['vlan']
5153 if 'mac' in descriptor:
5154 port_data['mac'] = descriptor['mac']
5155
5156 result = ovim.new_port(port_data)
5157 except ovimException as e:
5158 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5159 sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error)
5160 except db_base_Exception as e:
5161 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005162 network_id) + str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005163
5164 return 'Port uuid: '+ result
5165
5166def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
5167 if port_id:
5168 filter = {'uuid': port_id}
5169 else:
5170 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5171 if not sdn_network_id:
5172 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
5173 HTTP_Internal_Server_Error)
5174 #in case no port_id is specified only ports marked as 'external_port' will be detached
5175 filter = {'name': 'external_port', 'net_id': sdn_network_id}
5176
5177 try:
5178 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
5179 except ovimException as e:
5180 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
5181 HTTP_Internal_Server_Error)
5182
5183 if len(port_list) == 0:
5184 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
5185 HTTP_Bad_Request)
5186
5187 port_uuid_list = []
5188 for port in port_list:
5189 try:
5190 port_uuid_list.append(port['uuid'])
5191 ovim.delete_port(port['uuid'])
5192 except ovimException as e:
5193 raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error)
5194
5195 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
tiernob3d36742017-03-03 23:51:05 +01005196
tierno7edb6752016-03-21 17:37:52 +01005197def vim_action_get(mydb, tenant_id, datacenter, item, name):
5198 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005199 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01005200 filter_dict={}
5201 if name:
tierno42fcc3b2016-07-06 17:20:40 +02005202 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01005203 filter_dict["id"] = name
5204 else:
5205 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02005206 try:
5207 if item=="networks":
5208 #filter_dict['tenant_id'] = myvim['tenant_id']
5209 content = myvim.get_network_list(filter_dict=filter_dict)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005210
5211 if len(content) == 0:
5212 raise NfvoException("Network {} is not present in the system. ".format(name),
5213 HTTP_Bad_Request)
5214
5215 #Update the networks with the attached ports
5216 for net in content:
5217 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
5218 if sdn_network_id != None:
5219 try:
5220 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5221 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
5222 except ovimException as e:
5223 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error)
5224 #Remove field name and if port name is external_port save it as 'type'
5225 for port in port_list:
5226 if port['name'] == 'external_port':
5227 port['type'] = "External"
5228 del port['name']
5229 net['sdn_network_id'] = sdn_network_id
5230 net['sdn_attached_ports'] = port_list
5231
tiernoae4a8d12016-07-08 12:30:39 +02005232 elif item=="tenants":
5233 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01005234 elif item == "images":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005235
tierno4540ea52017-01-18 17:44:32 +01005236 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02005237 else:
tiernof97fd272016-07-11 14:32:37 +02005238 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02005239 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02005240 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02005241 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02005242 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02005243 raise NfvoException("No {} found with ".format(item[:-1]) + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())),
tiernobe41e222016-09-02 15:16:13 +02005244 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005245 else:
tiernof97fd272016-07-11 14:32:37 +02005246 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02005247 except vimconn.vimconnException as e:
5248 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02005249 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01005250
tiernob3d36742017-03-03 23:51:05 +01005251
tierno7edb6752016-03-21 17:37:52 +01005252def vim_action_delete(mydb, tenant_id, datacenter, item, name):
5253 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02005254 if tenant_id == "any":
5255 tenant_id=None
5256
tiernoa2793912016-10-04 08:15:08 +00005257 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02005258 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02005259 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
5260 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02005261 items = content.values()[0]
5262 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02005263 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02005264 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02005265 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02005266 else: # it is a dict
5267 item_id = items["id"]
5268 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01005269
tiernoae4a8d12016-07-08 12:30:39 +02005270 try:
5271 if item=="networks":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005272 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5273 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
5274 if sdn_network_id != None:
5275 #Delete any port attachment to this network
5276 try:
5277 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
5278 except ovimException as e:
5279 raise NfvoException(
5280 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
5281 HTTP_Internal_Server_Error)
5282
5283 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5284 for port in port_list:
5285 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
5286
5287 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5288 try:
5289 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
5290 except db_base_Exception as e:
5291 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
tierno9c5c8322018-03-23 15:44:03 +01005292 str(e), e.http_code)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005293
5294 #Delete the SDN network
5295 try:
5296 ovim.delete_network(sdn_network_id)
5297 except ovimException as e:
5298 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
5299 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
5300 HTTP_Internal_Server_Error)
5301
tiernoae4a8d12016-07-08 12:30:39 +02005302 content = myvim.delete_network(item_id)
5303 elif item=="tenants":
5304 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01005305 elif item == "images":
5306 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02005307 else:
tierno42026a02017-02-10 15:13:40 +01005308 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005309 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005310 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5311 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005312
tiernof97fd272016-07-11 14:32:37 +02005313 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01005314
tiernob3d36742017-03-03 23:51:05 +01005315
tierno7edb6752016-03-21 17:37:52 +01005316def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
5317 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00005318 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02005319 if tenant_id == "any":
5320 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00005321 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02005322 try:
5323 if item=="networks":
5324 net = descriptor["network"]
5325 net_name = net.pop("name")
5326 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02005327 net_public = net.pop("shared", False)
5328 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01005329 net_vlan = net.pop("vlan", None)
5330 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005331
5332 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5333 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
tierno00e3df72017-11-29 17:20:13 +01005334 #obtain datacenter_tenant_id
5335 datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',),
5336 FROM='datacenter_tenants',
5337 WHERE={'datacenter_id': datacenter})[0]['uuid']
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005338 try:
5339 sdn_network = {}
5340 sdn_network['vlan'] = net_vlan
5341 sdn_network['type'] = net_type
5342 sdn_network['name'] = net_name
tierno00e3df72017-11-29 17:20:13 +01005343 sdn_network['region'] = datacenter_tenant_id
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005344 ovim_content = ovim.new_network(sdn_network)
5345 except ovimException as e:
tierno00e3df72017-11-29 17:20:13 +01005346 logger.error("ovimException creating SDN network={} ".format(
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005347 sdn_network) + str(e), exc_info=True)
5348 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
5349 HTTP_Internal_Server_Error)
5350
5351 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5352 # use instance_scenario_id=None to distinguish from real instaces of nets
tierno00e3df72017-11-29 17:20:13 +01005353 correspondence = {'instance_scenario_id': None,
5354 'sdn_net_id': ovim_content,
5355 'vim_net_id': content,
5356 'datacenter_tenant_id': datacenter_tenant_id
5357 }
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005358 try:
5359 mydb.new_row('instance_nets', correspondence, add_uuid=True)
5360 except db_base_Exception as e:
tierno00e3df72017-11-29 17:20:13 +01005361 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
tierno9c5c8322018-03-23 15:44:03 +01005362 correspondence, e), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005363 elif item=="tenants":
5364 tenant = descriptor["tenant"]
5365 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
5366 else:
tierno42026a02017-02-10 15:13:40 +01005367 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02005368 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02005369 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02005370
tierno7edb6752016-03-21 17:37:52 +01005371 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005372
5373def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005374 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005375 logger.debug('New SDN controller created with uuid {}'.format(data))
5376 return data
5377
5378def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005379 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005380 msg = 'SDN controller {} updated'.format(data)
5381 logger.debug(msg)
5382 return msg
5383
5384def sdn_controller_list(mydb, tenant_id, controller_id=None):
5385 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005386 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005387 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005388 data = ovim.show_of_controller(controller_id)
5389
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005390 msg = 'SDN controller list:\n {}'.format(data)
5391 logger.debug(msg)
5392 return data
5393
5394def sdn_controller_delete(mydb, tenant_id, controller_id):
5395 select_ = ('uuid', 'config')
5396 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
5397 for datacenter in datacenters:
5398 if datacenter['config']:
5399 config = yaml.load(datacenter['config'])
5400 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
5401 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict)
5402
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005403 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005404 msg = 'SDN controller {} deleted'.format(data)
5405 logger.debug(msg)
5406 return msg
5407
5408def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
5409 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
5410 if len(controller) < 1:
5411 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found)
5412
5413 try:
5414 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
5415 except:
5416 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request)
5417
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005418 sdn_controller = ovim.show_of_controller(sdn_controller_id)
5419 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005420
5421 maps = list()
5422 for compute_node in sdn_port_mapping:
5423 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5424 element = dict()
5425 element["compute_node"] = compute_node["compute_node"]
5426 for port in compute_node["ports"]:
tierno7f426e92018-06-28 15:21:32 +02005427 pci = port.get("pci")
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005428 element["switch_port"] = port.get("switch_port")
5429 element["switch_mac"] = port.get("switch_mac")
tierno7f426e92018-06-28 15:21:32 +02005430 if not pci or not (element["switch_port"] or element["switch_mac"]):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005431 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
5432 " or 'switch_mac'", HTTP_Bad_Request)
tierno7f426e92018-06-28 15:21:32 +02005433 for pci_expanded in utils.expand_brackets(pci):
5434 element["pci"] = pci_expanded
5435 maps.append(dict(element))
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005436
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005437 return ovim.set_of_port_mapping(maps, ofc_id=sdn_controller_id, switch_dpid=switch_dpid, region=datacenter_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005438
5439def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02005440 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005441
5442 result = {
5443 "sdn-controller": None,
5444 "datacenter-id": datacenter_id,
5445 "dpid": None,
5446 "ports_mapping": list()
5447 }
5448
5449 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
5450 if datacenter['config']:
5451 config = yaml.load(datacenter['config'])
5452 if 'sdn-controller' in config:
5453 controller_id = config['sdn-controller']
5454 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
5455 result["sdn-controller"] = controller_id
5456 result["dpid"] = sdn_controller["dpid"]
5457
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02005458 if result["sdn-controller"] == None:
5459 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request)
5460 if result["dpid"] == None:
5461 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
5462 HTTP_Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01005463
5464 if len(maps) == 0:
5465 return result
5466
5467 ports_correspondence_dict = dict()
5468 for link in maps:
5469 if result["sdn-controller"] != link["ofc_id"]:
5470 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error)
5471 if result["dpid"] != link["switch_dpid"]:
5472 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error)
5473 element = dict()
5474 element["pci"] = link["pci"]
5475 if link["switch_port"]:
5476 element["switch_port"] = link["switch_port"]
5477 if link["switch_mac"]:
5478 element["switch_mac"] = link["switch_mac"]
5479
5480 if not link["compute_node"] in ports_correspondence_dict:
5481 content = dict()
5482 content["compute_node"] = link["compute_node"]
5483 content["ports"] = list()
5484 ports_correspondence_dict[link["compute_node"]] = content
5485
5486 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
5487
5488 for key in sorted(ports_correspondence_dict):
5489 result["ports_mapping"].append(ports_correspondence_dict[key])
5490
5491 return result
5492
5493def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02005494 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
gcalvinoe580c7d2017-09-22 14:09:51 +02005495
5496def create_RO_keypair(tenant_id):
5497 """
5498 Creates a public / private keys for a RO tenant and returns their values
5499 Params:
5500 tenant_id: ID of the tenant
5501 Return:
5502 public_key: Public key for the RO tenant
5503 private_key: Encrypted private key for RO tenant
5504 """
5505
5506 bits = 2048
5507 key = RSA.generate(bits)
5508 try:
5509 public_key = key.publickey().exportKey('OpenSSH')
5510 if isinstance(public_key, ValueError):
5511 raise NfvoException("Unable to create public key: {}".format(public_key), HTTP_Internal_Server_Error)
5512 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
5513 except (ValueError, NameError) as e:
5514 raise NfvoException("Unable to create private key: {}".format(e), HTTP_Internal_Server_Error)
5515 return public_key, private_key
5516
5517def decrypt_key (key, tenant_id):
5518 """
5519 Decrypts an encrypted RSA key
5520 Params:
5521 key: Private key to be decrypted
5522 tenant_id: ID of the tenant
5523 Return:
5524 unencrypted_key: Unencrypted private key for RO tenant
5525 """
5526 try:
5527 key = RSA.importKey(key,tenant_id)
5528 unencrypted_key = key.exportKey('PEM')
5529 if isinstance(unencrypted_key, ValueError):
5530 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), HTTP_Internal_Server_Error)
5531 except ValueError as e:
5532 raise NfvoException("Unable to decrypt the private key: {}".format(e), HTTP_Internal_Server_Error)
5533 return unencrypted_key