blob: e5d13eba12cab7d2a4ae75cff6e386bed42b5db8 [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openmano
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24'''
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
tierno42026a02017-02-10 15:13:40 +010034import vim_thread
tiernof97fd272016-07-11 14:32:37 +020035from db_base import HTTP_Unauthorized, HTTP_Bad_Request, HTTP_Internal_Server_Error, HTTP_Not_Found,\
tierno7edb6752016-03-21 17:37:52 +010036 HTTP_Conflict, HTTP_Method_Not_Allowed
37import console_proxy_thread as cli
tiernoae4a8d12016-07-08 12:30:39 +020038import vimconn
39import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020040import collections
tierno8e690322017-08-10 15:58:50 +020041from uuid import uuid4
tiernof97fd272016-07-11 14:32:37 +020042from db_base import db_base_Exception
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010043
tiernob3d36742017-03-03 23:51:05 +010044import nfvo_db
45from threading import Lock
tierno868220c2017-09-26 00:11:05 +020046import time as t
tierno01b3e172017-04-21 10:52:34 +020047from lib_osm_openvim import ovim as ovim_module
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +020048from lib_osm_openvim.ovim import ovimException
gcalvinoe580c7d2017-09-22 14:09:51 +020049from Crypto.PublicKey import RSA
tierno7edb6752016-03-21 17:37:52 +010050
tiernof1ba57e2017-09-07 12:23:19 +020051import osm_im.vnfd as vnfd_catalog
52import osm_im.nsd as nsd_catalog
tiernof1ba57e2017-09-07 12:23:19 +020053from pyangbind.lib.serialise import pybindJSONDecoder
54from itertools import chain
55
tierno7edb6752016-03-21 17:37:52 +010056global global_config
57global vimconn_imported
tierno73ad9e42016-09-12 18:11:11 +020058global logger
montesmoreno0c8def02016-12-22 12:16:23 +000059global default_volume_size
60default_volume_size = '5' #size in GB
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +010061global ovim
62ovim = None
tiernoc5651792017-03-27 10:50:43 +020063global_config = None
tiernoae4a8d12016-07-08 12:30:39 +020064
tierno42026a02017-02-10 15:13:40 +010065vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
66vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
tiernob3d36742017-03-03 23:51:05 +010067vim_persistent_info = {}
tierno73ad9e42016-09-12 18:11:11 +020068logger = logging.getLogger('openmano.nfvo')
tiernob3d36742017-03-03 23:51:05 +010069task_lock = Lock()
tiernob3d36742017-03-03 23:51:05 +010070last_task_id = 0.0
tierno868220c2017-09-26 00:11:05 +020071db = None
72db_lock = Lock()
tierno7edb6752016-03-21 17:37:52 +010073
74class NfvoException(Exception):
tiernoae4a8d12016-07-08 12:30:39 +020075 def __init__(self, message, http_code):
76 self.http_code = http_code
77 Exception.__init__(self, message)
tierno7edb6752016-03-21 17:37:52 +010078
79
tiernob3d36742017-03-03 23:51:05 +010080def get_task_id():
81 global last_task_id
tierno868220c2017-09-26 00:11:05 +020082 task_id = t.time()
tiernob3d36742017-03-03 23:51:05 +010083 if task_id <= last_task_id:
84 task_id = last_task_id + 0.000001
85 last_task_id = task_id
tierno868220c2017-09-26 00:11:05 +020086 return "ACTION-{:.6f}".format(task_id)
87 # 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 +010088
89
tierno867ffe92017-03-27 12:50:34 +020090def new_task(name, params, depends=None):
tierno868220c2017-09-26 00:11:05 +020091 """Deprected!!!"""
tiernob3d36742017-03-03 23:51:05 +010092 task_id = get_task_id()
93 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
94 if depends:
95 task["depends"] = depends
tiernob3d36742017-03-03 23:51:05 +010096 return task
97
98
99def is_task_id(id):
tierno868220c2017-09-26 00:11:05 +0200100 return True if id[:5] == "TASK-" else False
tiernob3d36742017-03-03 23:51:05 +0100101
102
tierno42026a02017-02-10 15:13:40 +0100103def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
104 name = datacenter_name[:16]
105 if name not in vim_threads["names"]:
106 vim_threads["names"].append(name)
107 return name
tiernob3d36742017-03-03 23:51:05 +0100108 name = datacenter_name[:16] + "." + tenant_name[:16]
tierno42026a02017-02-10 15:13:40 +0100109 if name not in vim_threads["names"]:
110 vim_threads["names"].append(name)
111 return name
112 name = datacenter_id + "-" + tenant_id
113 vim_threads["names"].append(name)
114 return name
115
116
117def start_service(mydb):
tiernob3d36742017-03-03 23:51:05 +0100118 global db, global_config
119 db = nfvo_db.nfvo_db()
120 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 +0100121 global ovim
122
123 # Initialize openvim for SDN control
124 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
125 # TODO: review ovim.py to delete not needed configuration
126 ovim_configuration = {
tierno639520f2017-04-05 19:55:36 +0200127 'logger_name': 'openmano.ovim',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100128 'network_vlan_range_start': 1000,
129 'network_vlan_range_end': 4096,
tierno639520f2017-04-05 19:55:36 +0200130 'db_name': global_config["db_ovim_name"],
131 'db_host': global_config["db_ovim_host"],
132 'db_user': global_config["db_ovim_user"],
133 'db_passwd': global_config["db_ovim_passwd"],
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100134 'bridge_ifaces': {},
135 'mode': 'normal',
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100136 'network_type': 'bridge',
137 #TODO: log_level_of should not be needed. To be modified in ovim
138 'log_level_of': 'DEBUG'
139 }
tierno42026a02017-02-10 15:13:40 +0100140 try:
tierno3fcfdb72017-10-24 07:48:24 +0200141 # starts ovim library
tierno46df9672017-05-26 13:12:21 +0200142 ovim = ovim_module.ovim(ovim_configuration)
143 ovim.start_service()
144
tierno3fcfdb72017-10-24 07:48:24 +0200145 #delete old unneeded vim_actions
146 clean_db(mydb)
147
148 # starts vim_threads
tierno46df9672017-05-26 13:12:21 +0200149 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
150 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
151 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
152 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
153 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
154 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
tierno42026a02017-02-10 15:13:40 +0100155 vims = mydb.get_rows(FROM=from_, SELECT=select_)
156 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200157 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
158 'datacenter_id': vim.get('datacenter_id')}
tierno42026a02017-02-10 15:13:40 +0100159 if vim["config"]:
160 extra.update(yaml.load(vim["config"]))
161 if vim.get('dt_config'):
162 extra.update(yaml.load(vim["dt_config"]))
163 if vim["type"] not in vimconn_imported:
164 module_info=None
165 try:
166 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200167 pkg = __import__("osm_ro." + module)
168 vim_conn = getattr(pkg, module)
169 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
170 # vim_conn = imp.load_module(vim["type"], *module_info)
tierno42026a02017-02-10 15:13:40 +0100171 vimconn_imported[vim["type"]] = vim_conn
172 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200173 # if module_info and module_info[0]:
174 # file.close(module_info[0])
tiernocdee8cc2017-04-25 13:42:06 +0200175 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
tiernob3d36742017-03-03 23:51:05 +0100176 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100177
tierno867ffe92017-03-27 12:50:34 +0200178 thread_id = vim['datacenter_tenant_id']
tiernob3d36742017-03-03 23:51:05 +0100179 vim_persistent_info[thread_id] = {}
tierno42026a02017-02-10 15:13:40 +0100180 try:
181 #if not tenant:
182 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
183 myvim = vimconn_imported[ vim["type"] ].vimconnector(
tiernob3d36742017-03-03 23:51:05 +0100184 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
185 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
186 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
187 user=vim['user'], passwd=vim['passwd'],
188 config=extra, persistent_info=vim_persistent_info[thread_id]
189 )
tierno9c22f2d2017-10-09 16:23:55 +0200190 except vimconn.vimconnException as e:
191 myvim = e
192 logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
193 vim['datacenter_id'], e))
tierno42026a02017-02-10 15:13:40 +0100194 except Exception as e:
tierno46df9672017-05-26 13:12:21 +0200195 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
196 HTTP_Internal_Server_Error)
197 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
198 vim['vim_tenant_id'])
tiernob3d36742017-03-03 23:51:05 +0100199 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
tierno867ffe92017-03-27 12:50:34 +0200200 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
tierno42026a02017-02-10 15:13:40 +0100201 new_thread.start()
tierno42026a02017-02-10 15:13:40 +0100202 vim_threads["running"][thread_id] = new_thread
203 except db_base_Exception as e:
204 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno46df9672017-05-26 13:12:21 +0200205 except ovim_module.ovimException as e:
206 message = str(e)
207 if message[:22] == "DATABASE wrong version":
208 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
209 "at host {dbhost}".format(
210 msg=message[22:-3], dbname=global_config["db_ovim_name"],
211 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
212 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
213 raise NfvoException(message, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100214
tierno867ffe92017-03-27 12:50:34 +0200215
tierno42026a02017-02-10 15:13:40 +0100216def stop_service():
tiernoc5651792017-03-27 10:50:43 +0200217 global ovim, global_config
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +0100218 if ovim:
219 ovim.stop_service()
tierno42026a02017-02-10 15:13:40 +0100220 for thread_id,thread in vim_threads["running"].items():
tierno868220c2017-09-26 00:11:05 +0200221 thread.insert_task("exit")
tierno42026a02017-02-10 15:13:40 +0100222 vim_threads["deleting"][thread_id] = thread
tiernob3d36742017-03-03 23:51:05 +0100223 vim_threads["running"] = {}
tiernoc5651792017-03-27 10:50:43 +0200224 if global_config and global_config.get("console_thread"):
225 for thread in global_config["console_thread"]:
226 thread.terminate = True
tiernob3d36742017-03-03 23:51:05 +0100227
tierno6ddeded2017-05-16 15:40:26 +0200228def get_version():
229 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
230 global_config["version_date"] ))
231
tierno3fcfdb72017-10-24 07:48:24 +0200232def clean_db(mydb):
233 """
234 Clean unused or old entries at database to avoid unlimited growing
235 :param mydb: database connector
236 :return: None
237 """
238 # get and delete unused vim_actions: all elements deleted, one week before, instance not present
239 now = t.time()-3600*24*7
240 instance_action_id = None
241 nb_deleted = 0
242 while True:
243 actions_to_delete = mydb.get_rows(
244 SELECT=("item", "item_id", "instance_action_id"),
245 FROM="vim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
246 "left join instance_scenarios as i on ia.instance_id=i.uuid",
247 WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None,
248 "va.status": ("DONE", "SUPERSEDED")},
249 LIMIT=100
250 )
251 for to_delete in actions_to_delete:
252 mydb.delete_row(FROM="vim_actions", WHERE=to_delete)
253 if instance_action_id != to_delete["instance_action_id"]:
254 instance_action_id = to_delete["instance_action_id"]
255 mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id})
256 nb_deleted += len(actions_to_delete)
257 if len(actions_to_delete) < 100:
258 break
259 if nb_deleted:
260 logger.debug("Removed {} unused vim_actions".format(nb_deleted))
261
262
tierno42026a02017-02-10 15:13:40 +0100263
tierno7edb6752016-03-21 17:37:52 +0100264def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
265 '''Obtain flavorList
266 return result, content:
267 <0, error_text upon error
268 nb_records, flavor_list on success
269 '''
270 WHERE_dict={}
271 WHERE_dict['vnf_id'] = vnf_id
272 if nfvo_tenant is not None:
273 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100274
tierno7edb6752016-03-21 17:37:52 +0100275 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
276 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200277 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
278 #print "get_flavor_list result:", result
279 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +0100280 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +0200281 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +0100282 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +0200283 return flavorList
tierno7edb6752016-03-21 17:37:52 +0100284
tiernob3d36742017-03-03 23:51:05 +0100285
tierno7edb6752016-03-21 17:37:52 +0100286def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
287 '''Obtain imageList
288 return result, content:
289 <0, error_text upon error
290 nb_records, flavor_list on success
291 '''
292 WHERE_dict={}
293 WHERE_dict['vnf_id'] = vnf_id
294 if nfvo_tenant is not None:
295 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
tierno42026a02017-02-10 15:13:40 +0100296
tierno7edb6752016-03-21 17:37:52 +0100297 #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +0200298 images = mydb.get_rows(FROM='vms join images on vms.image_id=images.uuid',SELECT=('image_id',),WHERE=WHERE_dict )
tierno7edb6752016-03-21 17:37:52 +0100299 imageList=[]
tiernof97fd272016-07-11 14:32:37 +0200300 for image in images:
tierno7edb6752016-03-21 17:37:52 +0100301 imageList.append(image['image_id'])
tiernof97fd272016-07-11 14:32:37 +0200302 return imageList
tierno7edb6752016-03-21 17:37:52 +0100303
tiernob3d36742017-03-03 23:51:05 +0100304
tiernoa2793912016-10-04 08:15:08 +0000305def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
306 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None):
tierno7edb6752016-03-21 17:37:52 +0100307 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tierno42026a02017-02-10 15:13:40 +0100308 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100309 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200310 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100311 '''
312 WHERE_dict={}
313 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
314 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000315 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100316 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
317 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000318 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
319 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100320 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 +0000321 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 +0100322 '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 +0000323 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100324 else:
325 from_ = 'datacenters as d'
326 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200327 try:
328 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
329 vim_dict={}
330 for vim in vims:
tierno867ffe92017-03-27 12:50:34 +0200331 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
332 'datacenter_id': vim.get('datacenter_id')}
tierno8008c3a2016-10-13 15:34:28 +0000333 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200334 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000335 if vim.get('dt_config'):
336 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200337 if vim["type"] not in vimconn_imported:
338 module_info=None
339 try:
340 module = "vimconn_" + vim["type"]
tierno361275f2017-04-25 16:24:34 +0200341 pkg = __import__("osm_ro." + module)
342 vim_conn = getattr(pkg, module)
343 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
344 # vim_conn = imp.load_module(vim["type"], *module_info)
tiernof97fd272016-07-11 14:32:37 +0200345 vimconn_imported[vim["type"]] = vim_conn
346 except (IOError, ImportError) as e:
tierno361275f2017-04-25 16:24:34 +0200347 # if module_info and module_info[0]:
348 # file.close(module_info[0])
tiernof97fd272016-07-11 14:32:37 +0200349 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
350 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100351
tierno7edb6752016-03-21 17:37:52 +0100352 try:
tierno867ffe92017-03-27 12:50:34 +0200353 if 'datacenter_tenant_id' in vim:
354 thread_id = vim["datacenter_tenant_id"]
tiernob3d36742017-03-03 23:51:05 +0100355 if thread_id not in vim_persistent_info:
356 vim_persistent_info[thread_id] = {}
357 persistent_info = vim_persistent_info[thread_id]
358 else:
359 persistent_info = {}
tiernof97fd272016-07-11 14:32:37 +0200360 #if not tenant:
361 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
362 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
363 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tiernob3d36742017-03-03 23:51:05 +0100364 tenant_id=vim.get('vim_tenant_id',vim_tenant),
365 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tierno42026a02017-02-10 15:13:40 +0100366 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200367 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernob3d36742017-03-03 23:51:05 +0100368 config=extra, persistent_info=persistent_info
tiernof97fd272016-07-11 14:32:37 +0200369 )
370 except Exception as e:
371 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
372 return vim_dict
373 except db_base_Exception as e:
374 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
tierno42026a02017-02-10 15:13:40 +0100375
tiernob3d36742017-03-03 23:51:05 +0100376
tierno7edb6752016-03-21 17:37:52 +0100377def rollback(mydb, vims, rollback_list):
378 undeleted_items=[]
tierno42026a02017-02-10 15:13:40 +0100379 #delete things by reverse order
tierno7edb6752016-03-21 17:37:52 +0100380 for i in range(len(rollback_list)-1, -1, -1):
381 item = rollback_list[i]
382 if item["where"]=="vim":
383 if item["vim_id"] not in vims:
384 continue
tierno56d73d22017-08-02 13:53:02 +0200385 if is_task_id(item["uuid"]):
386 continue
387 vim = vims[item["vim_id"]]
tiernoae4a8d12016-07-08 12:30:39 +0200388 try:
389 if item["what"]=="image":
390 vim.delete_image(item["uuid"])
tierno868220c2017-09-26 00:11:05 +0200391 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200392 elif item["what"]=="flavor":
393 vim.delete_flavor(item["uuid"])
garciadeblas9f8456e2016-09-05 05:02:59 +0200394 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200395 elif item["what"]=="network":
396 vim.delete_network(item["uuid"])
397 elif item["what"]=="vm":
398 vim.delete_vminstance(item["uuid"])
399 except vimconn.vimconnException as e:
400 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
401 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200402 except db_base_Exception as e:
403 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 +0100404
tierno7edb6752016-03-21 17:37:52 +0100405 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200406 try:
407 if item["what"]=="image":
408 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
409 elif item["what"]=="flavor":
410 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
411 except db_base_Exception as e:
412 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
413 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno42026a02017-02-10 15:13:40 +0100414 if len(undeleted_items)==0:
tierno7edb6752016-03-21 17:37:52 +0100415 return True," Rollback successful."
416 else:
417 return False," Rollback fails to delete: " + str(undeleted_items)
tierno42026a02017-02-10 15:13:40 +0100418
tiernob3d36742017-03-03 23:51:05 +0100419
tiernoafed5f12017-01-26 17:57:43 +0100420def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100421 global global_config
tierno42026a02017-02-10 15:13:40 +0100422 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
tierno7edb6752016-03-21 17:37:52 +0100423 vnfc_interfaces={}
424 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100425 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100426 #dataplane interfaces
427 for numa in vnfc.get("numas",() ):
428 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100429 if interface["name"] in name_dict:
430 raise NfvoException(
431 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
432 vnfc["name"], interface["name"]),
433 HTTP_Bad_Request)
434 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100435 #bridge interfaces
436 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100437 if interface["name"] in name_dict:
438 raise NfvoException(
439 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
440 vnfc["name"], interface["name"]),
441 HTTP_Bad_Request)
442 name_dict[ interface["name"] ] = "overlay"
443 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100444 # check bood-data info
tierno40e1bce2017-08-09 09:12:04 +0200445 # if "boot-data" in vnfc:
446 # # check that user-data is incompatible with users and config-files
447 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
448 # raise NfvoException(
449 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
450 # HTTP_Bad_Request)
tierno36c0b172017-01-12 18:32:28 +0100451
tierno7edb6752016-03-21 17:37:52 +0100452 #check if the info in external_connections matches with the one in the vnfcs
453 name_list=[]
454 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
455 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100456 raise NfvoException(
457 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
458 external_connection["name"]),
459 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100460 name_list.append(external_connection["name"])
461 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100462 raise NfvoException(
463 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
464 external_connection["name"], external_connection["VNFC"]),
465 HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +0100466
tierno7edb6752016-03-21 17:37:52 +0100467 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100468 raise NfvoException(
469 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
470 external_connection["name"],
471 external_connection["local_iface_name"]),
472 HTTP_Bad_Request )
tierno42026a02017-02-10 15:13:40 +0100473
tierno7edb6752016-03-21 17:37:52 +0100474 #check if the info in internal_connections matches with the one in the vnfcs
475 name_list=[]
476 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
477 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100478 raise NfvoException(
479 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
480 internal_connection["name"]),
481 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100482 name_list.append(internal_connection["name"])
483 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100484
485 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
486 raise NfvoException(
487 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
488 internal_connection["name"],
489 'ptp' if vnf_descriptor_version==1 else 'e-line',
490 'data' if vnf_descriptor_version==1 else "e-lan"),
491 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100492 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100493 vnf = port["VNFC"]
494 iface = port["local_iface_name"]
495 if vnf not in vnfc_interfaces:
496 raise NfvoException(
497 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
498 internal_connection["name"], vnf),
499 HTTP_Bad_Request)
500 if iface not in vnfc_interfaces[ vnf ]:
501 raise NfvoException(
502 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
503 internal_connection["name"], iface),
504 HTTP_Bad_Request)
505 return -HTTP_Bad_Request,
506 if vnf_descriptor_version==1 and "type" not in internal_connection:
507 if vnfc_interfaces[vnf][iface] == "overlay":
508 internal_connection["type"] = "bridge"
509 else:
510 internal_connection["type"] = "data"
511 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
512 if vnfc_interfaces[vnf][iface] == "overlay":
513 internal_connection["implementation"] = "overlay"
514 else:
515 internal_connection["implementation"] = "underlay"
516 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
517 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
518 raise NfvoException(
519 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
520 internal_connection["name"],
521 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
522 'data' if vnf_descriptor_version==1 else 'underlay'),
523 HTTP_Bad_Request)
524 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
525 vnfc_interfaces[vnf][iface] == "underlay":
526 raise NfvoException(
527 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
528 internal_connection["name"], iface,
529 'data' if vnf_descriptor_version==1 else 'underlay',
530 'bridge' if vnf_descriptor_version==1 else 'overlay'),
531 HTTP_Bad_Request)
532
tierno7edb6752016-03-21 17:37:52 +0100533
tierno56d73d22017-08-02 13:53:02 +0200534def 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 +0100535 #look if image exist
536 if only_create_at_vim:
537 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000538 if return_on_error == None:
539 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100540 else:
garciadeblas14480452017-01-10 13:08:07 +0100541 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200542 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
543 else:
544 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200545 if len(images)>=1:
546 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100547 else:
garciadeblas14480452017-01-10 13:08:07 +0100548 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100549 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200550 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
551 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100552 }
garciadeblas14480452017-01-10 13:08:07 +0100553 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200554 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
555 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100556 #create image at every vim
557 for vim_id,vim in vims.iteritems():
tierno868220c2017-09-26 00:11:05 +0200558 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100559 image_created="false"
560 #look at database
tierno868220c2017-09-26 00:11:05 +0200561 image_db = mydb.get_rows(FROM="datacenters_images",
562 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100563 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200564 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200565 if image_dict['location'] is not None:
566 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
567 else:
garciadeblas30833382017-01-09 09:46:31 +0100568 filter_dict = {}
569 filter_dict['name'] = image_dict['universal_name']
570 if image_dict.get('checksum') != None:
571 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000572 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200573 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100574 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200575 if len(vim_images) > 1:
garciadeblas3fa2c052017-01-05 12:00:08 +0100576 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000577 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100578 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200579 else:
garciadeblas14480452017-01-10 13:08:07 +0100580 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
581 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200582
tiernoae4a8d12016-07-08 12:30:39 +0200583 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100584 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tierno42026a02017-02-10 15:13:40 +0100585 try:
garciadeblas14480452017-01-10 13:08:07 +0100586 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
587 if image_dict['location']:
588 image_vim_id = vim.new_image(image_dict)
589 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
590 image_created="true"
591 else:
garciadeblasb6153a22017-02-06 15:38:33 +0100592 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
593 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200594 except vimconn.vimconnException as e:
595 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100596 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200597 raise
tierno5e91eb82016-10-04 09:39:07 +0000598 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100599 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200600 continue
601 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000602 if return_on_error:
603 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
604 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200605 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000606 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100607 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200608 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200609 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100610 #add new vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200611 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
612 'image_id':image_mano_id,
613 'vim_id': image_vim_id,
614 'created':image_created})
tierno7edb6752016-03-21 17:37:52 +0100615 elif image_db[0]["vim_id"]!=image_vim_id:
616 #modify existing vim_id at datacenters_images
tierno868220c2017-09-26 00:11:05 +0200617 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 +0100618
tiernof97fd272016-07-11 14:32:37 +0200619 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100620
tiernob3d36742017-03-03 23:51:05 +0100621
tierno5e91eb82016-10-04 09:39:07 +0000622def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
tierno7edb6752016-03-21 17:37:52 +0100623 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
624 'ram':flavor_dict.get('ram'),
625 'vcpus':flavor_dict.get('vcpus'),
626 }
627 if 'extended' in flavor_dict and flavor_dict['extended']==None:
628 del flavor_dict['extended']
629 if 'extended' in flavor_dict:
630 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
631
632 #look if flavor exist
633 if only_create_at_vim:
634 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000635 if return_on_error == None:
636 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100637 else:
tiernof97fd272016-07-11 14:32:37 +0200638 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
639 if len(flavors)>=1:
640 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100641 else:
642 #create flavor
643 #create one by one the images of aditional disks
644 dev_image_list=[] #list of images
645 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
646 dev_nb=0
647 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200648 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100649 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200650 image_dict={}
651 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
652 image_dict['universal_name']=device.get('image name')
653 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
654 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100655 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200656 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100657 image_metadata_dict = device.get('image metadata', None)
658 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100659 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100660 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
661 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200662 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
663 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100664 dev_image_list.append(image_id)
tierno42026a02017-02-10 15:13:40 +0100665 dev_nb += 1
tierno7edb6752016-03-21 17:37:52 +0100666 temp_flavor_dict['name'] = flavor_dict['name']
667 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200668 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
669 flavor_mano_id= content
670 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100671 #create flavor at every vim
672 if 'uuid' in flavor_dict:
673 del flavor_dict['uuid']
674 flavor_vim_id=None
675 for vim_id,vim in vims.items():
tierno868220c2017-09-26 00:11:05 +0200676 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
tierno7edb6752016-03-21 17:37:52 +0100677 flavor_created="false"
678 #look at database
tierno868220c2017-09-26 00:11:05 +0200679 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
680 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100681 #look at VIM if this flavor exist SKIPPED
682 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
683 #if res_vim < 0:
684 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
685 # continue
686 #elif res_vim==0:
tierno42026a02017-02-10 15:13:40 +0100687
tiernof1ba57e2017-09-07 12:23:19 +0200688 # Create the flavor in VIM
689 # Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000690 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100691 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
tiernof1ba57e2017-09-07 12:23:19 +0200692 # make a copy of original devices
tierno7edb6752016-03-21 17:37:52 +0100693 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000694
tierno7edb6752016-03-21 17:37:52 +0100695 for device in flavor_dict["extended"].get("devices",[]):
696 dev={}
697 dev.update(device)
698 devices_original.append(dev)
699 if 'image' in device:
700 del device['image']
701 if 'image metadata' in device:
702 del device['image metadata']
tiernof1ba57e2017-09-07 12:23:19 +0200703 if 'image checksum' in device:
704 del device['image checksum']
705 dev_nb = 0
tierno7edb6752016-03-21 17:37:52 +0100706 for index in range(0,len(devices_original)) :
707 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000708 if "image" not in device and "image name" not in device:
709 if 'size' in device:
710 disk_list.append({'size': device.get('size', default_volume_size)})
tierno7edb6752016-03-21 17:37:52 +0100711 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200712 image_dict={}
713 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
714 image_dict['universal_name']=device.get('image name')
715 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
716 image_dict['location']=device.get('image')
tiernof1ba57e2017-09-07 12:23:19 +0200717 # image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200718 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100719 image_metadata_dict = device.get('image metadata', None)
720 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +0100721 if image_metadata_dict != None:
tierno7edb6752016-03-21 17:37:52 +0100722 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
723 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200724 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 +0100725 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200726 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 +0000727
728 #save disk information (image must be based on and size
729 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
730
tierno7edb6752016-03-21 17:37:52 +0100731 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
732 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200733 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100734 #check that this vim_id exist in VIM, if not create
735 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200736 try:
737 vim.get_flavor(flavor_vim_id)
738 continue #flavor exist
739 except vimconn.vimconnException:
740 pass
tierno7edb6752016-03-21 17:37:52 +0100741 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200742 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
743 try:
tiernocf157a82017-01-30 14:07:06 +0100744 flavor_vim_id = None
745 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
746 flavor_create="false"
747 except vimconn.vimconnException as e:
748 pass
749 try:
750 if not flavor_vim_id:
751 flavor_vim_id = vim.new_flavor(flavor_dict)
752 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
753 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200754 except vimconn.vimconnException as e:
755 if return_on_error:
756 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200757 raise
tiernoae4a8d12016-07-08 12:30:39 +0200758 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000759 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200760 continue
tierno7edb6752016-03-21 17:37:52 +0100761 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200762 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100763 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000764 extended_devices_yaml = None
765 if len(disk_list) > 0:
766 extended_devices = dict()
767 extended_devices['disks'] = disk_list
768 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
769 mydb.new_row('datacenters_flavors',
tierno868220c2017-09-26 00:11:05 +0200770 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
771 'created': flavor_created, 'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100772 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
773 #modify existing vim_id at datacenters_flavors
tierno868220c2017-09-26 00:11:05 +0200774 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
775 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
tierno42026a02017-02-10 15:13:40 +0100776
tiernof97fd272016-07-11 14:32:37 +0200777 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100778
tiernob3d36742017-03-03 23:51:05 +0100779
tiernof1ba57e2017-09-07 12:23:19 +0200780def get_str(obj, field, length):
781 """
782 Obtain the str value,
783 :param obj:
784 :param length:
785 :return:
786 """
787 value = obj.get(field)
788 if value is not None:
789 value = str(value)[:length]
790 return value
791
792def _lookfor_or_create_image(db_image, mydb, descriptor):
793 """
794 fill image content at db_image dictionary. Check if the image with this image and checksum exist
795 :param db_image: dictionary to insert data
796 :param mydb: database connector
797 :param descriptor: yang descriptor
798 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
799 """
800
801 db_image["name"] = get_str(descriptor, "image", 255)
802 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
803 if not db_image["checksum"]: # Ensure that if empty string, None is stored
804 db_image["checksum"] = None
805 if db_image["name"].startswith("/"):
806 db_image["location"] = db_image["name"]
807 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
808 else:
809 db_image["universal_name"] = db_image["name"]
810 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
811 'checksum': db_image['checksum']})
812 if existing_images:
813 return existing_images[0]["uuid"]
814 else:
815 image_uuid = str(uuid4())
816 db_image["uuid"] = image_uuid
817 return None
818
819def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
820 """
821 Parses an OSM IM vnfd_catalog and insert at DB
822 :param mydb:
823 :param tenant_id:
824 :param vnf_descriptor:
825 :return: The list of cretated vnf ids
826 """
827 try:
828 myvnfd = vnfd_catalog.vnfd()
tiernoa9550202017-09-22 13:31:35 +0200829 try:
830 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd)
831 except Exception as e:
tiernob2880eb2017-10-04 15:04:53 +0200832 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +0200833 db_vnfs = []
834 db_nets = []
835 db_vms = []
836 db_vms_index = 0
837 db_interfaces = []
838 db_images = []
839 db_flavors = []
840 uuid_list = []
841 vnfd_uuid_list = []
tiernoe18ba432017-10-12 10:22:45 +0200842 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
843 if not vnfd_catalog_descriptor:
844 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
845 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
846 if not vnfd_descriptor_list:
847 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
tiernob2880eb2017-10-04 15:04:53 +0200848 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
849 vnfd = vnfd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +0200850
851 # table vnf
852 vnf_uuid = str(uuid4())
853 uuid_list.append(vnf_uuid)
854 vnfd_uuid_list.append(vnf_uuid)
855 db_vnf = {
856 "uuid": vnf_uuid,
857 "osm_id": get_str(vnfd, "id", 255),
858 "name": get_str(vnfd, "name", 255),
859 "description": get_str(vnfd, "description", 255),
860 "tenant_id": tenant_id,
861 "vendor": get_str(vnfd, "vendor", 255),
862 "short_name": get_str(vnfd, "short-name", 255),
863 "descriptor": str(vnf_descriptor)[:60000]
864 }
865
tiernoe18ba432017-10-12 10:22:45 +0200866 for vnfd_descriptor in vnfd_descriptor_list:
867 if vnfd_descriptor["id"] == str(vnfd["id"]):
868 break
869
tiernof1ba57e2017-09-07 12:23:19 +0200870 # table nets (internal-vld)
871 net_id2uuid = {} # for mapping interface with network
872 for vld in vnfd.get("internal-vld").itervalues():
873 net_uuid = str(uuid4())
874 uuid_list.append(net_uuid)
875 db_net = {
876 "name": get_str(vld, "name", 255),
877 "vnf_id": vnf_uuid,
878 "uuid": net_uuid,
879 "description": get_str(vld, "description", 255),
880 "type": "bridge", # TODO adjust depending on connection point type
881 }
882 net_id2uuid[vld.get("id")] = net_uuid
883 db_nets.append(db_net)
884
885 # table vms (vdus)
886 vdu_id2uuid = {}
887 vdu_id2db_table_index = {}
888 for vdu in vnfd.get("vdu").itervalues():
889 vm_uuid = str(uuid4())
890 uuid_list.append(vm_uuid)
891 db_vm = {
892 "uuid": vm_uuid,
893 "osm_id": get_str(vdu, "id", 255),
894 "name": get_str(vdu, "name", 255),
895 "description": get_str(vdu, "description", 255),
896 "vnf_id": vnf_uuid,
897 }
898 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
899 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
900 if vdu.get("count"):
901 db_vm["count"] = int(vdu["count"])
902
903 # table image
904 image_present = False
905 if vdu.get("image"):
906 image_present = True
907 db_image = {}
908 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
909 if not image_uuid:
910 image_uuid = db_image["uuid"]
911 db_images.append(db_image)
912 db_vm["image_id"] = image_uuid
913
914 # volumes
915 devices = []
916 if vdu.get("volumes"):
917 for volume_key in sorted(vdu["volumes"]):
918 volume = vdu["volumes"][volume_key]
919 if not image_present:
920 # Convert the first volume to vnfc.image
921 image_present = True
922 db_image = {}
923 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
924 if not image_uuid:
925 image_uuid = db_image["uuid"]
926 db_images.append(db_image)
927 db_vm["image_id"] = image_uuid
928 else:
929 # Add Openmano devices
930 device = {}
931 device["type"] = str(volume.get("device-type"))
932 if volume.get("size"):
933 device["size"] = int(volume["size"])
934 if volume.get("image"):
935 device["image name"] = str(volume["image"])
936 if volume.get("image-checksum"):
937 device["image checksum"] = str(volume["image-checksum"])
938 devices.append(device)
939
940 # table flavors
941 db_flavor = {
942 "name": get_str(vdu, "name", 250) + "-flv",
943 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
944 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
945 "disk": int(vdu["vm-flavor"].get("storage-gb", 1)),
946 }
947 # EPA TODO revise
948 extended = {}
949 numa = {}
950 if devices:
951 extended["devices"] = devices
952 if vdu.get("guest-epa"): # TODO or dedicated_int:
953 epa_vcpu_set = False
954 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
955 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
956 if numa_node_policy.get("node"):
tierno39dddcc2017-10-05 18:48:06 +0200957 numa_node = numa_node_policy["node"]['0']
tiernof1ba57e2017-09-07 12:23:19 +0200958 if numa_node.get("num-cores"):
959 numa["cores"] = numa_node["num-cores"]
960 epa_vcpu_set = True
961 if numa_node.get("paired-threads"):
962 if numa_node["paired-threads"].get("num-paired-threads"):
tierno39dddcc2017-10-05 18:48:06 +0200963 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
tiernof1ba57e2017-09-07 12:23:19 +0200964 epa_vcpu_set = True
tierno39dddcc2017-10-05 18:48:06 +0200965 if len(numa_node["paired-threads"].get("paired-thread-ids")):
tiernof1ba57e2017-09-07 12:23:19 +0200966 numa["paired-threads-id"] = []
tierno39dddcc2017-10-05 18:48:06 +0200967 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
tiernof1ba57e2017-09-07 12:23:19 +0200968 numa["paired-threads-id"].append(
969 (str(pair["thread-a"]), str(pair["thread-b"]))
970 )
971 if numa_node.get("num-threads"):
tierno39dddcc2017-10-05 18:48:06 +0200972 numa["threads"] = int(numa_node["num-threads"])
tiernof1ba57e2017-09-07 12:23:19 +0200973 epa_vcpu_set = True
974 if numa_node.get("memory-mb"):
975 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
976 if vdu["guest-epa"].get("mempage-size"):
977 if vdu["guest-epa"]["mempage-size"] != "SMALL":
978 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
979 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
980 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
981 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
982 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
983 numa["cores"] = max(db_flavor["vcpus"], 1)
984 else:
985 numa["threads"] = max(db_flavor["vcpus"], 1)
986 if numa:
987 extended["numas"] = [numa]
988 if extended:
989 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
990 db_flavor["extended"] = extended_text
991 # look if flavor exist
992
993 temp_flavor_dict = {'disk': db_flavor.get('disk', 1),
994 'ram': db_flavor.get('ram'),
995 'vcpus': db_flavor.get('vcpus'),
996 'extended': db_flavor.get('extended')
997 }
998 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
999 if existing_flavors:
1000 flavor_uuid = existing_flavors[0]["uuid"]
1001 else:
1002 flavor_uuid = str(uuid4())
1003 uuid_list.append(flavor_uuid)
1004 db_flavor["uuid"] = flavor_uuid
1005 db_flavors.append(db_flavor)
1006 db_vm["flavor_id"] = flavor_uuid
1007
1008 # cloud-init
1009 boot_data = {}
1010 if vdu.get("cloud-init"):
gcalvinoe580c7d2017-09-22 14:09:51 +02001011 boot_data["user-data"] = str(vdu["cloud-init"])
tiernof1ba57e2017-09-07 12:23:19 +02001012 elif vdu.get("cloud-init-file"):
1013 # TODO Where this file content is present???
tiernob2880eb2017-10-04 15:04:53 +02001014 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
tiernof1ba57e2017-09-07 12:23:19 +02001015 boot_data["user-data"] = str(vdu["cloud-init-file"])
1016
1017 if vdu.get("supplemental-boot-data"):
1018 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1019 boot_data['boot-data-drive'] = True
1020 if vdu["supplemental-boot-data"].get('config-file'):
1021 om_cfgfile_list = list()
1022 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
1023 # TODO Where this file content is present???
1024 cfg_source = str(custom_config_file["source"])
1025 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1026 "content": cfg_source})
1027 boot_data['config-files'] = om_cfgfile_list
1028 if boot_data:
gcalvino51757e92017-10-03 11:31:31 +02001029 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
tiernof1ba57e2017-09-07 12:23:19 +02001030
1031 db_vms.append(db_vm)
1032 db_vms_index += 1
1033
1034 # table interfaces (internal/external interfaces)
1035 cp_name2iface_uuid = {}
1036 cp_name2vm_uuid = {}
tiernoe2ff1ce2017-11-02 17:01:10 +01001037 cp_name2db_interface = {}
tiernoa9550202017-09-22 13:31:35 +02001038 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1039 for iface in vdu.get("interface").itervalues():
tiernof1ba57e2017-09-07 12:23:19 +02001040 iface_uuid = str(uuid4())
1041 uuid_list.append(iface_uuid)
1042 db_interface = {
1043 "uuid": iface_uuid,
1044 "internal_name": get_str(iface, "name", 255),
1045 "vm_id": vm_uuid,
1046 }
1047 if iface.get("virtual-interface").get("vpci"):
1048 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1049
1050 if iface.get("virtual-interface").get("bandwidth"):
1051 bps = int(iface.get("virtual-interface").get("bandwidth"))
1052 db_interface["bw"] = bps/1000
1053
1054 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1055 db_interface["type"] = "mgmt"
1056 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000"):
1057 db_interface["type"] = "bridge"
1058 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1059 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1060 db_interface["type"] = "data"
1061 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1062 else:
tiernob2880eb2017-10-04 15:04:53 +02001063 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1064 "-interface':'type':'{}'. Interface type is not supported".format(
1065 str(vnfd["id"])[:255], str(vdu["id"])[:255],
1066 iface.get("virtual-interface").get("type")),
1067 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001068
tiernoa9550202017-09-22 13:31:35 +02001069 if iface.get("external-connection-point-ref"):
tiernof1ba57e2017-09-07 12:23:19 +02001070 try:
tiernoa9550202017-09-22 13:31:35 +02001071 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
tiernof1ba57e2017-09-07 12:23:19 +02001072 db_interface["external_name"] = get_str(cp, "name", 255)
1073 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1074 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
tiernoe2ff1ce2017-11-02 17:01:10 +01001075 cp_name2db_interface[db_interface["external_name"]] = db_interface
tiernoe18ba432017-10-12 10:22:45 +02001076 for cp_descriptor in vnfd_descriptor["connection-point"]:
1077 if cp_descriptor["name"] == db_interface["external_name"]:
1078 break
1079 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
tierno137b0d92017-10-06 14:03:05 +02001080 db_interface["port_security"] = 0
tiernoe18ba432017-10-12 10:22:45 +02001081 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
tierno137b0d92017-10-06 14:03:05 +02001082 db_interface["port_security"] = 1
tiernof1ba57e2017-09-07 12:23:19 +02001083 except KeyError:
tiernob2880eb2017-10-04 15:04:53 +02001084 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1085 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1086 " at connection-point".format(
1087 vnf=vnfd["id"], vdu=vdu["id"], iface=iface["name"],
1088 cp=iface.get("vnfd-connection-point-ref")),
1089 HTTP_Bad_Request)
tiernoa9550202017-09-22 13:31:35 +02001090 elif iface.get("internal-connection-point-ref"):
tiernof1ba57e2017-09-07 12:23:19 +02001091 try:
1092 for vld in vnfd.get("internal-vld").itervalues():
1093 for cp in vld.get("internal-connection-point").itervalues():
tiernoa9550202017-09-22 13:31:35 +02001094 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
tiernof1ba57e2017-09-07 12:23:19 +02001095 db_interface["net_id"] = net_id2uuid[vld.get("id")]
tiernoe18ba432017-10-12 10:22:45 +02001096 for cp_descriptor in vnfd_descriptor["connection-point"]:
1097 if cp_descriptor["name"] == db_interface["external_name"]:
1098 break
1099 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
tierno137b0d92017-10-06 14:03:05 +02001100 db_interface["port_security"] = 0
tiernoe18ba432017-10-12 10:22:45 +02001101 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
tierno137b0d92017-10-06 14:03:05 +02001102 db_interface["port_security"] = 1
tiernof1ba57e2017-09-07 12:23:19 +02001103 break
1104 except KeyError:
tiernob2880eb2017-10-04 15:04:53 +02001105 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1106 "'interface[{iface}]':'vdu-internal-connection-point-ref':'{cp}' is not"
1107 " referenced by any internal-vld".format(
1108 vnf=vnfd["id"], vdu=vdu["id"], iface=iface["name"],
1109 cp=iface.get("vdu-internal-connection-point-ref")),
1110 HTTP_Bad_Request)
tiernoa9550202017-09-22 13:31:35 +02001111 if iface.get("position") is not None:
1112 db_interface["created_at"] = int(iface.get("position")) - 1000
tiernof1ba57e2017-09-07 12:23:19 +02001113 db_interfaces.append(db_interface)
1114
1115 # VNF affinity and antiaffinity
1116 for pg in vnfd.get("placement-groups").itervalues():
1117 pg_name = get_str(pg, "name", 255)
1118 for vdu in pg.get("member-vdus").itervalues():
1119 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1120 if vdu_id not in vdu_id2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02001121 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1122 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1123 vnf=vnfd["id"], pg=pg_name, vdu=vdu_id),
1124 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001125 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
1126 # TODO consider the case of isolation and not colocation
1127 # if pg.get("strategy") == "ISOLATION":
1128
1129 # VNF mgmt configuration
1130 mgmt_access = {}
1131 if vnfd["mgmt-interface"].get("vdu-id"):
1132 if vnfd["mgmt-interface"]["vdu-id"] not in vdu_id2uuid:
tiernob2880eb2017-10-04 15:04:53 +02001133 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1134 "'{vdu}'. Reference to a non-existing vdu".format(
1135 vnf=vnfd["id"], vdu=vnfd["mgmt-interface"]["vdu-id"]),
1136 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001137 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
1138 if vnfd["mgmt-interface"].get("ip-address"):
1139 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1140 if vnfd["mgmt-interface"].get("cp"):
1141 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
tiernob2880eb2017-10-04 15:04:53 +02001142 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. "
1143 "Reference to a non-existing connection-point".format(
1144 vnf=vnfd["id"], cp=vnfd["mgmt-interface"]["cp"]),
1145 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02001146 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1147 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
tiernoe2ff1ce2017-11-02 17:01:10 +01001148 # mark this interface as of type mgmt
1149 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
1150
tiernoa9550202017-09-22 13:31:35 +02001151 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
tiernof1ba57e2017-09-07 12:23:19 +02001152 "default-user", 64)
gcalvinoe580c7d2017-09-22 14:09:51 +02001153
tiernof1ba57e2017-09-07 12:23:19 +02001154 if default_user:
1155 mgmt_access["default_user"] = default_user
gcalvinoe580c7d2017-09-22 14:09:51 +02001156 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1157 "required", 6)
1158 if required:
1159 mgmt_access["required"] = required
1160
tiernof1ba57e2017-09-07 12:23:19 +02001161 if mgmt_access:
1162 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1163
gcalvinoe580c7d2017-09-22 14:09:51 +02001164
1165
tiernof1ba57e2017-09-07 12:23:19 +02001166 db_vnfs.append(db_vnf)
1167 db_tables=[
1168 {"vnfs": db_vnfs},
1169 {"nets": db_nets},
1170 {"images": db_images},
1171 {"flavors": db_flavors},
1172 {"vms": db_vms},
1173 {"interfaces": db_interfaces},
1174 ]
1175
1176 logger.debug("create_vnf Deployment done vnfDict: %s",
1177 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1178 mydb.new_rows(db_tables, uuid_list)
1179 return vnfd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02001180 except NfvoException:
1181 raise
tiernof1ba57e2017-09-07 12:23:19 +02001182 except Exception as e:
1183 logger.error("Exception {}".format(e))
1184 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1185
1186
tierno7edb6752016-03-21 17:37:52 +01001187def new_vnf(mydb, tenant_id, vnf_descriptor):
1188 global global_config
tierno42026a02017-02-10 15:13:40 +01001189
tierno7edb6752016-03-21 17:37:52 +01001190 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001191 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +01001192 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001193 vims = {}
tierno7edb6752016-03-21 17:37:52 +01001194 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001195 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001196 if "tenant_id" in vnf_descriptor["vnf"]:
1197 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001198 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1199 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001200 else:
1201 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1202 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001203 if global_config["auto_push_VNF_to_VIMs"]:
1204 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001205
1206 # Step 4. Review the descriptor and add missing fields
1207 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +02001208 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +01001209 vnf_name = vnf_descriptor['vnf']['name']
1210 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1211 if "physical" in vnf_descriptor['vnf']:
1212 del vnf_descriptor['vnf']['physical']
1213 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001214
tierno42026a02017-02-10 15:13:40 +01001215 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001216 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1217 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001218
tierno7edb6752016-03-21 17:37:52 +01001219 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1220 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1221 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +01001222 try:
tiernof97fd272016-07-11 14:32:37 +02001223 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001224 for vnfc in vnf_descriptor['vnf']['VNFC']:
1225 VNFCitem={}
1226 VNFCitem["name"] = vnfc['name']
mirabal29356312017-07-27 12:21:22 +02001227 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
tierno7edb6752016-03-21 17:37:52 +01001228 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001229
tiernof97fd272016-07-11 14:32:37 +02001230 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001231
tierno7edb6752016-03-21 17:37:52 +01001232 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001233 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 +01001234 myflavorDict["description"] = VNFCitem["description"]
1235 myflavorDict["ram"] = vnfc.get("ram", 0)
1236 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1237 myflavorDict["disk"] = vnfc.get("disk", 1)
1238 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001239
tierno7edb6752016-03-21 17:37:52 +01001240 devices = vnfc.get("devices")
1241 if devices != None:
1242 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001243
tierno7edb6752016-03-21 17:37:52 +01001244 # TODO:
1245 # 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 +01001246 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1247
tierno7edb6752016-03-21 17:37:52 +01001248 # Previous code has been commented
1249 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1250 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1251 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1252 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1253 #else:
1254 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1255 # if result2:
1256 # print "Error creating flavor: unknown processor model. Rollback successful."
1257 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1258 # else:
1259 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1260 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001261
tierno7edb6752016-03-21 17:37:52 +01001262 if 'numas' in vnfc and len(vnfc['numas'])>0:
1263 myflavorDict['extended']['numas'] = vnfc['numas']
1264
1265 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001266
tierno7edb6752016-03-21 17:37:52 +01001267 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +02001268 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +01001269
tiernof97fd272016-07-11 14:32:37 +02001270 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +01001271 VNFCitem["flavor_id"] = flavor_id
1272 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001273
tiernof97fd272016-07-11 14:32:37 +02001274 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +01001275 # Step 6.3 New images are created in the VIM
1276 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001277 #This "for" loop might be integrated with the previous one
tierno7edb6752016-03-21 17:37:52 +01001278 #In case this integration is made, the VNFCDict might become a VNFClist.
1279 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +02001280 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001281 image_dict={}
1282 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1283 image_dict['universal_name']=vnfc.get('image name')
1284 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1285 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001286 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001287 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +01001288 image_metadata_dict = vnfc.get('image metadata', None)
1289 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001290 if image_metadata_dict is not None:
tierno7edb6752016-03-21 17:37:52 +01001291 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1292 image_dict['metadata']=image_metadata_str
1293 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +02001294 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1295 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +01001296 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001297 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001298 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001299 if vnfc.get("boot-data"):
1300 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +01001301
tierno42026a02017-02-10 15:13:40 +01001302
tiernof97fd272016-07-11 14:32:37 +02001303 # Step 7. Storing the VNF descriptor in the repository
1304 if "descriptor" not in vnf_descriptor["vnf"]:
1305 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001306
tiernof97fd272016-07-11 14:32:37 +02001307 # Step 8. Adding the VNF to the NFVO DB
1308 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1309 return vnf_id
1310 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +01001311 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +02001312 if isinstance(e, db_base_Exception):
1313 error_text = "Exception at database"
1314 elif isinstance(e, KeyError):
1315 error_text = "KeyError exception "
1316 e.http_code = HTTP_Internal_Server_Error
1317 else:
1318 error_text = "Exception at VIM"
1319 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1320 #logger.error("start_scenario %s", error_text)
1321 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01001322
tiernob3d36742017-03-03 23:51:05 +01001323
garciadeblas9f8456e2016-09-05 05:02:59 +02001324def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1325 global global_config
tierno42026a02017-02-10 15:13:40 +01001326
garciadeblas9f8456e2016-09-05 05:02:59 +02001327 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +01001328 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +02001329 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +01001330 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +02001331 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001332 check_tenant(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001333 if "tenant_id" in vnf_descriptor["vnf"]:
1334 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1335 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1336 HTTP_Unauthorized)
1337 else:
1338 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1339 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +01001340 if global_config["auto_push_VNF_to_VIMs"]:
1341 vims = get_vim(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001342
1343 # Step 4. Review the descriptor and add missing fields
1344 #print vnf_descriptor
1345 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1346 vnf_name = vnf_descriptor['vnf']['name']
1347 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1348 if "physical" in vnf_descriptor['vnf']:
1349 del vnf_descriptor['vnf']['physical']
1350 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +01001351
tierno42026a02017-02-10 15:13:40 +01001352 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
garciadeblas9f8456e2016-09-05 05:02:59 +02001353 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1354 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno42026a02017-02-10 15:13:40 +01001355
garciadeblas9f8456e2016-09-05 05:02:59 +02001356 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1357 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1358 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1359 try:
1360 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1361 for vnfc in vnf_descriptor['vnf']['VNFC']:
1362 VNFCitem={}
1363 VNFCitem["name"] = vnfc['name']
1364 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
tierno42026a02017-02-10 15:13:40 +01001365
garciadeblas9f8456e2016-09-05 05:02:59 +02001366 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno42026a02017-02-10 15:13:40 +01001367
garciadeblas9f8456e2016-09-05 05:02:59 +02001368 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +02001369 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 +02001370 myflavorDict["description"] = VNFCitem["description"]
1371 myflavorDict["ram"] = vnfc.get("ram", 0)
1372 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1373 myflavorDict["disk"] = vnfc.get("disk", 1)
1374 myflavorDict["extended"] = {}
tierno42026a02017-02-10 15:13:40 +01001375
garciadeblas9f8456e2016-09-05 05:02:59 +02001376 devices = vnfc.get("devices")
1377 if devices != None:
1378 myflavorDict["extended"]["devices"] = devices
tierno42026a02017-02-10 15:13:40 +01001379
garciadeblas9f8456e2016-09-05 05:02:59 +02001380 # TODO:
1381 # 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 +01001382 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1383
garciadeblas9f8456e2016-09-05 05:02:59 +02001384 # Previous code has been commented
1385 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1386 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1387 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1388 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1389 #else:
1390 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1391 # if result2:
1392 # print "Error creating flavor: unknown processor model. Rollback successful."
1393 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1394 # else:
1395 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1396 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
tierno42026a02017-02-10 15:13:40 +01001397
garciadeblas9f8456e2016-09-05 05:02:59 +02001398 if 'numas' in vnfc and len(vnfc['numas'])>0:
1399 myflavorDict['extended']['numas'] = vnfc['numas']
1400
1401 #print myflavorDict
tierno42026a02017-02-10 15:13:40 +01001402
garciadeblas9f8456e2016-09-05 05:02:59 +02001403 # Step 6.2 New flavors are created in the VIM
1404 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1405
1406 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1407 VNFCitem["flavor_id"] = flavor_id
1408 VNFCDict[vnfc['name']] = VNFCitem
tierno42026a02017-02-10 15:13:40 +01001409
garciadeblas9f8456e2016-09-05 05:02:59 +02001410 logger.debug("Creating new images in the VIM for each VNFC")
1411 # Step 6.3 New images are created in the VIM
1412 #For each VNFC, we must create the appropriate image.
tierno42026a02017-02-10 15:13:40 +01001413 #This "for" loop might be integrated with the previous one
garciadeblas9f8456e2016-09-05 05:02:59 +02001414 #In case this integration is made, the VNFCDict might become a VNFClist.
1415 for vnfc in vnf_descriptor['vnf']['VNFC']:
1416 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +02001417 image_dict={}
1418 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1419 image_dict['universal_name']=vnfc.get('image name')
1420 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1421 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +01001422 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +02001423 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +02001424 image_metadata_dict = vnfc.get('image metadata', None)
1425 image_metadata_str = None
tierno42026a02017-02-10 15:13:40 +01001426 if image_metadata_dict is not None:
garciadeblas9f8456e2016-09-05 05:02:59 +02001427 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1428 image_dict['metadata']=image_metadata_str
1429 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1430 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1431 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1432 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +02001433 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno8e690322017-08-10 15:58:50 +02001434 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
tierno36c0b172017-01-12 18:32:28 +01001435 if vnfc.get("boot-data"):
1436 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +02001437
garciadeblas9f8456e2016-09-05 05:02:59 +02001438 # Step 7. Storing the VNF descriptor in the repository
1439 if "descriptor" not in vnf_descriptor["vnf"]:
1440 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01001441
garciadeblas9f8456e2016-09-05 05:02:59 +02001442 # Step 8. Adding the VNF to the NFVO DB
1443 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1444 return vnf_id
1445 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1446 _, message = rollback(mydb, vims, rollback_list)
1447 if isinstance(e, db_base_Exception):
1448 error_text = "Exception at database"
1449 elif isinstance(e, KeyError):
1450 error_text = "KeyError exception "
1451 e.http_code = HTTP_Internal_Server_Error
1452 else:
1453 error_text = "Exception at VIM"
1454 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1455 #logger.error("start_scenario %s", error_text)
1456 raise NfvoException(error_text, e.http_code)
1457
tiernob3d36742017-03-03 23:51:05 +01001458
tierno7edb6752016-03-21 17:37:52 +01001459def get_vnf_id(mydb, tenant_id, vnf_id):
1460 #check valid tenant_id
tierno42026a02017-02-10 15:13:40 +01001461 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001462 #obtain data
1463 where_or = {}
1464 if tenant_id != "any":
1465 where_or["tenant_id"] = tenant_id
1466 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001467 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1468
tiernof1ba57e2017-09-07 12:23:19 +02001469 vnf_id = vnf["uuid"]
1470 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +02001471 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +01001472 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1473 data={'vnf' : filtered_content}
1474 #GET VM
tiernof97fd272016-07-11 14:32:37 +02001475 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tiernof1ba57e2017-09-07 12:23:19 +02001476 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1477 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +01001478 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001479 if len(content)==0:
1480 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno36c0b172017-01-12 18:32:28 +01001481 # change boot_data into boot-data
1482 for vm in content:
1483 if vm.get("boot_data"):
1484 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1485 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +01001486
1487 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001488 #TODO: GET all the information from a VNFC and include it in the output.
tierno42026a02017-02-10 15:13:40 +01001489
tierno7edb6752016-03-21 17:37:52 +01001490 #GET NET
tierno42026a02017-02-10 15:13:40 +01001491 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +01001492 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1493 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +02001494 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +02001495
1496 #GET ip-profile for each net
1497 for net in data['vnf']['nets']:
1498 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1499 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1500 WHERE={'net_id': net["uuid"]} )
1501 if len(ipprofiles)==1:
1502 net["ip_profile"] = ipprofiles[0]
1503 elif len(ipprofiles)>1:
1504 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 +01001505
1506
garciadeblas9f8456e2016-09-05 05:02:59 +02001507 #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 +01001508
garciadeblas9f8456e2016-09-05 05:02:59 +02001509 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +02001510 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 +01001511 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1512 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
tierno3fcfdb72017-10-24 07:48:24 +02001513 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001514 #print content
tiernof97fd272016-07-11 14:32:37 +02001515 data['vnf']['external-connections'] = content
tierno42026a02017-02-10 15:13:40 +01001516
tiernof97fd272016-07-11 14:32:37 +02001517 return data
tierno7edb6752016-03-21 17:37:52 +01001518
1519
1520def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1521 # Check tenant exist
1522 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001523 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001524 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +02001525 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001526 else:
1527 vims={}
1528
1529 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1530 where_or = {}
1531 if tenant_id != "any":
1532 where_or["tenant_id"] = tenant_id
1533 where_or["public"] = True
tierno42026a02017-02-10 15:13:40 +01001534 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 +02001535 vnf_id = vnf["uuid"]
tierno42026a02017-02-10 15:13:40 +01001536
tierno7edb6752016-03-21 17:37:52 +01001537 # "Getting the list of flavors and tenants of the VNF"
tierno42026a02017-02-10 15:13:40 +01001538 flavorList = get_flavorlist(mydb, vnf_id)
tiernof97fd272016-07-11 14:32:37 +02001539 if len(flavorList)==0:
1540 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001541
tiernof97fd272016-07-11 14:32:37 +02001542 imageList = get_imagelist(mydb, vnf_id)
1543 if len(imageList)==0:
1544 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno42026a02017-02-10 15:13:40 +01001545
tiernof97fd272016-07-11 14:32:37 +02001546 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1547 if deleted == 0:
1548 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno42026a02017-02-10 15:13:40 +01001549
tierno7edb6752016-03-21 17:37:52 +01001550 undeletedItems = []
1551 for flavor in flavorList:
1552 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +02001553 try:
1554 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1555 if len(c) > 0:
1556 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1557 continue
1558 #flavor not used, must be deleted
1559 #delelte at VIM
1560 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
tierno7edb6752016-03-21 17:37:52 +01001561 for flavor_vim in c:
tierno868220c2017-09-26 00:11:05 +02001562 if flavor_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +01001563 continue
1564 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
1565 continue
1566 myvim=vims[ flavor_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001567 try:
1568 myvim.delete_flavor(flavor_vim["vim_id"])
1569 except vimconn.vimconnNotFoundException as e:
1570 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
1571 except vimconn.vimconnException as e:
1572 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1573 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
1574 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001575 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1576 mydb.delete_row_by_id('flavors', flavor)
1577 except db_base_Exception as e:
1578 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno7edb6752016-03-21 17:37:52 +01001579 undeletedItems.append("flavor %s" % flavor)
tiernof97fd272016-07-11 14:32:37 +02001580
tierno42026a02017-02-10 15:13:40 +01001581
tierno7edb6752016-03-21 17:37:52 +01001582 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +02001583 try:
1584 #check if image is used by other vnf
1585 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
1586 if len(c) > 0:
1587 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1588 continue
1589 #image not used, must be deleted
1590 #delelte at VIM
1591 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +01001592 for image_vim in c:
tierno868220c2017-09-26 00:11:05 +02001593 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +01001594 continue
1595 if image_vim['created']=='false': #skip this image because not created by openmano
1596 continue
1597 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +02001598 try:
1599 myvim.delete_image(image_vim["vim_id"])
1600 except vimconn.vimconnNotFoundException as e:
1601 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1602 except vimconn.vimconnException as e:
1603 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1604 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1605 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +02001606 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1607 mydb.delete_row_by_id('images', image)
1608 except db_base_Exception as e:
1609 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +01001610 undeletedItems.append("image %s" % image)
1611
tiernof97fd272016-07-11 14:32:37 +02001612 return vnf_id + " " + vnf["name"]
tierno42026a02017-02-10 15:13:40 +01001613 #if undeletedItems:
tiernof97fd272016-07-11 14:32:37 +02001614 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +01001615
tiernob3d36742017-03-03 23:51:05 +01001616
tierno7edb6752016-03-21 17:37:52 +01001617def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1618 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1619 if result < 0:
1620 return result, vims
1621 elif result == 0:
1622 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
1623 myvim = vims.values()[0]
1624 result,servers = myvim.get_hosts_info()
1625 if result < 0:
1626 return result, servers
1627 topology = {'name':myvim['name'] , 'servers': servers}
1628 return result, topology
1629
tiernob3d36742017-03-03 23:51:05 +01001630
tierno7edb6752016-03-21 17:37:52 +01001631def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +02001632 vims = get_vim(mydb, nfvo_tenant_id)
1633 if len(vims) == 0:
1634 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
1635 elif len(vims)>1:
1636 #print "nfvo.datacenter_action() error. Several datacenters found"
1637 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001638 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +02001639 try:
1640 hosts = myvim.get_hosts()
1641 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01001642
tiernof97fd272016-07-11 14:32:37 +02001643 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1644 for host in hosts:
1645 server={'name':host['name'], 'vms':[]}
1646 for vm in host['instances']:
1647 #get internal name and model
tierno42026a02017-02-10 15:13:40 +01001648 try:
tiernof97fd272016-07-11 14:32:37 +02001649 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1650 WHERE={'vim_vm_id':vm['id']} )
1651 if len(c) == 0:
1652 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1653 continue
1654 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
tierno42026a02017-02-10 15:13:40 +01001655
tiernof97fd272016-07-11 14:32:37 +02001656 except db_base_Exception as e:
1657 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1658 datacenter['Datacenters'][0]['servers'].append(server)
1659 #return -400, "en construccion"
tierno42026a02017-02-10 15:13:40 +01001660
tiernof97fd272016-07-11 14:32:37 +02001661 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1662 return datacenter
1663 except vimconn.vimconnException as e:
1664 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001665
tiernob3d36742017-03-03 23:51:05 +01001666
tierno7edb6752016-03-21 17:37:52 +01001667def new_scenario(mydb, tenant_id, topo):
1668
1669# result, vims = get_vim(mydb, tenant_id)
1670# if result < 0:
1671# return result, vims
1672#1: parse input
1673 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001674 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001675 if "tenant_id" in topo:
1676 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001677 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1678 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001679 else:
1680 tenant_id=None
1681
tierno42026a02017-02-10 15:13:40 +01001682#1.1: get VNFs and external_networks (other_nets).
tierno7edb6752016-03-21 17:37:52 +01001683 vnfs={}
1684 other_nets={} #external_networks, bridge_networks and data_networkds
1685 nodes = topo['topology']['nodes']
1686 for k in nodes.keys():
1687 if nodes[k]['type'] == 'VNF':
1688 vnfs[k] = nodes[k]
1689 vnfs[k]['ifaces'] = {}
tierno42026a02017-02-10 15:13:40 +01001690 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
tierno7edb6752016-03-21 17:37:52 +01001691 other_nets[k] = nodes[k]
1692 other_nets[k]['external']=True
tierno42026a02017-02-10 15:13:40 +01001693 elif nodes[k]['type'] == 'network':
tierno7edb6752016-03-21 17:37:52 +01001694 other_nets[k] = nodes[k]
1695 other_nets[k]['external']=False
tierno42026a02017-02-10 15:13:40 +01001696
tierno7edb6752016-03-21 17:37:52 +01001697
1698#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1699 for name,vnf in vnfs.items():
tierno3fcfdb72017-10-24 07:48:24 +02001700 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01001701 error_text = ""
1702 error_pos = "'topology':'nodes':'" + name + "'"
1703 if 'vnf_id' in vnf:
1704 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001705 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001706 if 'VNF model' in vnf:
1707 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001708 where['name'] = vnf['VNF model']
tierno3fcfdb72017-10-24 07:48:24 +02001709 if len(where) == 1:
tiernof97fd272016-07-11 14:32:37 +02001710 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01001711
tiernocea279c2016-07-18 12:36:49 +02001712 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1713 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02001714 WHERE=where)
tiernof97fd272016-07-11 14:32:37 +02001715 if len(vnf_db)==0:
1716 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1717 elif len(vnf_db)>1:
1718 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001719 vnf['uuid']=vnf_db[0]['uuid']
1720 vnf['description']=vnf_db[0]['description']
1721 #get external interfaces
tierno42026a02017-02-10 15:13:40 +01001722 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1723 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 +02001724 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001725 for ext_iface in ext_ifaces:
1726 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1727
1728#1.4 get list of connections
1729 conections = topo['topology']['connections']
1730 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001731 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001732 for k in conections.keys():
1733 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1734 ifaces_list = conections[k]['nodes'].items()
1735 elif type(conections[k]['nodes'])==list: #list with dictionary
1736 ifaces_list=[]
1737 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1738 for k2 in conection_pair_list:
1739 ifaces_list += k2
1740
1741 con_type = conections[k].get("type", "link")
1742 if con_type != "link":
1743 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001744 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001745 other_nets[k] = {'external': False}
1746 if conections[k].get("graph"):
1747 other_nets[k]["graph"] = conections[k]["graph"]
1748 ifaces_list.append( (k, None) )
1749
tierno42026a02017-02-10 15:13:40 +01001750
tierno7edb6752016-03-21 17:37:52 +01001751 if con_type == "external_network":
1752 other_nets[k]['external'] = True
1753 if conections[k].get("model"):
1754 other_nets[k]["model"] = conections[k]["model"]
1755 else:
1756 other_nets[k]["model"] = k
tierno42026a02017-02-10 15:13:40 +01001757 if con_type == "dataplane_net" or con_type == "bridge_net":
tierno7edb6752016-03-21 17:37:52 +01001758 other_nets[k]["model"] = con_type
tierno42026a02017-02-10 15:13:40 +01001759
tiernoefd80c92016-09-16 14:17:46 +02001760 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001761 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)
1762 #print set(ifaces_list)
1763 #check valid VNF and iface names
1764 for iface in ifaces_list:
1765 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001766 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1767 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001768 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001769 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1770 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001771
1772#1.5 unify connections from the pair list to a consolidated list
1773 index=0
1774 while index < len(conections_list):
1775 index2 = index+1
1776 while index2 < len(conections_list):
1777 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1778 conections_list[index] |= conections_list[index2]
1779 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001780 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001781 else:
1782 index2 += 1
1783 conections_list[index] = list(conections_list[index]) # from set to list again
1784 index += 1
1785 #for k in conections_list:
1786 # print k
tierno42026a02017-02-10 15:13:40 +01001787
tierno7edb6752016-03-21 17:37:52 +01001788
1789
1790#1.6 Delete non external nets
1791# for k in other_nets.keys():
1792# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1793# for con in conections_list:
1794# delete_indexes=[]
1795# for index in range(0,len(con)):
1796# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1797# for index in delete_indexes:
1798# del con[index]
1799# del other_nets[k]
1800#1.7: Check external_ports are present at database table datacenter_nets
1801 for k,net in other_nets.items():
1802 error_pos = "'topology':'nodes':'" + k + "'"
1803 if net['external']==False:
1804 if 'name' not in net:
1805 net['name']=k
1806 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001807 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001808 if net['model']=='bridge_net':
1809 net['type']='bridge';
1810 elif net['model']=='dataplane_net':
1811 net['type']='data';
1812 else:
tiernof97fd272016-07-11 14:32:37 +02001813 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001814 else: #external
1815#IF we do not want to check that external network exist at datacenter
1816 pass
tierno42026a02017-02-10 15:13:40 +01001817#ELSE
tierno7edb6752016-03-21 17:37:52 +01001818# error_text = ""
1819# WHERE_={}
1820# if 'net_id' in net:
1821# error_text += " 'net_id' " + net['net_id']
1822# WHERE_['uuid'] = net['net_id']
1823# if 'model' in net:
1824# error_text += " 'model' " + net['model']
1825# WHERE_['name'] = net['model']
1826# if len(WHERE_) == 0:
1827# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1828# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1829# FROM='datacenter_nets', WHERE=WHERE_ )
1830# if r<0:
1831# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1832# elif r==0:
1833# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1834# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1835# elif r>1:
tierno42026a02017-02-10 15:13:40 +01001836# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1837# 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 +01001838# other_nets[k].update(net_db[0])
tierno42026a02017-02-10 15:13:40 +01001839#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001840 net_list={}
1841 net_nb=0 #Number of nets
1842 for con in conections_list:
1843 #check if this is connected to a external net
1844 other_net_index=-1
1845 #print
1846 #print "con", con
1847 for index in range(0,len(con)):
1848 #check if this is connected to a external net
1849 for net_key in other_nets.keys():
1850 if con[index][0]==net_key:
1851 if other_net_index>=0:
tierno42026a02017-02-10 15:13:40 +01001852 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 +02001853 #print "nfvo.new_scenario " + error_text
1854 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001855 else:
1856 other_net_index = index
1857 net_target = net_key
1858 break
1859 #print "other_net_index", other_net_index
1860 try:
1861 if other_net_index>=0:
1862 del con[other_net_index]
1863#IF we do not want to check that external network exist at datacenter
1864 if other_nets[net_target]['external'] :
1865 if "name" not in other_nets[net_target]:
1866 other_nets[net_target]['name'] = other_nets[net_target]['model']
1867 if other_nets[net_target]["type"] == "external_network":
1868 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1869 other_nets[net_target]["type"] = "data"
1870 else:
1871 other_nets[net_target]["type"] = "bridge"
tierno42026a02017-02-10 15:13:40 +01001872#ELSE
tierno7edb6752016-03-21 17:37:52 +01001873# if other_nets[net_target]['external'] :
1874# 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
1875# if type_=='data' and other_nets[net_target]['type']=="ptp":
1876# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1877# print "nfvo.new_scenario " + error_text
1878# return -HTTP_Bad_Request, error_text
tierno42026a02017-02-10 15:13:40 +01001879#ENDIF
tierno7edb6752016-03-21 17:37:52 +01001880 for iface in con:
1881 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1882 else:
1883 #create a net
1884 net_type_bridge=False
1885 net_type_data=False
1886 net_target = "__-__net"+str(net_nb)
tierno42026a02017-02-10 15:13:40 +01001887 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
tiernoefd80c92016-09-16 14:17:46 +02001888 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno42026a02017-02-10 15:13:40 +01001889 'external':False}
tierno7edb6752016-03-21 17:37:52 +01001890 for iface in con:
1891 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1892 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1893 if iface_type=='mgmt' or iface_type=='bridge':
1894 net_type_bridge = True
1895 else:
1896 net_type_data = True
1897 if net_type_bridge and net_type_data:
1898 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 +02001899 #print "nfvo.new_scenario " + error_text
1900 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001901 elif net_type_bridge:
1902 type_='bridge'
1903 else:
1904 type_='data' if len(con)>2 else 'ptp'
1905 net_list[net_target]['type'] = type_
1906 net_nb+=1
1907 except Exception:
1908 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001909 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01001910 #raise e
tiernof97fd272016-07-11 14:32:37 +02001911 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001912
1913#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
tierno42026a02017-02-10 15:13:40 +01001914 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02001915 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01001916 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
tierno42026a02017-02-10 15:13:40 +01001917 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02001918 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01001919 add_mgmt_net = False
1920 for vnf in vnfs.values():
1921 for iface in vnf['ifaces'].values():
1922 if iface['type']=='mgmt' and 'net_key' not in iface:
1923 #iface not connected
1924 iface['net_key'] = 'mgmt'
1925 add_mgmt_net = True
1926 if add_mgmt_net and 'mgmt' not in net_list:
1927 net_list['mgmt']=mgmt_net[0]
1928 net_list['mgmt']['external']=True
1929 net_list['mgmt']['graph']={'visible':False}
1930
1931 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02001932 #print
1933 #print 'net_list', net_list
1934 #print
1935 #print 'vnfs', vnfs
1936 #print
tierno7edb6752016-03-21 17:37:52 +01001937
1938#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02001939 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02001940 'tenant_id':tenant_id, 'name':topo['name'],
1941 'description':topo.get('description',topo['name']),
1942 'public': topo.get('public', False)
1943 })
tierno42026a02017-02-10 15:13:40 +01001944
tiernof97fd272016-07-11 14:32:37 +02001945 return c
tierno7edb6752016-03-21 17:37:52 +01001946
tiernob3d36742017-03-03 23:51:05 +01001947
tierno5bb59dc2017-02-13 14:53:54 +01001948def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
1949 """ This creates a new scenario for version 0.2 and 0.3"""
tierno392f2852016-05-13 12:28:55 +02001950 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01001951 if tenant_id != "any":
tierno42026a02017-02-10 15:13:40 +01001952 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001953 if "tenant_id" in scenario:
1954 if scenario["tenant_id"] != tenant_id:
tierno5bb59dc2017-02-13 14:53:54 +01001955 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02001956 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1957 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001958 else:
1959 tenant_id=None
1960
tierno5bb59dc2017-02-13 14:53:54 +01001961 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
tierno7edb6752016-03-21 17:37:52 +01001962 for name,vnf in scenario["vnfs"].iteritems():
tierno3fcfdb72017-10-24 07:48:24 +02001963 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
tierno7edb6752016-03-21 17:37:52 +01001964 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001965 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01001966 if 'vnf_id' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01001967 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001968 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02001969 if 'vnf_name' in vnf:
tierno5bb59dc2017-02-13 14:53:54 +01001970 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02001971 where['name'] = vnf['vnf_name']
tierno3fcfdb72017-10-24 07:48:24 +02001972 if len(where) == 1:
garciadeblas71781ea2016-09-19 14:41:59 +02001973 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tierno5bb59dc2017-02-13 14:53:54 +01001974 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
tiernocea279c2016-07-18 12:36:49 +02001975 FROM='vnfs',
tierno3fcfdb72017-10-24 07:48:24 +02001976 WHERE=where)
tierno5bb59dc2017-02-13 14:53:54 +01001977 if len(vnf_db) == 0:
tiernof97fd272016-07-11 14:32:37 +02001978 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
tierno5bb59dc2017-02-13 14:53:54 +01001979 elif len(vnf_db) > 1:
tiernof97fd272016-07-11 14:32:37 +02001980 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno5bb59dc2017-02-13 14:53:54 +01001981 vnf['uuid'] = vnf_db[0]['uuid']
1982 vnf['description'] = vnf_db[0]['description']
tierno7edb6752016-03-21 17:37:52 +01001983 vnf['ifaces'] = {}
tierno5bb59dc2017-02-13 14:53:54 +01001984 # get external interfaces
1985 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
1986 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 +02001987 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
tierno7edb6752016-03-21 17:37:52 +01001988 for ext_iface in ext_ifaces:
tierno5bb59dc2017-02-13 14:53:54 +01001989 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
1990 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
tierno7edb6752016-03-21 17:37:52 +01001991
tierno5bb59dc2017-02-13 14:53:54 +01001992 # 2: Insert net_key and ip_address at every vnf interface
1993 for net_name, net in scenario["networks"].items():
1994 net_type_bridge = False
1995 net_type_data = False
tierno7edb6752016-03-21 17:37:52 +01001996 for iface_dict in net["interfaces"]:
tierno5bb59dc2017-02-13 14:53:54 +01001997 if version == "0.2":
1998 temp_dict = iface_dict
1999 ip_address = None
2000 elif version == "0.3":
2001 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2002 ip_address = iface_dict.get('ip_address', None)
2003 for vnf, iface in temp_dict.items():
tierno7edb6752016-03-21 17:37:52 +01002004 if vnf not in scenario["vnfs"]:
tierno5bb59dc2017-02-13 14:53:54 +01002005 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2006 net_name, vnf)
2007 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002008 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002009 if iface not in scenario["vnfs"][vnf]['ifaces']:
tierno5bb59dc2017-02-13 14:53:54 +01002010 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2011 .format(net_name, iface)
2012 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002013 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002014 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
tierno5bb59dc2017-02-13 14:53:54 +01002015 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2016 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2017 # logger.debug("nfvo.new_scenario_v02 " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002018 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002019 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
tierno5bb59dc2017-02-13 14:53:54 +01002020 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
tierno7edb6752016-03-21 17:37:52 +01002021 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
tierno5bb59dc2017-02-13 14:53:54 +01002022 if iface_type == 'mgmt' or iface_type == 'bridge':
tierno7edb6752016-03-21 17:37:52 +01002023 net_type_bridge = True
2024 else:
2025 net_type_data = True
tierno5bb59dc2017-02-13 14:53:54 +01002026
tierno7edb6752016-03-21 17:37:52 +01002027 if net_type_bridge and net_type_data:
tierno5bb59dc2017-02-13 14:53:54 +01002028 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2029 .format(net_name)
2030 # logger.debug("nfvo.new_scenario " + error_text)
tiernof97fd272016-07-11 14:32:37 +02002031 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002032 elif net_type_bridge:
tierno5bb59dc2017-02-13 14:53:54 +01002033 type_ = 'bridge'
tierno7edb6752016-03-21 17:37:52 +01002034 else:
tierno5bb59dc2017-02-13 14:53:54 +01002035 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2036
2037 if net.get("implementation"): # for v0.3
2038 if type_ == "bridge" and net["implementation"] == "underlay":
2039 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2040 "'network':'{}'".format(net_name)
2041 # logger.debug(error_text)
2042 raise NfvoException(error_text, HTTP_Bad_Request)
2043 elif type_ != "bridge" and net["implementation"] == "overlay":
2044 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2045 "'network':'{}'".format(net_name)
2046 # logger.debug(error_text)
2047 raise NfvoException(error_text, HTTP_Bad_Request)
2048 net.pop("implementation")
2049 if "type" in net and version == "0.3": # for v0.3
2050 if type_ == "data" and net["type"] == "e-line":
2051 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2052 "'e-line' at 'network':'{}'".format(net_name)
2053 # logger.debug(error_text)
2054 raise NfvoException(error_text, HTTP_Bad_Request)
2055 elif type_ == "ptp" and net["type"] == "e-lan":
2056 type_ = "data"
2057
tierno7edb6752016-03-21 17:37:52 +01002058 net['type'] = type_
2059 net['name'] = net_name
2060 net['external'] = net.get('external', False)
2061
tierno5bb59dc2017-02-13 14:53:54 +01002062 # 3: insert at database
tierno7edb6752016-03-21 17:37:52 +01002063 scenario["nets"] = scenario["networks"]
2064 scenario['tenant_id'] = tenant_id
tierno5bb59dc2017-02-13 14:53:54 +01002065 scenario_id = mydb.new_scenario(scenario)
tiernof97fd272016-07-11 14:32:37 +02002066 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01002067
tiernob3d36742017-03-03 23:51:05 +01002068
tiernof1ba57e2017-09-07 12:23:19 +02002069def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2070 """
2071 Parses an OSM IM nsd_catalog and insert at DB
2072 :param mydb:
2073 :param tenant_id:
2074 :param nsd_descriptor:
2075 :return: The list of cretated NSD ids
2076 """
2077 try:
2078 mynsd = nsd_catalog.nsd()
tiernoa9550202017-09-22 13:31:35 +02002079 try:
2080 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd)
2081 except Exception as e:
tiernob2880eb2017-10-04 15:04:53 +02002082 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002083 db_scenarios = []
2084 db_sce_nets = []
2085 db_sce_vnfs = []
2086 db_sce_interfaces = []
2087 db_ip_profiles = []
2088 db_ip_profiles_index = 0
2089 uuid_list = []
2090 nsd_uuid_list = []
tiernob2880eb2017-10-04 15:04:53 +02002091 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2092 nsd = nsd_yang.get()
tiernof1ba57e2017-09-07 12:23:19 +02002093
2094 # table sceanrios
2095 scenario_uuid = str(uuid4())
2096 uuid_list.append(scenario_uuid)
2097 nsd_uuid_list.append(scenario_uuid)
2098 db_scenario = {
2099 "uuid": scenario_uuid,
2100 "osm_id": get_str(nsd, "id", 255),
2101 "name": get_str(nsd, "name", 255),
2102 "description": get_str(nsd, "description", 255),
2103 "tenant_id": tenant_id,
2104 "vendor": get_str(nsd, "vendor", 255),
2105 "short_name": get_str(nsd, "short-name", 255),
2106 "descriptor": str(nsd_descriptor)[:60000],
2107 }
2108 db_scenarios.append(db_scenario)
2109
2110 # table sce_vnfs (constituent-vnfd)
2111 vnf_index2scevnf_uuid = {}
2112 vnf_index2vnf_uuid = {}
2113 for vnf in nsd.get("constituent-vnfd").itervalues():
2114 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2115 'tenant_id': tenant_id})
2116 if not existing_vnf:
tiernob2880eb2017-10-04 15:04:53 +02002117 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2118 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2119 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
2120 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002121 sce_vnf_uuid = str(uuid4())
2122 uuid_list.append(sce_vnf_uuid)
2123 db_sce_vnf = {
2124 "uuid": sce_vnf_uuid,
2125 "scenario_id": scenario_uuid,
2126 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 5),
2127 "vnf_id": existing_vnf[0]["uuid"],
2128 "member_vnf_index": int(vnf["member-vnf-index"]),
2129 # TODO 'start-by-default': True
2130 }
2131 vnf_index2scevnf_uuid[int(vnf['member-vnf-index'])] = sce_vnf_uuid
2132 vnf_index2vnf_uuid[int(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
2133 db_sce_vnfs.append(db_sce_vnf)
2134
2135 # table ip_profiles (ip-profiles)
2136 ip_profile_name2db_table_index = {}
2137 for ip_profile in nsd.get("ip-profiles").itervalues():
2138 db_ip_profile = {
2139 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2140 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2141 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2142 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2143 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2144 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2145 }
2146 dns_list = []
2147 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2148 dns_list.append(str(dns.get("address")))
2149 db_ip_profile["dns_address"] = ";".join(dns_list)
2150 if ip_profile["ip-profile-params"].get('security-group'):
2151 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2152 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2153 db_ip_profiles_index += 1
2154 db_ip_profiles.append(db_ip_profile)
2155
2156 # table sce_nets (internal-vld)
2157 for vld in nsd.get("vld").itervalues():
2158 sce_net_uuid = str(uuid4())
2159 uuid_list.append(sce_net_uuid)
2160 db_sce_net = {
2161 "uuid": sce_net_uuid,
2162 "name": get_str(vld, "name", 255),
2163 "scenario_id": scenario_uuid,
2164 # "type": #TODO
2165 "multipoint": not vld.get("type") == "ELINE",
2166 # "external": #TODO
2167 "description": get_str(vld, "description", 255),
2168 }
2169 # guess type of network
2170 if vld.get("mgmt-network"):
2171 db_sce_net["type"] = "bridge"
2172 db_sce_net["external"] = True
2173 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2174 db_sce_net["type"] = "data"
2175 else:
2176 db_sce_net["type"] = "bridge"
2177 db_sce_nets.append(db_sce_net)
2178
2179 # ip-profile, link db_ip_profile with db_sce_net
2180 if vld.get("ip-profile-ref"):
2181 ip_profile_name = vld.get("ip-profile-ref")
2182 if ip_profile_name not in ip_profile_name2db_table_index:
tiernob2880eb2017-10-04 15:04:53 +02002183 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2184 " Reference to a non-existing 'ip_profiles'".format(
2185 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
2186 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002187 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
2188
2189 # table sce_interfaces (vld:vnfd-connection-point-ref)
2190 for iface in vld.get("vnfd-connection-point-ref").itervalues():
2191 vnf_index = int(iface['member-vnf-index-ref'])
2192 # check correct parameters
2193 if vnf_index not in vnf_index2vnf_uuid:
tiernob2880eb2017-10-04 15:04:53 +02002194 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2195 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2196 "'nsd':'constituent-vnfd'".format(
2197 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
2198 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002199
2200 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2201 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2202 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2203 'external_name': get_str(iface, "vnfd-connection-point-ref",
2204 255)})
2205 if not existing_ifaces:
tiernob2880eb2017-10-04 15:04:53 +02002206 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2207 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2208 "connection-point name at VNFD '{}'".format(
2209 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2210 str(iface.get("vnfd-id-ref"))[:255]),
2211 HTTP_Bad_Request)
tiernof1ba57e2017-09-07 12:23:19 +02002212 interface_uuid = existing_ifaces[0]["uuid"]
2213 sce_interface_uuid = str(uuid4())
2214 uuid_list.append(sce_net_uuid)
2215 db_sce_interface = {
2216 "uuid": sce_interface_uuid,
2217 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2218 "sce_net_id": sce_net_uuid,
2219 "interface_id": interface_uuid,
2220 # "ip_address": #TODO
2221 }
2222 db_sce_interfaces.append(db_sce_interface)
2223
2224 db_tables = [
2225 {"scenarios": db_scenarios},
2226 {"sce_nets": db_sce_nets},
2227 {"ip_profiles": db_ip_profiles},
2228 {"sce_vnfs": db_sce_vnfs},
2229 {"sce_interfaces": db_sce_interfaces},
2230 ]
2231
2232 logger.debug("create_vnf Deployment done vnfDict: %s",
2233 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2234 mydb.new_rows(db_tables, uuid_list)
2235 return nsd_uuid_list
tiernob2880eb2017-10-04 15:04:53 +02002236 except NfvoException:
2237 raise
tiernof1ba57e2017-09-07 12:23:19 +02002238 except Exception as e:
2239 logger.error("Exception {}".format(e))
2240 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2241
2242
tierno7edb6752016-03-21 17:37:52 +01002243def edit_scenario(mydb, tenant_id, scenario_id, data):
2244 data["uuid"] = scenario_id
2245 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02002246 c = mydb.edit_scenario( data )
2247 return c
tierno7edb6752016-03-21 17:37:52 +01002248
tiernob3d36742017-03-03 23:51:05 +01002249
tierno7edb6752016-03-21 17:37:52 +01002250def 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 +02002251 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002252 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2253 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02002254 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01002255 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00002256
tierno7edb6752016-03-21 17:37:52 +01002257 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02002258 try:
2259 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tierno868220c2017-09-26 00:11:05 +02002260 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00002261 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02002262 scenarioDict['datacenter_id'] = datacenter_id
2263 #print '================scenarioDict======================='
2264 #print json.dumps(scenarioDict, indent=4)
2265 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno42026a02017-02-10 15:13:40 +01002266
tiernoae4a8d12016-07-08 12:30:39 +02002267 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2268 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002269
tiernoae4a8d12016-07-08 12:30:39 +02002270 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2271 auxNetDict['scenario'] = {}
tierno42026a02017-02-10 15:13:40 +01002272
tiernoae4a8d12016-07-08 12:30:39 +02002273 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2274 for sce_net in scenarioDict['nets']:
2275 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno42026a02017-02-10 15:13:40 +01002276
tiernoae4a8d12016-07-08 12:30:39 +02002277 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01002278 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02002279 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01002280 myNetDict = {}
2281 myNetDict["name"] = myNetName
2282 myNetDict["type"] = myNetType
2283 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002284 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01002285 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02002286 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02002287 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02002288 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02002289 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002290 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2291 sce_net['vim_id'] = network_id
2292 auxNetDict['scenario'][sce_net['uuid']] = network_id
2293 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002294 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02002295 else:
2296 if sce_net['vim_id'] == None:
2297 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2298 _, message = rollback(mydb, vims, rollbackList)
2299 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02002300 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02002301 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2302 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno42026a02017-02-10 15:13:40 +01002303
tiernoae4a8d12016-07-08 12:30:39 +02002304 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2305 #For each vnf net, we create it and we add it to instanceNetlist.
mirabal29356312017-07-27 12:21:22 +02002306
tiernoae4a8d12016-07-08 12:30:39 +02002307 for sce_vnf in scenarioDict['vnfs']:
2308 for net in sce_vnf['nets']:
2309 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
tierno42026a02017-02-10 15:13:40 +01002310
tiernoae4a8d12016-07-08 12:30:39 +02002311 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2312 myNetName = myNetName[0:255] #limit length
2313 myNetType = net['type']
2314 myNetDict = {}
2315 myNetDict["name"] = myNetName
2316 myNetDict["type"] = myNetType
2317 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02002318 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02002319 #print myNetDict
2320 #TODO:
2321 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02002322 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02002323 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2324 net['vim_id'] = network_id
2325 if sce_vnf['uuid'] not in auxNetDict:
2326 auxNetDict[sce_vnf['uuid']] = {}
2327 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2328 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002329 net["created"] = True
tierno42026a02017-02-10 15:13:40 +01002330
tiernoae4a8d12016-07-08 12:30:39 +02002331 #print "auxNetDict:"
2332 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002333
tiernoae4a8d12016-07-08 12:30:39 +02002334 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2335 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2336 i = 0
2337 for sce_vnf in scenarioDict['vnfs']:
tierno5a3273c2017-08-29 11:43:46 +02002338 vnf_availability_zones = []
mirabal29356312017-07-27 12:21:22 +02002339 for vm in sce_vnf['vms']:
2340 vm_av = vm.get('availability_zone')
tierno5a3273c2017-08-29 11:43:46 +02002341 if vm_av and vm_av not in vnf_availability_zones:
2342 vnf_availability_zones.append(vm_av)
2343
2344 # check if there is enough availability zones available at vim level.
2345 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2346 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2347 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
2348
tiernoae4a8d12016-07-08 12:30:39 +02002349 for vm in sce_vnf['vms']:
2350 i += 1
2351 myVMDict = {}
2352 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01002353 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02002354 #myVMDict['description'] = vm['description']
2355 myVMDict['description'] = myVMDict['name'][0:99]
2356 if not startvms:
2357 myVMDict['start'] = "no"
2358 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2359 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
tierno42026a02017-02-10 15:13:40 +01002360
tiernoae4a8d12016-07-08 12:30:39 +02002361 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002362 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno42026a02017-02-10 15:13:40 +01002363 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002364 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01002365
tiernoae4a8d12016-07-08 12:30:39 +02002366 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002367 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002368 if flavor_dict['extended']!=None:
2369 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tierno42026a02017-02-10 15:13:40 +01002370 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02002371 vm['vim_flavor_id'] = flavor_id
tierno42026a02017-02-10 15:13:40 +01002372
2373
tiernoae4a8d12016-07-08 12:30:39 +02002374 myVMDict['imageRef'] = vm['vim_image_id']
2375 myVMDict['flavorRef'] = vm['vim_flavor_id']
2376 myVMDict['networks'] = []
2377 for iface in vm['interfaces']:
2378 netDict = {}
2379 if iface['type']=="data":
2380 netDict['type'] = iface['model']
2381 elif "model" in iface and iface["model"]!=None:
2382 netDict['model']=iface['model']
2383 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2384 #discover type of interface looking at flavor
2385 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2386 for flavor_iface in numa.get('interfaces',[]):
2387 if flavor_iface.get('name') == iface['internal_name']:
2388 if flavor_iface['dedicated'] == 'yes':
2389 netDict['type']="PF" #passthrough
2390 elif flavor_iface['dedicated'] == 'no':
2391 netDict['type']="VF" #siov
2392 elif flavor_iface['dedicated'] == 'yes:sriov':
2393 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2394 netDict["mac_address"] = flavor_iface.get("mac_address")
2395 break;
2396 netDict["use"]=iface['type']
2397 if netDict["use"]=="data" and not netDict.get("type"):
2398 #print "netDict", netDict
2399 #print "iface", iface
2400 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'])
2401 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02002402 raise NfvoException(e_text + "After database migration some information is not available. \
2403 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02002404 else:
tiernof97fd272016-07-11 14:32:37 +02002405 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02002406 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2407 netDict["type"]="virtual"
2408 if "vpci" in iface and iface["vpci"] is not None:
2409 netDict['vpci'] = iface['vpci']
2410 if "mac" in iface and iface["mac"] is not None:
2411 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002412 if "port-security" in iface and iface["port-security"] is not None:
2413 netDict['port_security'] = iface['port-security']
2414 if "floating-ip" in iface and iface["floating-ip"] is not None:
2415 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02002416 netDict['name'] = iface['internal_name']
2417 if iface['net_id'] is None:
2418 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002419 #print iface
2420 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02002421 if vnf_iface['interface_id']==iface['uuid']:
2422 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2423 break
2424 else:
2425 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2426 #skip bridge ifaces not connected to any net
2427 #if 'net_id' not in netDict or netDict['net_id']==None:
2428 # continue
2429 myVMDict['networks'].append(netDict)
2430 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2431 #print myVMDict['name']
2432 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2433 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2434 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
mirabal29356312017-07-27 12:21:22 +02002435
2436 if 'availability_zone' in myVMDict:
tierno5a3273c2017-08-29 11:43:46 +02002437 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
mirabal29356312017-07-27 12:21:22 +02002438 else:
tierno5a3273c2017-08-29 11:43:46 +02002439 av_index = None
mirabal29356312017-07-27 12:21:22 +02002440
tierno98e909c2017-10-14 13:27:03 +02002441 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
mirabal29356312017-07-27 12:21:22 +02002442 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
tierno5a3273c2017-08-29 11:43:46 +02002443 availability_zone_index=av_index,
2444 availability_zone_list=vnf_availability_zones)
tiernoae4a8d12016-07-08 12:30:39 +02002445 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2446 vm['vim_id'] = vm_id
2447 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2448 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2449 for net in myVMDict['networks']:
2450 if "vim_id" in net:
2451 for iface in vm['interfaces']:
2452 if net["name"]==iface["internal_name"]:
2453 iface["vim_id"]=net["vim_id"]
2454 break
tierno42026a02017-02-10 15:13:40 +01002455
tiernoae4a8d12016-07-08 12:30:39 +02002456 logger.debug("start scenario Deployment done")
2457 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2458 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02002459 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2460 return mydb.get_instance_scenario(instance_id)
tierno42026a02017-02-10 15:13:40 +01002461
tiernof97fd272016-07-11 14:32:37 +02002462 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02002463 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002464 if isinstance(e, db_base_Exception):
2465 error_text = "Exception at database"
2466 else:
2467 error_text = "Exception at VIM"
2468 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2469 #logger.error("start_scenario %s", error_text)
2470 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002471
tierno36c0b172017-01-12 18:32:28 +01002472def unify_cloud_config(cloud_config_preserve, cloud_config):
tierno40e1bce2017-08-09 09:12:04 +02002473 """ join the cloud config information into cloud_config_preserve.
tierno36c0b172017-01-12 18:32:28 +01002474 In case of conflict cloud_config_preserve preserves
tierno40e1bce2017-08-09 09:12:04 +02002475 None is allowed
2476 """
tierno36c0b172017-01-12 18:32:28 +01002477 if not cloud_config_preserve and not cloud_config:
2478 return None
2479
2480 new_cloud_config = {"key-pairs":[], "users":[]}
2481 # key-pairs
2482 if cloud_config_preserve:
2483 for key in cloud_config_preserve.get("key-pairs", () ):
2484 if key not in new_cloud_config["key-pairs"]:
2485 new_cloud_config["key-pairs"].append(key)
2486 if cloud_config:
2487 for key in cloud_config.get("key-pairs", () ):
2488 if key not in new_cloud_config["key-pairs"]:
2489 new_cloud_config["key-pairs"].append(key)
2490 if not new_cloud_config["key-pairs"]:
2491 del new_cloud_config["key-pairs"]
2492
2493 # users
2494 if cloud_config:
2495 new_cloud_config["users"] += cloud_config.get("users", () )
2496 if cloud_config_preserve:
2497 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02002498 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01002499 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02002500 for index0 in range(0,len(users)):
2501 if index0 in index_to_delete:
2502 continue
2503 for index1 in range(index0+1,len(users)):
2504 if index1 in index_to_delete:
2505 continue
2506 if users[index0]["name"] == users[index1]["name"]:
2507 index_to_delete.append(index1)
2508 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01002509 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02002510 users[index0]["key-pairs"] = [key]
2511 elif key not in users[index0]["key-pairs"]:
2512 users[index0]["key-pairs"].append(key)
2513 index_to_delete.sort(reverse=True)
2514 for index in index_to_delete:
2515 del users[index]
tierno36c0b172017-01-12 18:32:28 +01002516 if not new_cloud_config["users"]:
2517 del new_cloud_config["users"]
2518
2519 #boot-data-drive
2520 if cloud_config and cloud_config.get("boot-data-drive") != None:
2521 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2522 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2523 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2524
2525 # user-data
tierno40e1bce2017-08-09 09:12:04 +02002526 new_cloud_config["user-data"] = []
2527 if cloud_config and cloud_config.get("user-data"):
2528 if isinstance(cloud_config["user-data"], list):
2529 new_cloud_config["user-data"] += cloud_config["user-data"]
2530 else:
2531 new_cloud_config["user-data"].append(cloud_config["user-data"])
2532 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2533 if isinstance(cloud_config_preserve["user-data"], list):
2534 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2535 else:
2536 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2537 if not new_cloud_config["user-data"]:
2538 del new_cloud_config["user-data"]
tierno36c0b172017-01-12 18:32:28 +01002539
2540 # config files
2541 new_cloud_config["config-files"] = []
2542 if cloud_config and cloud_config.get("config-files") != None:
2543 new_cloud_config["config-files"] += cloud_config["config-files"]
2544 if cloud_config_preserve:
2545 for file in cloud_config_preserve.get("config-files", ()):
2546 for index in range(0, len(new_cloud_config["config-files"])):
2547 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2548 new_cloud_config["config-files"][index] = file
2549 break
2550 else:
2551 new_cloud_config["config-files"].append(file)
2552 if not new_cloud_config["config-files"]:
2553 del new_cloud_config["config-files"]
2554 return new_cloud_config
2555
2556
tierno867ffe92017-03-27 12:50:34 +02002557def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
tiernob3d36742017-03-03 23:51:05 +01002558 datacenter_id = None
2559 datacenter_name = None
2560 thread = None
tierno867ffe92017-03-27 12:50:34 +02002561 try:
2562 if datacenter_tenant_id:
2563 thread_id = datacenter_tenant_id
2564 thread = vim_threads["running"].get(datacenter_tenant_id)
tiernob3d36742017-03-03 23:51:05 +01002565 else:
tierno867ffe92017-03-27 12:50:34 +02002566 where_={"td.nfvo_tenant_id": tenant_id}
2567 if datacenter_id_name:
2568 if utils.check_valid_uuid(datacenter_id_name):
2569 datacenter_id = datacenter_id_name
2570 where_["dt.datacenter_id"] = datacenter_id
2571 else:
2572 datacenter_name = datacenter_id_name
2573 where_["d.name"] = datacenter_name
2574 if datacenter_tenant_id:
2575 where_["dt.uuid"] = datacenter_tenant_id
2576 datacenters = mydb.get_rows(
2577 SELECT=("dt.uuid as datacenter_tenant_id",),
2578 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2579 "join datacenters as d on d.uuid=dt.datacenter_id",
2580 WHERE=where_)
2581 if len(datacenters) > 1:
2582 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2583 elif datacenters:
2584 thread_id = datacenters[0]["datacenter_tenant_id"]
2585 thread = vim_threads["running"].get(thread_id)
2586 if not thread:
2587 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2588 return thread_id, thread
2589 except db_base_Exception as e:
2590 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
tiernoa4e1a6e2016-08-31 14:19:40 +02002591
tiernof5755962017-07-13 15:44:34 +02002592
tiernoa15c4b92017-10-05 12:41:44 +02002593def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2594 WHERE_dict={}
2595 if utils.check_valid_uuid(datacenter_id_name):
2596 WHERE_dict['d.uuid'] = datacenter_id_name
2597 else:
2598 WHERE_dict['d.name'] = datacenter_id_name
2599
2600 if tenant_id:
2601 WHERE_dict['nfvo_tenant_id'] = tenant_id
2602 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2603 " dt on td.datacenter_tenant_id=dt.uuid"
2604 else:
2605 from_ = 'datacenters as d'
2606 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid",), WHERE=WHERE_dict )
2607 if len(vimaccounts) == 0:
2608 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2609 elif len(vimaccounts)>1:
2610 #print "nfvo.datacenter_action() error. Several datacenters found"
2611 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2612 return vimaccounts[0]["uuid"]
2613
2614
tiernoa2793912016-10-04 08:15:08 +00002615def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02002616 datacenter_id = None
2617 datacenter_name = None
2618 if datacenter_id_name:
tierno42026a02017-02-10 15:13:40 +01002619 if utils.check_valid_uuid(datacenter_id_name):
tiernobe41e222016-09-02 15:16:13 +02002620 datacenter_id = datacenter_id_name
2621 else:
2622 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00002623 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02002624 if len(vims) == 0:
2625 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2626 elif len(vims)>1:
2627 #print "nfvo.datacenter_action() error. Several datacenters found"
2628 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2629 return vims.keys()[0], vims.values()[0]
2630
tiernob3d36742017-03-03 23:51:05 +01002631
garciadeblas9f8456e2016-09-05 05:02:59 +02002632def update(d, u):
2633 '''Takes dict d and updates it with the values in dict u.'''
2634 '''It merges all depth levels'''
2635 for k, v in u.iteritems():
2636 if isinstance(v, collections.Mapping):
2637 r = update(d.get(k, {}), v)
2638 d[k] = r
2639 else:
2640 d[k] = u[k]
2641 return d
2642
tierno7edb6752016-03-21 17:37:52 +01002643def create_instance(mydb, tenant_id, instance_dict):
tiernob3d36742017-03-03 23:51:05 +01002644 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2645 # logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01002646 scenario = instance_dict["scenario"]
tierno42026a02017-02-10 15:13:40 +01002647
tierno868220c2017-09-26 00:11:05 +02002648 # find main datacenter
tiernobe41e222016-09-02 15:16:13 +02002649 myvims = {}
tierno867ffe92017-03-27 12:50:34 +02002650 myvim_threads_id = {}
tierno7edb6752016-03-21 17:37:52 +01002651 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02002652 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
2653 myvims[default_datacenter_id] = vim
tierno867ffe92017-03-27 12:50:34 +02002654 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
gcalvinoe580c7d2017-09-22 14:09:51 +02002655 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
tierno868220c2017-09-26 00:11:05 +02002656 # myvim_tenant = myvim['tenant_id']
gcalvinoe580c7d2017-09-22 14:09:51 +02002657
tierno7edb6752016-03-21 17:37:52 +01002658 rollbackList=[]
tierno42026a02017-02-10 15:13:40 +01002659
tierno868220c2017-09-26 00:11:05 +02002660 # print "Checking that the scenario exists and getting the scenario dictionary"
2661 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
2662 datacenter_id=default_datacenter_id)
tierno42026a02017-02-10 15:13:40 +01002663
tierno868220c2017-09-26 00:11:05 +02002664 # logger.debug(">>>>>> Dictionaries before merging")
2665 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2666 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
tierno42026a02017-02-10 15:13:40 +01002667
tierno868220c2017-09-26 00:11:05 +02002668 db_instance_vnfs = []
2669 db_instance_vms = []
2670 db_instance_interfaces = []
2671 db_ip_profiles = []
2672 db_vim_actions = []
tierno8e690322017-08-10 15:58:50 +02002673 uuid_list = []
tierno868220c2017-09-26 00:11:05 +02002674 task_index = 0
tierno8e690322017-08-10 15:58:50 +02002675 instance_name = instance_dict["name"]
2676 instance_uuid = str(uuid4())
2677 uuid_list.append(instance_uuid)
2678 db_instance_scenario = {
2679 "uuid": instance_uuid,
2680 "name": instance_name,
2681 "tenant_id": tenant_id,
2682 "scenario_id": scenarioDict['uuid'],
2683 "datacenter_id": default_datacenter_id,
2684 # filled bellow 'datacenter_tenant_id'
2685 "description": instance_dict.get("description"),
2686 }
tierno8e690322017-08-10 15:58:50 +02002687 if scenarioDict.get("cloud-config"):
2688 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
2689 default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02002690 instance_action_id = get_task_id()
2691 db_instance_action = {
2692 "uuid": instance_action_id, # same uuid for the instance and the action on create
2693 "tenant_id": tenant_id,
2694 "instance_id": instance_uuid,
2695 "description": "CREATE",
2696 }
garciadeblas9f8456e2016-09-05 05:02:59 +02002697
tierno868220c2017-09-26 00:11:05 +02002698 # Auxiliary dictionaries from x to y
2699 vnf_net2instance = {}
tierno8e690322017-08-10 15:58:50 +02002700 sce_net2instance = {}
tierno868220c2017-09-26 00:11:05 +02002701 net2task_id = {'scenario': {}}
tierno42026a02017-02-10 15:13:40 +01002702
tierno868220c2017-09-26 00:11:05 +02002703 # logger.debug("Creating instance from scenario-dict:\n%s",
2704 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +01002705 try:
tiernob3d36742017-03-03 23:51:05 +01002706 # 0 check correct parameters
tierno868220c2017-09-26 00:11:05 +02002707 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
tiernob3d36742017-03-03 23:51:05 +01002708 found = False
tierno7edb6752016-03-21 17:37:52 +01002709 for scenario_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02002710 if net_name == scenario_net["name"]:
tierno7edb6752016-03-21 17:37:52 +01002711 found = True
2712 break
2713 if not found:
tierno868220c2017-09-26 00:11:05 +02002714 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name),
2715 HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02002716 if "sites" not in net_instance_desc:
2717 net_instance_desc["sites"] = [ {} ]
2718 site_without_datacenter_field = False
2719 for site in net_instance_desc["sites"]:
2720 if site.get("datacenter"):
tiernoa15c4b92017-10-05 12:41:44 +02002721 site["datacenter"] = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02002722 if site["datacenter"] not in myvims:
tierno868220c2017-09-26 00:11:05 +02002723 # Add this datacenter to myvims
tiernobe41e222016-09-02 15:16:13 +02002724 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
2725 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02002726 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
2727 site["datacenter"] = d # change name to id
tiernobe41e222016-09-02 15:16:13 +02002728 else:
2729 if site_without_datacenter_field:
tierno868220c2017-09-26 00:11:05 +02002730 raise NfvoException("Found more than one entries without datacenter field at "
2731 "instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
tiernobe41e222016-09-02 15:16:13 +02002732 site_without_datacenter_field = True
tierno868220c2017-09-26 00:11:05 +02002733 site["datacenter"] = default_datacenter_id # change name to id
tierno42026a02017-02-10 15:13:40 +01002734
tiernobe41e222016-09-02 15:16:13 +02002735 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno868220c2017-09-26 00:11:05 +02002736 found = False
tierno7edb6752016-03-21 17:37:52 +01002737 for scenario_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02002738 if vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01002739 found = True
2740 break
2741 if not found:
tiernobe41e222016-09-02 15:16:13 +02002742 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
2743 if "datacenter" in vnf_instance_desc:
tierno868220c2017-09-26 00:11:05 +02002744 # Add this datacenter to myvims
tiernoa15c4b92017-10-05 12:41:44 +02002745 vnf_instance_desc["datacenter"] = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernobe41e222016-09-02 15:16:13 +02002746 if vnf_instance_desc["datacenter"] not in myvims:
2747 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
2748 myvims[d] = v
tierno868220c2017-09-26 00:11:05 +02002749 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
tiernoa2793912016-10-04 08:15:08 +00002750 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01002751
tierno868220c2017-09-26 00:11:05 +02002752 # 0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01002753 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
gcalvinoe580c7d2017-09-22 14:09:51 +02002754 # We add the RO key to cloud_config
2755 if tenant[0].get('RO_pub_key'):
2756 RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
2757 cloud_config = unify_cloud_config(cloud_config, RO_key)
garciadeblas9f8456e2016-09-05 05:02:59 +02002758
tierno868220c2017-09-26 00:11:05 +02002759 # 0.2 merge instance information into scenario
2760 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
2761 # However, this is not possible yet.
garciadeblas9f8456e2016-09-05 05:02:59 +02002762 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
2763 for scenario_net in scenarioDict['nets']:
2764 if net_name == scenario_net["name"]:
2765 if 'ip-profile' in net_instance_desc:
tierno455612d2017-05-30 16:40:10 +02002766 # translate from input format to database format
2767 ipprofile_in = net_instance_desc['ip-profile']
2768 ipprofile_db = {}
2769 ipprofile_db['subnet_address'] = ipprofile_in.get('subnet-address')
2770 ipprofile_db['ip_version'] = ipprofile_in.get('ip-version', 'IPv4')
2771 ipprofile_db['gateway_address'] = ipprofile_in.get('gateway-address')
2772 ipprofile_db['dns_address'] = ipprofile_in.get('dns-address')
2773 if isinstance(ipprofile_db['dns_address'], (list, tuple)):
2774 ipprofile_db['dns_address'] = ";".join(ipprofile_db['dns_address'])
2775 if 'dhcp' in ipprofile_in:
2776 ipprofile_db['dhcp_start_address'] = ipprofile_in['dhcp'].get('start-address')
2777 ipprofile_db['dhcp_enabled'] = ipprofile_in['dhcp'].get('enabled', True)
2778 ipprofile_db['dhcp_count'] = ipprofile_in['dhcp'].get('count' )
garciadeblasedca7b32016-09-29 14:01:52 +00002779 if 'ip_profile' not in scenario_net:
tierno455612d2017-05-30 16:40:10 +02002780 scenario_net['ip_profile'] = ipprofile_db
garciadeblasedca7b32016-09-29 14:01:52 +00002781 else:
tierno455612d2017-05-30 16:40:10 +02002782 update(scenario_net['ip_profile'], ipprofile_db)
tiernoe6c58ce2016-09-14 16:02:49 +02002783 for interface in net_instance_desc.get('interfaces', () ):
garciadeblas9f8456e2016-09-05 05:02:59 +02002784 if 'ip_address' in interface:
2785 for vnf in scenarioDict['vnfs']:
2786 if interface['vnf'] == vnf['name']:
2787 for vnf_interface in vnf['interfaces']:
2788 if interface['vnf_interface'] == vnf_interface['external_name']:
2789 vnf_interface['ip_address']=interface['ip_address']
2790
tierno868220c2017-09-26 00:11:05 +02002791 # logger.debug(">>>>>>>> Merged dictionary")
2792 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
2793 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02002794
tiernob3d36742017-03-03 23:51:05 +01002795 # 1. Creating new nets (sce_nets) in the VIM"
tierno8e690322017-08-10 15:58:50 +02002796 db_instance_nets = []
tierno7edb6752016-03-21 17:37:52 +01002797 for sce_net in scenarioDict['nets']:
tierno868220c2017-09-26 00:11:05 +02002798 descriptor_net = instance_dict.get("networks", {}).get(sce_net["name"], {})
tiernobe41e222016-09-02 15:16:13 +02002799 net_name = descriptor_net.get("vim-network-name")
tierno8e690322017-08-10 15:58:50 +02002800 sce_net2instance[sce_net['uuid']] = {}
tierno868220c2017-09-26 00:11:05 +02002801 net2task_id['scenario'][sce_net['uuid']] = {}
tiernobe41e222016-09-02 15:16:13 +02002802
2803 sites = descriptor_net.get("sites", [ {} ])
2804 for site in sites:
2805 if site.get("datacenter"):
2806 vim = myvims[ site["datacenter"] ]
2807 datacenter_id = site["datacenter"]
tierno867ffe92017-03-27 12:50:34 +02002808 myvim_thread_id = myvim_threads_id[ site["datacenter"] ]
tierno7edb6752016-03-21 17:37:52 +01002809 else:
tiernobe41e222016-09-02 15:16:13 +02002810 vim = myvims[ default_datacenter_id ]
2811 datacenter_id = default_datacenter_id
tierno867ffe92017-03-27 12:50:34 +02002812 myvim_thread_id = myvim_threads_id[default_datacenter_id]
tiernobe41e222016-09-02 15:16:13 +02002813 net_type = sce_net['type']
tierno868220c2017-09-26 00:11:05 +02002814 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
tierno42026a02017-02-10 15:13:40 +01002815
tiernof1ba57e2017-09-07 12:23:19 +02002816 if not net_name:
2817 if sce_net["external"]:
2818 net_name = sce_net["name"]
2819 else:
2820 net_name = "{}.{}".format(instance_name, sce_net["name"])
2821 net_name = net_name[:255] # limit length
2822
2823 if "netmap-use" in site or "netmap-create" in site:
2824 create_network = False
2825 lookfor_network = False
2826 if "netmap-use" in site:
2827 lookfor_network = True
2828 if utils.check_valid_uuid(site["netmap-use"]):
2829 filter_text = "scenario id '%s'" % site["netmap-use"]
2830 lookfor_filter["id"] = site["netmap-use"]
2831 else:
2832 filter_text = "scenario name '%s'" % site["netmap-use"]
2833 lookfor_filter["name"] = site["netmap-use"]
2834 if "netmap-create" in site:
2835 create_network = True
2836 net_vim_name = net_name
2837 if site["netmap-create"]:
2838 net_vim_name = site["netmap-create"]
2839 elif sce_net["external"]:
2840 if sce_net['vim_id'] != None:
tierno868220c2017-09-26 00:11:05 +02002841 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
tiernobe41e222016-09-02 15:16:13 +02002842 create_network = False
2843 lookfor_network = True
2844 lookfor_filter["id"] = sce_net['vim_id']
tierno868220c2017-09-26 00:11:05 +02002845 filter_text = "vim_id '{}' datacenter_netmap name '{}'. Try to reload vims with "\
2846 "datacenter-net-update".format(sce_net['vim_id'], sce_net["name"])
2847 # look for network at datacenter and return error
tiernobe41e222016-09-02 15:16:13 +02002848 else:
tierno868220c2017-09-26 00:11:05 +02002849 # 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 +02002850 create_network = True
2851 lookfor_network = True
2852 lookfor_filter["name"] = sce_net["name"]
2853 net_vim_name = sce_net["name"]
2854 filter_text = "scenario name '%s'" % sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01002855 else:
tiernobe41e222016-09-02 15:16:13 +02002856 net_vim_name = net_name
2857 create_network = True
2858 lookfor_network = False
tierno42026a02017-02-10 15:13:40 +01002859
tiernof1450872017-10-17 23:15:08 +02002860 task_extra = {}
2861 if create_network:
2862 task_action = "CREATE"
2863 task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None))
2864 if lookfor_network:
2865 task_extra["find"] = (lookfor_filter,)
tierno868220c2017-09-26 00:11:05 +02002866 elif lookfor_network:
2867 task_action = "FIND"
tiernof1450872017-10-17 23:15:08 +02002868 task_extra["params"] = (lookfor_filter,)
tierno42026a02017-02-10 15:13:40 +01002869
tierno8e690322017-08-10 15:58:50 +02002870 # fill database content
2871 net_uuid = str(uuid4())
2872 uuid_list.append(net_uuid)
2873 sce_net2instance[sce_net['uuid']][datacenter_id] = net_uuid
2874 db_net = {
2875 "uuid": net_uuid,
tierno868220c2017-09-26 00:11:05 +02002876 'vim_net_id': None,
tierno8e690322017-08-10 15:58:50 +02002877 "instance_scenario_id": instance_uuid,
2878 "sce_net_id": sce_net["uuid"],
2879 "created": create_network,
2880 'datacenter_id': datacenter_id,
2881 'datacenter_tenant_id': myvim_thread_id,
2882 'status': 'BUILD' if create_network else "ACTIVE"
2883 }
2884 db_instance_nets.append(db_net)
tierno868220c2017-09-26 00:11:05 +02002885 db_vim_action = {
2886 "instance_action_id": instance_action_id,
2887 "status": "SCHEDULED",
2888 "task_index": task_index,
2889 "datacenter_vim_id": myvim_thread_id,
2890 "action": task_action,
2891 "item": "instance_nets",
2892 "item_id": net_uuid,
tiernof1450872017-10-17 23:15:08 +02002893 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
tierno868220c2017-09-26 00:11:05 +02002894 }
2895 net2task_id['scenario'][sce_net['uuid']][datacenter_id] = task_index
2896 task_index += 1
2897 db_vim_actions.append(db_vim_action)
2898
tierno8e690322017-08-10 15:58:50 +02002899 if 'ip_profile' in sce_net:
2900 db_ip_profile={
2901 'instance_net_id': net_uuid,
2902 'ip_version': sce_net['ip_profile']['ip_version'],
2903 'subnet_address': sce_net['ip_profile']['subnet_address'],
2904 'gateway_address': sce_net['ip_profile']['gateway_address'],
2905 'dns_address': sce_net['ip_profile']['dns_address'],
2906 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
2907 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
2908 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
2909 }
2910 db_ip_profiles.append(db_ip_profile)
2911
tiernob3d36742017-03-03 23:51:05 +01002912 # 2. Creating new nets (vnf internal nets) in the VIM"
mirabal29356312017-07-27 12:21:22 +02002913 # For each vnf net, we create it and we add it to instanceNetlist.
tierno7edb6752016-03-21 17:37:52 +01002914 for sce_vnf in scenarioDict['vnfs']:
2915 for net in sce_vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +02002916 if sce_vnf.get("datacenter"):
tiernobe41e222016-09-02 15:16:13 +02002917 datacenter_id = sce_vnf["datacenter"]
tierno868220c2017-09-26 00:11:05 +02002918 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
tiernobe41e222016-09-02 15:16:13 +02002919 else:
tiernobe41e222016-09-02 15:16:13 +02002920 datacenter_id = default_datacenter_id
tierno867ffe92017-03-27 12:50:34 +02002921 myvim_thread_id = myvim_threads_id[default_datacenter_id]
tierno868220c2017-09-26 00:11:05 +02002922 descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
tierno7edb6752016-03-21 17:37:52 +01002923 net_name = descriptor_net.get("name")
2924 if not net_name:
tierno868220c2017-09-26 00:11:05 +02002925 net_name = "{}.{}".format(instance_name, net["name"])
2926 net_name = net_name[:255] # limit length
tierno7edb6752016-03-21 17:37:52 +01002927 net_type = net['type']
tierno868220c2017-09-26 00:11:05 +02002928
tierno8e690322017-08-10 15:58:50 +02002929 if sce_vnf['uuid'] not in vnf_net2instance:
2930 vnf_net2instance[sce_vnf['uuid']] = {}
tierno868220c2017-09-26 00:11:05 +02002931 if sce_vnf['uuid'] not in net2task_id:
2932 net2task_id[sce_vnf['uuid']] = {}
2933 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
tierno66345bc2016-09-26 11:37:55 +02002934
tierno8e690322017-08-10 15:58:50 +02002935 # fill database content
2936 net_uuid = str(uuid4())
2937 uuid_list.append(net_uuid)
2938 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
2939 db_net = {
2940 "uuid": net_uuid,
tierno868220c2017-09-26 00:11:05 +02002941 'vim_net_id': None,
tierno8e690322017-08-10 15:58:50 +02002942 "instance_scenario_id": instance_uuid,
2943 "net_id": net["uuid"],
2944 "created": True,
2945 'datacenter_id': datacenter_id,
2946 'datacenter_tenant_id': myvim_thread_id,
2947 }
2948 db_instance_nets.append(db_net)
tierno868220c2017-09-26 00:11:05 +02002949
2950 db_vim_action = {
2951 "instance_action_id": instance_action_id,
2952 "task_index": task_index,
2953 "datacenter_vim_id": myvim_thread_id,
2954 "status": "SCHEDULED",
2955 "action": "CREATE",
2956 "item": "instance_nets",
2957 "item_id": net_uuid,
2958 "extra": yaml.safe_dump({"params": (net_name, net_type, net.get('ip_profile',None))},
2959 default_flow_style=True, width=256)
2960 }
2961 task_index += 1
2962 db_vim_actions.append(db_vim_action)
2963
tierno8e690322017-08-10 15:58:50 +02002964 if 'ip_profile' in net:
2965 db_ip_profile = {
2966 'instance_net_id': net_uuid,
2967 'ip_version': net['ip_profile']['ip_version'],
2968 'subnet_address': net['ip_profile']['subnet_address'],
2969 'gateway_address': net['ip_profile']['gateway_address'],
2970 'dns_address': net['ip_profile']['dns_address'],
2971 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
2972 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
2973 'dhcp_count': net['ip_profile']['dhcp_count'],
2974 }
2975 db_ip_profiles.append(db_ip_profile)
2976
tierno868220c2017-09-26 00:11:05 +02002977 # print "vnf_net2instance:"
2978 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
tierno42026a02017-02-10 15:13:40 +01002979
tiernob3d36742017-03-03 23:51:05 +01002980 # 3. Creating new vm instances in the VIM
tierno868220c2017-09-26 00:11:05 +02002981 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2982 sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
garciadeblasacd4e782017-07-23 19:44:55 +02002983 for sce_vnf in sce_vnf_list:
tierno5a3273c2017-08-29 11:43:46 +02002984 vnf_availability_zones = []
mirabal29356312017-07-27 12:21:22 +02002985 for vm in sce_vnf['vms']:
2986 vm_av = vm.get('availability_zone')
tierno5a3273c2017-08-29 11:43:46 +02002987 if vm_av and vm_av not in vnf_availability_zones:
2988 vnf_availability_zones.append(vm_av)
mirabal29356312017-07-27 12:21:22 +02002989
2990 # check if there is enough availability zones available at vim level.
tierno5a3273c2017-08-29 11:43:46 +02002991 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2992 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2993 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
mirabal29356312017-07-27 12:21:22 +02002994
tiernobe41e222016-09-02 15:16:13 +02002995 if sce_vnf.get("datacenter"):
2996 vim = myvims[ sce_vnf["datacenter"] ]
tierno867ffe92017-03-27 12:50:34 +02002997 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ]
tiernobe41e222016-09-02 15:16:13 +02002998 datacenter_id = sce_vnf["datacenter"]
2999 else:
3000 vim = myvims[ default_datacenter_id ]
tierno867ffe92017-03-27 12:50:34 +02003001 myvim_thread_id = myvim_threads_id[ default_datacenter_id ]
tiernobe41e222016-09-02 15:16:13 +02003002 datacenter_id = default_datacenter_id
mirabal29356312017-07-27 12:21:22 +02003003 sce_vnf["datacenter_id"] = datacenter_id
tierno7edb6752016-03-21 17:37:52 +01003004 i = 0
mirabal29356312017-07-27 12:21:22 +02003005
tierno8e690322017-08-10 15:58:50 +02003006 vnf_uuid = str(uuid4())
3007 uuid_list.append(vnf_uuid)
3008 db_instance_vnf = {
3009 'uuid': vnf_uuid,
3010 'instance_scenario_id': instance_uuid,
3011 'vnf_id': sce_vnf['vnf_id'],
3012 'sce_vnf_id': sce_vnf['uuid'],
3013 'datacenter_id': datacenter_id,
3014 'datacenter_tenant_id': myvim_thread_id,
3015 }
3016 db_instance_vnfs.append(db_instance_vnf)
3017
tierno7edb6752016-03-21 17:37:52 +01003018 for vm in sce_vnf['vms']:
tierno7edb6752016-03-21 17:37:52 +01003019 myVMDict = {}
tierno8e690322017-08-10 15:58:50 +02003020 myVMDict['name'] = "{}.{}.{}".format(instance_name[:64], sce_vnf['name'][:64], vm["name"][:64])
tierno7edb6752016-03-21 17:37:52 +01003021 myVMDict['description'] = myVMDict['name'][0:99]
3022# if not startvms:
3023# myVMDict['start'] = "no"
tierno868220c2017-09-26 00:11:05 +02003024 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
tierno7edb6752016-03-21 17:37:52 +01003025 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02003026 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno5e91eb82016-10-04 09:39:07 +00003027 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
tierno7edb6752016-03-21 17:37:52 +01003028 vm['vim_image_id'] = image_id
tierno42026a02017-02-10 15:13:40 +01003029
tierno868220c2017-09-26 00:11:05 +02003030 # create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02003031 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tierno7edb6752016-03-21 17:37:52 +01003032 if flavor_dict['extended']!=None:
tierno868220c2017-09-26 00:11:05 +02003033 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
montesmoreno0c8def02016-12-22 12:16:23 +00003034 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
3035
tierno868220c2017-09-26 00:11:05 +02003036 # Obtain information for additional disks
montesmoreno0c8def02016-12-22 12:16:23 +00003037 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
3038 if not extended_flavor_dict:
3039 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
3040 return
3041
tierno868220c2017-09-26 00:11:05 +02003042 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
montesmoreno0c8def02016-12-22 12:16:23 +00003043 myVMDict['disks'] = None
3044 extended_info = extended_flavor_dict[0]['extended']
3045 if extended_info != None:
3046 extended_flavor_dict_yaml = yaml.load(extended_info)
3047 if 'disks' in extended_flavor_dict_yaml:
3048 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
3049
tierno7edb6752016-03-21 17:37:52 +01003050 vm['vim_flavor_id'] = flavor_id
tierno7edb6752016-03-21 17:37:52 +01003051 myVMDict['imageRef'] = vm['vim_image_id']
3052 myVMDict['flavorRef'] = vm['vim_flavor_id']
mirabal29356312017-07-27 12:21:22 +02003053 myVMDict['availability_zone'] = vm.get('availability_zone')
tierno7edb6752016-03-21 17:37:52 +01003054 myVMDict['networks'] = []
tierno868220c2017-09-26 00:11:05 +02003055 task_depends_on = []
3056 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno8e690322017-08-10 15:58:50 +02003057 db_vm_ifaces = []
tierno7edb6752016-03-21 17:37:52 +01003058 for iface in vm['interfaces']:
3059 netDict = {}
3060 if iface['type']=="data":
3061 netDict['type'] = iface['model']
3062 elif "model" in iface and iface["model"]!=None:
3063 netDict['model']=iface['model']
tierno868220c2017-09-26 00:11:05 +02003064 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3065 # is obtained from iterface table model
3066 # discover type of interface looking at flavor
tierno7edb6752016-03-21 17:37:52 +01003067 for numa in flavor_dict.get('extended',{}).get('numas',[]):
3068 for flavor_iface in numa.get('interfaces',[]):
3069 if flavor_iface.get('name') == iface['internal_name']:
3070 if flavor_iface['dedicated'] == 'yes':
3071 netDict['type']="PF" #passthrough
3072 elif flavor_iface['dedicated'] == 'no':
3073 netDict['type']="VF" #siov
3074 elif flavor_iface['dedicated'] == 'yes:sriov':
3075 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
3076 netDict["mac_address"] = flavor_iface.get("mac_address")
3077 break;
3078 netDict["use"]=iface['type']
3079 if netDict["use"]=="data" and not netDict.get("type"):
3080 #print "netDict", netDict
3081 #print "iface", iface
3082 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'])
3083 if flavor_dict.get('extended')==None:
tiernoae4a8d12016-07-08 12:30:39 +02003084 raise NfvoException(e_text + "After database migration some information is not available. \
3085 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01003086 else:
tiernoae4a8d12016-07-08 12:30:39 +02003087 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01003088 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
3089 netDict["type"]="virtual"
3090 if "vpci" in iface and iface["vpci"] is not None:
3091 netDict['vpci'] = iface['vpci']
3092 if "mac" in iface and iface["mac"] is not None:
3093 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00003094 if "port-security" in iface and iface["port-security"] is not None:
3095 netDict['port_security'] = iface['port-security']
3096 if "floating-ip" in iface and iface["floating-ip"] is not None:
3097 netDict['floating_ip'] = iface['floating-ip']
tierno7edb6752016-03-21 17:37:52 +01003098 netDict['name'] = iface['internal_name']
3099 if iface['net_id'] is None:
3100 for vnf_iface in sce_vnf["interfaces"]:
tierno868220c2017-09-26 00:11:05 +02003101 # print iface
3102 # print vnf_iface
tierno7edb6752016-03-21 17:37:52 +01003103 if vnf_iface['interface_id']==iface['uuid']:
tierno868220c2017-09-26 00:11:05 +02003104 netDict['net_id'] = "TASK-{}".format(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id])
tierno8e690322017-08-10 15:58:50 +02003105 instance_net_id = sce_net2instance[ vnf_iface['sce_net_id'] ][datacenter_id]
tierno868220c2017-09-26 00:11:05 +02003106 task_depends_on.append(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id])
tierno7edb6752016-03-21 17:37:52 +01003107 break
3108 else:
tierno868220c2017-09-26 00:11:05 +02003109 netDict['net_id'] = "TASK-{}".format(net2task_id[ sce_vnf['uuid'] ][ iface['net_id'] ])
tierno8e690322017-08-10 15:58:50 +02003110 instance_net_id = vnf_net2instance[ sce_vnf['uuid'] ][ iface['net_id'] ]
tierno868220c2017-09-26 00:11:05 +02003111 task_depends_on.append(net2task_id[sce_vnf['uuid'] ][ iface['net_id']])
3112 # skip bridge ifaces not connected to any net
3113 if 'net_id' not in netDict or netDict['net_id']==None:
3114 continue
tierno7edb6752016-03-21 17:37:52 +01003115 myVMDict['networks'].append(netDict)
tierno8e690322017-08-10 15:58:50 +02003116 db_vm_iface={
3117 # "uuid"
3118 # 'instance_vm_id': instance_vm_uuid,
3119 "instance_net_id": instance_net_id,
3120 'interface_id': iface['uuid'],
3121 # 'vim_interface_id': ,
3122 'type': 'external' if iface['external_name'] is not None else 'internal',
3123 'ip_address': iface.get('ip_address'),
3124 'floating_ip': int(iface.get('floating-ip', False)),
3125 'port_security': int(iface.get('port-security', True))
3126 }
3127 db_vm_ifaces.append(db_vm_iface)
3128 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3129 # print myVMDict['name']
3130 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3131 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3132 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
tierno36c0b172017-01-12 18:32:28 +01003133 if vm.get("boot_data"):
3134 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
3135 else:
3136 cloud_config_vm = cloud_config
tierno5a3273c2017-08-29 11:43:46 +02003137 if myVMDict.get('availability_zone'):
3138 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
mirabal29356312017-07-27 12:21:22 +02003139 else:
tierno5a3273c2017-08-29 11:43:46 +02003140 av_index = None
tierno8e690322017-08-10 15:58:50 +02003141 for vm_index in range(0, vm.get('count', 1)):
3142 vm_index_name = ""
3143 if vm.get('count', 1) > 1:
3144 vm_index_name += "." + chr(97 + vm_index)
tierno868220c2017-09-26 00:11:05 +02003145 task_params = (myVMDict['name']+vm_index_name, myVMDict['description'], myVMDict.get('start', None),
3146 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
3147 myVMDict['disks'], av_index, vnf_availability_zones)
tierno8e690322017-08-10 15:58:50 +02003148 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3149 for net in myVMDict['networks']:
3150 if "vim_id" in net:
3151 for iface in vm['interfaces']:
3152 if net["name"]==iface["internal_name"]:
3153 iface["vim_id"]=net["vim_id"]
3154 break
3155 vm_uuid = str(uuid4())
3156 uuid_list.append(vm_uuid)
3157 db_vm = {
3158 "uuid": vm_uuid,
3159 'instance_vnf_id': vnf_uuid,
tierno868220c2017-09-26 00:11:05 +02003160 #TODO delete "vim_vm_id": vm_id,
tierno8e690322017-08-10 15:58:50 +02003161 "vm_id": vm["uuid"],
3162 # "status":
3163 }
3164 db_instance_vms.append(db_vm)
tierno868220c2017-09-26 00:11:05 +02003165
3166 iface_index = 0
tierno8e690322017-08-10 15:58:50 +02003167 for db_vm_iface in db_vm_ifaces:
3168 iface_uuid = str(uuid4())
3169 uuid_list.append(iface_uuid)
3170 db_vm_iface_instance = {
3171 "uuid": iface_uuid,
3172 "instance_vm_id": vm_uuid
3173 }
3174 db_vm_iface_instance.update(db_vm_iface)
3175 if db_vm_iface_instance.get("ip_address"): # increment ip_address
3176 ip = db_vm_iface_instance.get("ip_address")
3177 i = ip.rfind(".")
3178 if i > 0:
3179 try:
3180 i += 1
3181 ip = ip[i:] + str(int(ip[:i]) +1)
3182 db_vm_iface_instance["ip_address"] = ip
3183 except:
3184 db_vm_iface_instance["ip_address"] = None
3185 db_instance_interfaces.append(db_vm_iface_instance)
tierno868220c2017-09-26 00:11:05 +02003186 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
3187 iface_index += 1
3188
3189 db_vim_action = {
3190 "instance_action_id": instance_action_id,
3191 "task_index": task_index,
3192 "datacenter_vim_id": myvim_thread_id,
3193 "action": "CREATE",
3194 "status": "SCHEDULED",
3195 "item": "instance_vms",
3196 "item_id": vm_uuid,
3197 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
3198 default_flow_style=True, width=256)
3199 }
3200 task_index += 1
3201 db_vim_actions.append(db_vim_action)
tierno8e690322017-08-10 15:58:50 +02003202
tierno867ffe92017-03-27 12:50:34 +02003203 scenarioDict["datacenter2tenant"] = myvim_threads_id
tierno8e690322017-08-10 15:58:50 +02003204
tierno868220c2017-09-26 00:11:05 +02003205 db_instance_action["number_tasks"] = task_index
tierno8e690322017-08-10 15:58:50 +02003206 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3207 db_instance_scenario['datacenter_id'] = default_datacenter_id
3208 db_tables=[
3209 {"instance_scenarios": db_instance_scenario},
3210 {"instance_vnfs": db_instance_vnfs},
3211 {"instance_nets": db_instance_nets},
3212 {"ip_profiles": db_ip_profiles},
3213 {"instance_vms": db_instance_vms},
3214 {"instance_interfaces": db_instance_interfaces},
tierno868220c2017-09-26 00:11:05 +02003215 {"instance_actions": db_instance_action},
3216 {"vim_actions": db_vim_actions}
tierno8e690322017-08-10 15:58:50 +02003217 ]
3218
tierno868220c2017-09-26 00:11:05 +02003219 logger.debug("create_instance done DB tables: %s",
tierno8e690322017-08-10 15:58:50 +02003220 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3221 mydb.new_rows(db_tables, uuid_list)
tierno868220c2017-09-26 00:11:05 +02003222 for myvim_thread_id in myvim_threads_id.values():
3223 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
tierno867ffe92017-03-27 12:50:34 +02003224
tierno868220c2017-09-26 00:11:05 +02003225 returned_instance = mydb.get_instance_scenario(instance_uuid)
3226 returned_instance["action_id"] = instance_action_id
3227 return returned_instance
3228 except (NfvoException, vimconn.vimconnException, db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02003229 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02003230 if isinstance(e, db_base_Exception):
3231 error_text = "database Exception"
3232 elif isinstance(e, vimconn.vimconnException):
3233 error_text = "VIM Exception"
3234 else:
3235 error_text = "Exception"
3236 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
tierno868220c2017-09-26 00:11:05 +02003237 # logger.error("create_instance: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02003238 raise NfvoException(error_text, e.http_code)
tierno42026a02017-02-10 15:13:40 +01003239
tiernob3d36742017-03-03 23:51:05 +01003240
tierno7edb6752016-03-21 17:37:52 +01003241def delete_instance(mydb, tenant_id, instance_id):
tierno868220c2017-09-26 00:11:05 +02003242 # print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02003243 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tierno868220c2017-09-26 00:11:05 +02003244 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01003245 tenant_id = instanceDict["tenant_id"]
tierno868220c2017-09-26 00:11:05 +02003246 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno7edb6752016-03-21 17:37:52 +01003247
tierno868220c2017-09-26 00:11:05 +02003248 # 1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02003249 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01003250
tierno868220c2017-09-26 00:11:05 +02003251 # 2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00003252 error_msg = ""
tiernob3d36742017-03-03 23:51:05 +01003253 myvims = {}
3254 myvim_threads = {}
tierno868220c2017-09-26 00:11:05 +02003255 vimthread_affected = {}
tierno3fcfdb72017-10-24 07:48:24 +02003256 net2vm_dependencies = {}
tierno7edb6752016-03-21 17:37:52 +01003257
tierno868220c2017-09-26 00:11:05 +02003258 task_index = 0
3259 instance_action_id = get_task_id()
3260 db_vim_actions = []
3261 db_instance_action = {
3262 "uuid": instance_action_id, # same uuid for the instance and the action on create
3263 "tenant_id": tenant_id,
3264 "instance_id": instance_id,
3265 "description": "DELETE",
3266 # "number_tasks": 0 # filled bellow
3267 }
3268
3269 # 2.1 deleting VMs
3270 # vm_fail_list=[]
tierno7edb6752016-03-21 17:37:52 +01003271 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00003272 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tierno868220c2017-09-26 00:11:05 +02003273 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
tiernoa2793912016-10-04 08:15:08 +00003274 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01003275 try:
tierno867ffe92017-03-27 12:50:34 +02003276 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01003277 except NfvoException as e:
3278 logger.error(str(e))
3279 myvim_thread = None
3280 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00003281 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
3282 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
3283 if len(vims) == 0:
3284 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
3285 sce_vnf["datacenter_tenant_id"]))
3286 myvims[datacenter_key] = None
3287 else:
3288 myvims[datacenter_key] = vims.values()[0]
3289 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01003290 myvim_thread = myvim_threads[datacenter_key]
tierno7edb6752016-03-21 17:37:52 +01003291 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00003292 if not myvim:
3293 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
3294 continue
tierno3fcfdb72017-10-24 07:48:24 +02003295 db_vim_action = {
3296 "instance_action_id": instance_action_id,
3297 "task_index": task_index,
3298 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
3299 "action": "DELETE",
3300 "status": "SCHEDULED",
3301 "item": "instance_vms",
3302 "item_id": vm["uuid"],
3303 "extra": yaml.safe_dump({"params": vm["interfaces"]},
3304 default_flow_style=True, width=256)
3305 }
3306 db_vim_actions.append(db_vim_action)
3307 for interface in vm["interfaces"]:
3308 if not interface.get("instance_net_id"):
3309 continue
3310 if interface["instance_net_id"] not in net2vm_dependencies:
3311 net2vm_dependencies[interface["instance_net_id"]] = []
3312 net2vm_dependencies[interface["instance_net_id"]].append(task_index)
3313 task_index += 1
tierno42026a02017-02-10 15:13:40 +01003314
tierno868220c2017-09-26 00:11:05 +02003315 # 2.2 deleting NETS
3316 # net_fail_list=[]
tierno7edb6752016-03-21 17:37:52 +01003317 for net in instanceDict['nets']:
tierno868220c2017-09-26 00:11:05 +02003318 vimthread_affected[net["datacenter_tenant_id"]] = None
tiernoa2793912016-10-04 08:15:08 +00003319 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
3320 if datacenter_key not in myvims:
tiernob3d36742017-03-03 23:51:05 +01003321 try:
tierno867ffe92017-03-27 12:50:34 +02003322 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
tiernob3d36742017-03-03 23:51:05 +01003323 except NfvoException as e:
3324 logger.error(str(e))
3325 myvim_thread = None
3326 myvim_threads[datacenter_key] = myvim_thread
tiernoa2793912016-10-04 08:15:08 +00003327 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
3328 datacenter_tenant_id=net["datacenter_tenant_id"])
3329 if len(vims) == 0:
3330 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
3331 myvims[datacenter_key] = None
3332 else:
3333 myvims[datacenter_key] = vims.values()[0]
3334 myvim = myvims[datacenter_key]
tiernob3d36742017-03-03 23:51:05 +01003335 myvim_thread = myvim_threads[datacenter_key]
tiernoa2793912016-10-04 08:15:08 +00003336
tierno7edb6752016-03-21 17:37:52 +01003337 if not myvim:
tiernoa2793912016-10-04 08:15:08 +00003338 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
tierno7edb6752016-03-21 17:37:52 +01003339 continue
tierno3fcfdb72017-10-24 07:48:24 +02003340 extra = {"params": (net['vim_net_id'], net['sdn_net_id'])}
3341 if net2vm_dependencies.get(net["uuid"]):
3342 extra["depends_on"] = net2vm_dependencies[net["uuid"]]
3343 db_vim_action = {
3344 "instance_action_id": instance_action_id,
3345 "task_index": task_index,
3346 "datacenter_vim_id": net["datacenter_tenant_id"],
3347 "action": "DELETE",
3348 "status": "SCHEDULED",
3349 "item": "instance_nets",
3350 "item_id": net["uuid"],
3351 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
3352 }
3353 task_index += 1
3354 db_vim_actions.append(db_vim_action)
tierno868220c2017-09-26 00:11:05 +02003355
3356 db_instance_action["number_tasks"] = task_index
3357 db_tables = [
3358 {"instance_actions": db_instance_action},
3359 {"vim_actions": db_vim_actions}
3360 ]
3361
3362 logger.debug("delete_instance done DB tables: %s",
3363 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
3364 mydb.new_rows(db_tables, ())
3365 for myvim_thread_id in vimthread_affected.keys():
3366 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
3367
tiernob3d36742017-03-03 23:51:05 +01003368 if len(error_msg) > 0:
tierno868220c2017-09-26 00:11:05 +02003369 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
3370 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
tierno7edb6752016-03-21 17:37:52 +01003371 else:
tierno868220c2017-09-26 00:11:05 +02003372 return "action_id={} instance {} deleted".format(instance_action_id, message)
tierno7edb6752016-03-21 17:37:52 +01003373
tiernob3d36742017-03-03 23:51:05 +01003374
tierno7edb6752016-03-21 17:37:52 +01003375def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
3376 '''Refreshes a scenario instance. It modifies instanceDict'''
3377 '''Returns:
3378 - 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
3379 - error_msg
3380 '''
tierno867ffe92017-03-27 12:50:34 +02003381 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
3382 # #print "nfvo.refresh_instance begins"
3383 # #print json.dumps(instanceDict, indent=4)
3384 #
3385 # #print "Getting the VIM URL and the VIM tenant_id"
3386 # myvims={}
3387 #
3388 # # 1. Getting VIM vm and net list
3389 # vms_updated = [] #List of VM instance uuids in openmano that were updated
3390 # vms_notupdated=[]
3391 # vm_list = {}
3392 # for sce_vnf in instanceDict['vnfs']:
3393 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
3394 # if datacenter_key not in vm_list:
3395 # vm_list[datacenter_key] = []
3396 # if datacenter_key not in myvims:
3397 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
3398 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
3399 # if len(vims) == 0:
3400 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
3401 # myvims[datacenter_key] = None
3402 # else:
3403 # myvims[datacenter_key] = vims.values()[0]
3404 # for vm in sce_vnf['vms']:
3405 # vm_list[datacenter_key].append(vm['vim_vm_id'])
3406 # vms_notupdated.append(vm["uuid"])
3407 #
3408 # nets_updated = [] #List of VM instance uuids in openmano that were updated
3409 # nets_notupdated=[]
3410 # net_list = {}
3411 # for net in instanceDict['nets']:
3412 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
3413 # if datacenter_key not in net_list:
3414 # net_list[datacenter_key] = []
3415 # if datacenter_key not in myvims:
3416 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
3417 # datacenter_tenant_id=net["datacenter_tenant_id"])
3418 # if len(vims) == 0:
3419 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
3420 # myvims[datacenter_key] = None
3421 # else:
3422 # myvims[datacenter_key] = vims.values()[0]
3423 #
3424 # net_list[datacenter_key].append(net['vim_net_id'])
3425 # nets_notupdated.append(net["uuid"])
3426 #
3427 # # 1. Getting the status of all VMs
3428 # vm_dict={}
3429 # for datacenter_key in myvims:
3430 # if not vm_list.get(datacenter_key):
3431 # continue
3432 # failed = True
3433 # failed_message=""
3434 # if not myvims[datacenter_key]:
3435 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3436 # else:
3437 # try:
3438 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
3439 # failed = False
3440 # except vimconn.vimconnException as e:
3441 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3442 # failed_message = str(e)
3443 # if failed:
3444 # for vm in vm_list[datacenter_key]:
3445 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3446 #
3447 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
3448 # for sce_vnf in instanceDict['vnfs']:
3449 # for vm in sce_vnf['vms']:
3450 # vm_id = vm['vim_vm_id']
3451 # interfaces = vm_dict[vm_id].pop('interfaces', [])
3452 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
3453 # has_mgmt_iface = False
3454 # for iface in vm["interfaces"]:
3455 # if iface["type"]=="mgmt":
3456 # has_mgmt_iface = True
3457 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
3458 # vm_dict[vm_id]['status'] = "ACTIVE"
3459 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
3460 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
3461 # 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'):
3462 # vm['status'] = vm_dict[vm_id]['status']
3463 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
3464 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
3465 # # 2.1. Update in openmano DB the VMs whose status changed
3466 # try:
3467 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
3468 # vms_notupdated.remove(vm["uuid"])
3469 # if updates>0:
3470 # vms_updated.append(vm["uuid"])
3471 # except db_base_Exception as e:
3472 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3473 # # 2.2. Update in openmano DB the interface VMs
3474 # for interface in interfaces:
3475 # #translate from vim_net_id to instance_net_id
3476 # network_id_list=[]
3477 # for net in instanceDict['nets']:
3478 # if net["vim_net_id"] == interface["vim_net_id"]:
3479 # network_id_list.append(net["uuid"])
3480 # if not network_id_list:
3481 # continue
3482 # del interface["vim_net_id"]
3483 # try:
3484 # for network_id in network_id_list:
3485 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
3486 # except db_base_Exception as e:
3487 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
3488 #
3489 # # 3. Getting the status of all nets
3490 # net_dict = {}
3491 # for datacenter_key in myvims:
3492 # if not net_list.get(datacenter_key):
3493 # continue
3494 # failed = True
3495 # failed_message = ""
3496 # if not myvims[datacenter_key]:
3497 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3498 # else:
3499 # try:
3500 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
3501 # failed = False
3502 # except vimconn.vimconnException as e:
3503 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3504 # failed_message = str(e)
3505 # if failed:
3506 # for net in net_list[datacenter_key]:
3507 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3508 #
3509 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
3510 # # TODO: update nets inside a vnf
3511 # for net in instanceDict['nets']:
3512 # net_id = net['vim_net_id']
3513 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
3514 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
3515 # 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'):
3516 # net['status'] = net_dict[net_id]['status']
3517 # net['error_msg'] = net_dict[net_id].get('error_msg')
3518 # net['vim_info'] = net_dict[net_id].get('vim_info')
3519 # # 5.1. Update in openmano DB the nets whose status changed
3520 # try:
3521 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
3522 # nets_notupdated.remove(net["uuid"])
3523 # if updated>0:
3524 # nets_updated.append(net["uuid"])
3525 # except db_base_Exception as e:
3526 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3527 #
3528 # # Returns appropriate output
3529 # #print "nfvo.refresh_instance finishes"
3530 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
3531 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01003532 instance_id = instanceDict['uuid']
tierno867ffe92017-03-27 12:50:34 +02003533 # if len(vms_notupdated)+len(nets_notupdated)>0:
3534 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
3535 # 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 +01003536
tiernoae4a8d12016-07-08 12:30:39 +02003537 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01003538
3539def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02003540 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02003541 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01003542 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3543
tiernoae4a8d12016-07-08 12:30:39 +02003544 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02003545 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
3546 if len(vims) == 0:
3547 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01003548 myvim = vims.values()[0]
tierno42026a02017-02-10 15:13:40 +01003549
tierno868220c2017-09-26 00:11:05 +02003550 if action_dict.get("create-vdu"):
3551 for vdu in action_dict["create-vdu"]:
3552 vdu_id = vdu.get("vdu-id")
3553 vdu_count = vdu.get("count", 1)
3554 # get from database TODO
3555 # insert tasks TODO
3556 pass
tierno7edb6752016-03-21 17:37:52 +01003557
3558 input_vnfs = action_dict.pop("vnfs", [])
3559 input_vms = action_dict.pop("vms", [])
3560 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
3561 vm_result = {}
3562 vm_error = 0
3563 vm_ok = 0
3564 for sce_vnf in instanceDict['vnfs']:
3565 for vm in sce_vnf['vms']:
3566 if not action_over_all:
3567 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
tierno868220c2017-09-26 00:11:05 +02003568 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
tierno7edb6752016-03-21 17:37:52 +01003569 continue
tiernoae4a8d12016-07-08 12:30:39 +02003570 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02003571 if "add_public_key" in action_dict:
3572 mgmt_access = {}
3573 if sce_vnf.get('mgmt_access'):
3574 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
3575 ssh_access = mgmt_access['config-access']['ssh-access']
3576 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
tierno42026a02017-02-10 15:13:40 +01003577 try:
gcalvinoe580c7d2017-09-22 14:09:51 +02003578 if ssh_access['required'] and ssh_access['default-user']:
3579 if 'ip_address' in vm:
3580 mgmt_ip = vm['ip_address'].split(';')
3581 password = mgmt_access['config-access'].get('password')
3582 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
3583 myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'],
3584 action_dict['add_public_key'],
3585 password=password, ro_key=priv_RO_key)
3586 else:
3587 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
3588 HTTP_Internal_Server_Error)
3589 except KeyError:
3590 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
3591 HTTP_Internal_Server_Error)
3592 else:
3593 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
3594 HTTP_Internal_Server_Error)
3595 else:
3596 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
3597 if "console" in action_dict:
3598 if not global_config["http_console_proxy"]:
tierno20fc2a22016-08-19 17:02:35 +02003599 vm_result[ vm['uuid'] ] = {"vim_result": 200,
3600 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3601 protocol=data["protocol"],
gcalvinoe580c7d2017-09-22 14:09:51 +02003602 ip = data["server"],
3603 port = data["port"],
tierno20fc2a22016-08-19 17:02:35 +02003604 suffix = data["suffix"]),
3605 "name":vm['name']
3606 }
3607 vm_ok +=1
gcalvinoe580c7d2017-09-22 14:09:51 +02003608 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
3609 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
3610 "description": "this console is only reachable by local interface",
3611 "name":vm['name']
3612 }
tierno20fc2a22016-08-19 17:02:35 +02003613 vm_error+=1
gcalvinoe580c7d2017-09-22 14:09:51 +02003614 else:
3615 #print "console data", data
3616 try:
3617 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
3618 vm_result[ vm['uuid'] ] = {"vim_result": 200,
3619 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3620 protocol=data["protocol"],
3621 ip = global_config["http_console_host"],
3622 port = console_thread.port,
3623 suffix = data["suffix"]),
3624 "name":vm['name']
3625 }
3626 vm_ok +=1
3627 except NfvoException as e:
3628 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
3629 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02003630
gcalvinoe580c7d2017-09-22 14:09:51 +02003631 else:
3632 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
3633 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02003634 except vimconn.vimconnException as e:
3635 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
3636 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01003637
3638 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02003639 return vm_result
tierno7edb6752016-03-21 17:37:52 +01003640 else:
tierno351863c2016-07-23 01:46:03 +02003641 return vm_result
tierno42026a02017-02-10 15:13:40 +01003642
tierno868220c2017-09-26 00:11:05 +02003643def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
3644 filter={}
3645 if nfvo_tenant and nfvo_tenant != "any":
3646 filter["tenant_id"] = nfvo_tenant
3647 if instance_id and instance_id != "any":
3648 filter["instance_id"] = instance_id
3649 if action_id:
3650 filter["uuid"] = action_id
3651 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
3652 if not rows and action_id:
3653 raise NfvoException("Not found any action with this criteria", HTTP_Not_Found)
3654 return {"ations": rows}
3655
tiernob3d36742017-03-03 23:51:05 +01003656
tierno7edb6752016-03-21 17:37:52 +01003657def create_or_use_console_proxy_thread(console_server, console_port):
3658 #look for a non-used port
3659 console_thread_key = console_server + ":" + str(console_port)
3660 if console_thread_key in global_config["console_thread"]:
3661 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02003662 return global_config["console_thread"][console_thread_key]
tierno42026a02017-02-10 15:13:40 +01003663
tierno7edb6752016-03-21 17:37:52 +01003664 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02003665 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01003666 if port in global_config["console_ports"]:
3667 continue
3668 try:
3669 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
3670 clithread.start()
3671 global_config["console_thread"][console_thread_key] = clithread
3672 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02003673 return clithread
tierno7edb6752016-03-21 17:37:52 +01003674 except cli.ConsoleProxyExceptionPortUsed as e:
3675 #port used, try with onoher
3676 continue
3677 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02003678 raise NfvoException(str(e), HTTP_Bad_Request)
3679 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01003680
tiernob3d36742017-03-03 23:51:05 +01003681
tierno7edb6752016-03-21 17:37:52 +01003682def check_tenant(mydb, tenant_id):
3683 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02003684 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
3685 if not tenant:
3686 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
3687 return
tierno7edb6752016-03-21 17:37:52 +01003688
3689def new_tenant(mydb, tenant_dict):
tierno7edb6752016-03-21 17:37:52 +01003690
gcalvinoe580c7d2017-09-22 14:09:51 +02003691 tenant_uuid = str(uuid4())
3692 tenant_dict['uuid'] = tenant_uuid
3693 try:
3694 pub_key, priv_key = create_RO_keypair(tenant_uuid)
3695 tenant_dict['RO_pub_key'] = pub_key
3696 tenant_dict['encrypted_RO_priv_key'] = priv_key
gcalvinoc62cfa52017-10-05 18:21:25 +02003697 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
gcalvinoe580c7d2017-09-22 14:09:51 +02003698 except db_base_Exception as e:
3699 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), HTTP_Internal_Server_Error)
3700 return tenant_uuid
tiernob3d36742017-03-03 23:51:05 +01003701
tierno7edb6752016-03-21 17:37:52 +01003702def delete_tenant(mydb, tenant):
3703 #get nfvo_tenant info
tierno42026a02017-02-10 15:13:40 +01003704
tiernof97fd272016-07-11 14:32:37 +02003705 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
3706 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
3707 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01003708
tiernob3d36742017-03-03 23:51:05 +01003709
tierno7edb6752016-03-21 17:37:52 +01003710def new_datacenter(mydb, datacenter_descriptor):
3711 if "config" in datacenter_descriptor:
3712 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
tierno3ae39742016-09-07 12:17:51 +02003713 #Check that datacenter-type is correct
3714 datacenter_type = datacenter_descriptor.get("type", "openvim");
3715 module_info = None
3716 try:
3717 module = "vimconn_" + datacenter_type
tierno361275f2017-04-25 16:24:34 +02003718 pkg = __import__("osm_ro." + module)
3719 vim_conn = getattr(pkg, module)
3720 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
tierno3ae39742016-09-07 12:17:51 +02003721 except (IOError, ImportError):
tierno361275f2017-04-25 16:24:34 +02003722 # if module_info and module_info[0]:
3723 # file.close(module_info[0])
tierno3ae39742016-09-07 12:17:51 +02003724 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
tierno42026a02017-02-10 15:13:40 +01003725
gcalvinoc62cfa52017-10-05 18:21:25 +02003726 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
tiernof97fd272016-07-11 14:32:37 +02003727 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01003728
tiernob3d36742017-03-03 23:51:05 +01003729
tierno7edb6752016-03-21 17:37:52 +01003730def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
tierno8fe7a492017-07-11 13:50:04 +02003731 # obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02003732 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno8fe7a492017-07-11 13:50:04 +02003733
3734 # edit data
tiernof97fd272016-07-11 14:32:37 +02003735 datacenter_id = datacenter['uuid']
3736 where={'uuid': datacenter['uuid']}
tierno8fe7a492017-07-11 13:50:04 +02003737 remove_port_mapping = False
tierno7edb6752016-03-21 17:37:52 +01003738 if "config" in datacenter_descriptor:
tierno8fe7a492017-07-11 13:50:04 +02003739 if datacenter_descriptor['config'] != None:
tierno7edb6752016-03-21 17:37:52 +01003740 try:
3741 new_config_dict = datacenter_descriptor["config"]
3742 #delete null fields
3743 to_delete=[]
3744 for k in new_config_dict:
tierno8fe7a492017-07-11 13:50:04 +02003745 if new_config_dict[k] == None:
tierno7edb6752016-03-21 17:37:52 +01003746 to_delete.append(k)
tierno8fe7a492017-07-11 13:50:04 +02003747 if k == 'sdn-controller':
3748 remove_port_mapping = True
tierno42026a02017-02-10 15:13:40 +01003749
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003750 config_text = datacenter.get("config")
3751 if not config_text:
3752 config_text = '{}'
3753 config_dict = yaml.load(config_text)
tierno7edb6752016-03-21 17:37:52 +01003754 config_dict.update(new_config_dict)
3755 #delete null fields
3756 for k in to_delete:
3757 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02003758 except Exception as e:
3759 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno8fe7a492017-07-11 13:50:04 +02003760 if config_dict:
3761 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
3762 else:
3763 datacenter_descriptor["config"] = None
3764 if remove_port_mapping:
3765 try:
3766 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
3767 except ovimException as e:
3768 logger.error("Error deleting datacenter-port-mapping " + str(e))
3769
tiernof97fd272016-07-11 14:32:37 +02003770 mydb.update_rows('datacenters', datacenter_descriptor, where)
3771 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01003772
tiernob3d36742017-03-03 23:51:05 +01003773
tierno7edb6752016-03-21 17:37:52 +01003774def delete_datacenter(mydb, datacenter):
3775 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02003776 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
3777 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
tierno8fe7a492017-07-11 13:50:04 +02003778 try:
3779 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
3780 except ovimException as e:
3781 logger.error("Error deleting datacenter-port-mapping " + str(e))
tiernof97fd272016-07-11 14:32:37 +02003782 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01003783
tiernob3d36742017-03-03 23:51:05 +01003784
tierno8008c3a2016-10-13 15:34:28 +00003785def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
tierno9c22f2d2017-10-09 16:23:55 +02003786 # get datacenter info
tierno0ea2a7e2017-10-18 00:06:26 +02003787 try:
3788 datacenter_id = get_datacenter_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003789
tierno0ea2a7e2017-10-18 00:06:26 +02003790 create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False
tierno42026a02017-02-10 15:13:40 +01003791
tierno0ea2a7e2017-10-18 00:06:26 +02003792 # get nfvo_tenant info
3793 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
3794 if vim_tenant_name==None:
3795 vim_tenant_name=tenant_dict['name']
tierno42026a02017-02-10 15:13:40 +01003796
tierno0ea2a7e2017-10-18 00:06:26 +02003797 #check that this association does not exist before
3798 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
3799 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
3800 if len(tenants_datacenters)>0:
3801 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01003802
tierno0ea2a7e2017-10-18 00:06:26 +02003803 vim_tenant_id_exist_atdb=False
3804 if not create_vim_tenant:
3805 where_={"datacenter_id": datacenter_id}
3806 if vim_tenant_id!=None:
3807 where_["vim_tenant_id"] = vim_tenant_id
3808 if vim_tenant_name!=None:
3809 where_["vim_tenant_name"] = vim_tenant_name
3810 #check if vim_tenant_id is already at database
3811 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
3812 if len(datacenter_tenants_dict)>=1:
3813 datacenter_tenants_dict = datacenter_tenants_dict[0]
3814 vim_tenant_id_exist_atdb=True
3815 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
3816 else: #result=0
3817 datacenter_tenants_dict = {}
3818 #insert at table datacenter_tenants
3819 else: #if vim_tenant_id==None:
3820 #create tenant at VIM if not provided
3821 try:
3822 _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username,
3823 vim_passwd=vim_password)
3824 datacenter_name = myvim["name"]
3825 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
3826 except vimconn.vimconnException as e:
3827 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01003828 datacenter_tenants_dict = {}
tierno0ea2a7e2017-10-18 00:06:26 +02003829 datacenter_tenants_dict["created"]="true"
tierno42026a02017-02-10 15:13:40 +01003830
tierno0ea2a7e2017-10-18 00:06:26 +02003831 #fill datacenter_tenants table
3832 if not vim_tenant_id_exist_atdb:
3833 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
3834 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
3835 datacenter_tenants_dict["user"] = vim_username
3836 datacenter_tenants_dict["passwd"] = vim_password
3837 datacenter_tenants_dict["datacenter_id"] = datacenter_id
3838 if config:
3839 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
3840 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
3841 datacenter_tenants_dict["uuid"] = id_
tierno42026a02017-02-10 15:13:40 +01003842
tierno0ea2a7e2017-10-18 00:06:26 +02003843 #fill tenants_datacenters table
3844 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
3845 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
3846 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
3847 # create thread
3848 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data
3849 datacenter_name = myvim["name"]
3850 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
3851 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id,
3852 db=db, db_lock=db_lock, ovim=ovim)
3853 new_thread.start()
3854 thread_id = datacenter_tenants_dict["uuid"]
3855 vim_threads["running"][thread_id] = new_thread
3856 return datacenter_id
3857 except vimconn.vimconnException as e:
3858 raise NfvoException(str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01003859
tierno99314902017-04-26 13:23:09 +02003860
3861def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None,
3862 vim_username=None, vim_password=None, config=None):
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01003863 #Obtain the data of this datacenter_tenant_id
3864 vim_data = mydb.get_rows(
3865 SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
3866 "datacenter_tenants.passwd", "datacenter_tenants.config"),
3867 FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
3868 WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant,
3869 "tenants_datacenters.datacenter_id": datacenter_id})
3870
3871 logger.debug(str(vim_data))
3872 if len(vim_data) < 1:
3873 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict)
3874
3875 v = vim_data[0]
3876 if v['config']:
3877 v['config'] = yaml.load(v['config'])
3878
3879 if vim_tenant_id:
3880 v['vim_tenant_id'] = vim_tenant_id
3881 if vim_tenant_name:
3882 v['vim_tenant_name'] = vim_tenant_name
3883 if vim_username:
3884 v['user'] = vim_username
3885 if vim_password:
3886 v['passwd'] = vim_password
3887 if config:
3888 if not v['config']:
3889 v['config'] = {}
3890 v['config'].update(config)
3891
3892 logger.debug(str(v))
3893 deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'])
3894 associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'],
3895 vim_username=v['user'], vim_password=v['passwd'], config=v['config'])
3896
3897 return datacenter_id
tiernob3d36742017-03-03 23:51:05 +01003898
tierno7edb6752016-03-21 17:37:52 +01003899def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
tierno7edb6752016-03-21 17:37:52 +01003900 #get nfvo_tenant info
3901 if not tenant_id or tenant_id=="any":
3902 tenant_uuid = None
3903 else:
tiernof97fd272016-07-11 14:32:37 +02003904 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01003905 tenant_uuid = tenant_dict['uuid']
3906
tierno0ea2a7e2017-10-18 00:06:26 +02003907 datacenter_id = get_datacenter_uuid(mydb, tenant_uuid, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003908 #check that this association exist before
tierno0ea2a7e2017-10-18 00:06:26 +02003909 tenants_datacenter_dict={"datacenter_id": datacenter_id }
tierno7edb6752016-03-21 17:37:52 +01003910 if tenant_uuid:
3911 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02003912 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
3913 if len(tenant_datacenter_list)==0 and tenant_uuid:
3914 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01003915
3916 #delete this association
tiernof97fd272016-07-11 14:32:37 +02003917 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01003918
3919 #get vim_tenant info and deletes
3920 warning=''
3921 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02003922 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
3923 #try to delete vim:tenant
3924 try:
3925 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
3926 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01003927 #delete tenant at VIM if created by NFVO
tierno42026a02017-02-10 15:13:40 +01003928 try:
tierno0ea2a7e2017-10-18 00:06:26 +02003929 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02003930 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
3931 except vimconn.vimconnException as e:
3932 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
3933 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02003934 except db_base_Exception as e:
3935 logger.error("Cannot delete datacenter_tenants " + str(e))
tierno42026a02017-02-10 15:13:40 +01003936 pass # the error will be caused because dependencies, vim_tenant can not be deleted
tierno867ffe92017-03-27 12:50:34 +02003937 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
tierno42026a02017-02-10 15:13:40 +01003938 thread = vim_threads["running"][thread_id]
tierno868220c2017-09-26 00:11:05 +02003939 thread.insert_task("exit")
tierno42026a02017-02-10 15:13:40 +01003940 vim_threads["deleting"][thread_id] = thread
tiernof97fd272016-07-11 14:32:37 +02003941 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01003942
tiernob3d36742017-03-03 23:51:05 +01003943
tierno7edb6752016-03-21 17:37:52 +01003944def datacenter_action(mydb, tenant_id, datacenter, action_dict):
3945 #DEPRECATED
tierno42026a02017-02-10 15:13:40 +01003946 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003947 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003948
3949 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02003950 try:
tiernof97fd272016-07-11 14:32:37 +02003951 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02003952 #print content
3953 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02003954 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
3955 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01003956 #update nets Change from VIM format to NFVO format
3957 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02003958 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01003959 net_nfvo={'datacenter_id': datacenter_id}
3960 net_nfvo['name'] = net['name']
3961 #net_nfvo['description']= net['name']
3962 net_nfvo['vim_net_id'] = net['id']
3963 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3964 net_nfvo['shared'] = net['shared']
3965 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
3966 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02003967 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
3968 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
3969 return inserted
tierno7edb6752016-03-21 17:37:52 +01003970 elif 'net-edit' in action_dict:
3971 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02003972 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01003973 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01003974 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02003975 return result
tierno7edb6752016-03-21 17:37:52 +01003976 elif 'net-delete' in action_dict:
3977 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02003978 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tierno42026a02017-02-10 15:13:40 +01003979 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01003980 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02003981 return result
tierno7edb6752016-03-21 17:37:52 +01003982
3983 else:
tiernof97fd272016-07-11 14:32:37 +02003984 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01003985
tiernob3d36742017-03-03 23:51:05 +01003986
tierno7edb6752016-03-21 17:37:52 +01003987def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
3988 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003989 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01003990
tierno42fcc3b2016-07-06 17:20:40 +02003991 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tierno42026a02017-02-10 15:13:40 +01003992 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01003993 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02003994 return result
tierno7edb6752016-03-21 17:37:52 +01003995
tiernob3d36742017-03-03 23:51:05 +01003996
tierno7edb6752016-03-21 17:37:52 +01003997def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
3998 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00003999 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004000 filter_dict={}
4001 if action_dict:
4002 action_dict = action_dict["netmap"]
4003 if 'vim_id' in action_dict:
4004 filter_dict["id"] = action_dict['vim_id']
4005 if 'vim_name' in action_dict:
4006 filter_dict["name"] = action_dict['vim_name']
4007 else:
4008 filter_dict["shared"] = True
tierno42026a02017-02-10 15:13:40 +01004009
tiernoae4a8d12016-07-08 12:30:39 +02004010 try:
tiernof97fd272016-07-11 14:32:37 +02004011 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02004012 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02004013 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
4014 raise NfvoException(str(e), HTTP_Internal_Server_Error)
4015 if len(vim_nets)>1 and action_dict:
4016 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
4017 elif len(vim_nets)==0: # and action_dict:
4018 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01004019 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02004020 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01004021 net_nfvo={'datacenter_id': datacenter_id}
4022 if action_dict and "name" in action_dict:
4023 net_nfvo['name'] = action_dict['name']
4024 else:
4025 net_nfvo['name'] = net['name']
4026 #net_nfvo['description']= net['name']
4027 net_nfvo['vim_net_id'] = net['id']
4028 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4029 net_nfvo['shared'] = net['shared']
4030 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02004031 try:
4032 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01004033 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02004034 net_nfvo["uuid"] = net_id
4035 except db_base_Exception as e:
4036 if action_dict:
4037 raise
4038 else:
4039 net_nfvo["status"] = "FAIL: " + str(e)
tierno42026a02017-02-10 15:13:40 +01004040 net_list.append(net_nfvo)
4041 return net_list
tierno7edb6752016-03-21 17:37:52 +01004042
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004043def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
4044 # obtain all network data
4045 try:
4046 if utils.check_valid_uuid(network_id):
4047 filter_dict = {"id": network_id}
4048 else:
4049 filter_dict = {"name": network_id}
4050
4051 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
4052 network = myvim.get_network_list(filter_dict=filter_dict)
4053 except vimconn.vimconnException as e:
tiernof1ba57e2017-09-07 12:23:19 +02004054 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 +02004055
4056 # ensure the network is defined
4057 if len(network) == 0:
4058 raise NfvoException("Network {} is not present in the system".format(network_id),
4059 HTTP_Bad_Request)
4060
4061 # ensure there is only one network with the provided name
4062 if len(network) > 1:
4063 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request)
4064
4065 # ensure it is a dataplane network
4066 if network[0]['type'] != 'data':
4067 return None
4068
4069 # ensure we use the id
4070 network_id = network[0]['id']
4071
4072 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
4073 # and with instance_scenario_id==NULL
4074 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
4075 search_dict = {'vim_net_id': network_id}
4076
4077 try:
4078 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
4079 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
4080 except db_base_Exception as e:
4081 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
4082 network_id) + str(e), HTTP_Internal_Server_Error)
4083
4084 sdn_net_counter = 0
4085 for net in result:
4086 if net['sdn_net_id'] != None:
4087 sdn_net_counter+=1
4088 sdn_net_id = net['sdn_net_id']
4089
4090 if sdn_net_counter == 0:
4091 return None
4092 elif sdn_net_counter == 1:
4093 return sdn_net_id
4094 else:
4095 raise NfvoException("More than one SDN network is associated to vim network {}".format(
4096 network_id), HTTP_Internal_Server_Error)
4097
4098def get_sdn_controller_id(mydb, datacenter):
4099 # Obtain sdn controller id
4100 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
4101 if not config:
4102 return None
4103
4104 return yaml.load(config).get('sdn-controller')
4105
4106def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
4107 try:
4108 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
4109 if not sdn_network_id:
4110 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error)
4111
4112 #Obtain sdn controller id
4113 controller_id = get_sdn_controller_id(mydb, datacenter)
4114 if not controller_id:
4115 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error)
4116
4117 #Obtain sdn controller info
4118 sdn_controller = ovim.show_of_controller(controller_id)
4119
4120 port_data = {
4121 'name': 'external_port',
4122 'net_id': sdn_network_id,
4123 'ofc_id': controller_id,
4124 'switch_dpid': sdn_controller['dpid'],
4125 'switch_port': descriptor['port']
4126 }
4127
4128 if 'vlan' in descriptor:
4129 port_data['vlan'] = descriptor['vlan']
4130 if 'mac' in descriptor:
4131 port_data['mac'] = descriptor['mac']
4132
4133 result = ovim.new_port(port_data)
4134 except ovimException as e:
4135 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
4136 sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error)
4137 except db_base_Exception as e:
4138 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
4139 network_id) + str(e), HTTP_Internal_Server_Error)
4140
4141 return 'Port uuid: '+ result
4142
4143def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
4144 if port_id:
4145 filter = {'uuid': port_id}
4146 else:
4147 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
4148 if not sdn_network_id:
4149 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
4150 HTTP_Internal_Server_Error)
4151 #in case no port_id is specified only ports marked as 'external_port' will be detached
4152 filter = {'name': 'external_port', 'net_id': sdn_network_id}
4153
4154 try:
4155 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
4156 except ovimException as e:
4157 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
4158 HTTP_Internal_Server_Error)
4159
4160 if len(port_list) == 0:
4161 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
4162 HTTP_Bad_Request)
4163
4164 port_uuid_list = []
4165 for port in port_list:
4166 try:
4167 port_uuid_list.append(port['uuid'])
4168 ovim.delete_port(port['uuid'])
4169 except ovimException as e:
4170 raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error)
4171
4172 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
tiernob3d36742017-03-03 23:51:05 +01004173
tierno7edb6752016-03-21 17:37:52 +01004174def vim_action_get(mydb, tenant_id, datacenter, item, name):
4175 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004176 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01004177 filter_dict={}
4178 if name:
tierno42fcc3b2016-07-06 17:20:40 +02004179 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01004180 filter_dict["id"] = name
4181 else:
4182 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02004183 try:
4184 if item=="networks":
4185 #filter_dict['tenant_id'] = myvim['tenant_id']
4186 content = myvim.get_network_list(filter_dict=filter_dict)
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004187
4188 if len(content) == 0:
4189 raise NfvoException("Network {} is not present in the system. ".format(name),
4190 HTTP_Bad_Request)
4191
4192 #Update the networks with the attached ports
4193 for net in content:
4194 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
4195 if sdn_network_id != None:
4196 try:
4197 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
4198 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
4199 except ovimException as e:
4200 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error)
4201 #Remove field name and if port name is external_port save it as 'type'
4202 for port in port_list:
4203 if port['name'] == 'external_port':
4204 port['type'] = "External"
4205 del port['name']
4206 net['sdn_network_id'] = sdn_network_id
4207 net['sdn_attached_ports'] = port_list
4208
tiernoae4a8d12016-07-08 12:30:39 +02004209 elif item=="tenants":
4210 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01004211 elif item == "images":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004212
tierno4540ea52017-01-18 17:44:32 +01004213 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02004214 else:
tiernof97fd272016-07-11 14:32:37 +02004215 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02004216 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02004217 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02004218 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02004219 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02004220 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 +02004221 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02004222 else:
tiernof97fd272016-07-11 14:32:37 +02004223 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02004224 except vimconn.vimconnException as e:
4225 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02004226 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno42026a02017-02-10 15:13:40 +01004227
tiernob3d36742017-03-03 23:51:05 +01004228
tierno7edb6752016-03-21 17:37:52 +01004229def vim_action_delete(mydb, tenant_id, datacenter, item, name):
4230 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02004231 if tenant_id == "any":
4232 tenant_id=None
4233
tiernoa2793912016-10-04 08:15:08 +00004234 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02004235 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02004236 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
4237 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02004238 items = content.values()[0]
4239 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02004240 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02004241 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02004242 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02004243 else: # it is a dict
4244 item_id = items["id"]
4245 item_name = str(items.get("name"))
tierno42026a02017-02-10 15:13:40 +01004246
tiernoae4a8d12016-07-08 12:30:39 +02004247 try:
4248 if item=="networks":
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004249 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
4250 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
4251 if sdn_network_id != None:
4252 #Delete any port attachment to this network
4253 try:
4254 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
4255 except ovimException as e:
4256 raise NfvoException(
4257 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
4258 HTTP_Internal_Server_Error)
4259
4260 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
4261 for port in port_list:
4262 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
4263
4264 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
4265 try:
4266 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
4267 except db_base_Exception as e:
4268 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
4269 str(e), HTTP_Internal_Server_Error)
4270
4271 #Delete the SDN network
4272 try:
4273 ovim.delete_network(sdn_network_id)
4274 except ovimException as e:
4275 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
4276 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
4277 HTTP_Internal_Server_Error)
4278
tiernoae4a8d12016-07-08 12:30:39 +02004279 content = myvim.delete_network(item_id)
4280 elif item=="tenants":
4281 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01004282 elif item == "images":
4283 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02004284 else:
tierno42026a02017-02-10 15:13:40 +01004285 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02004286 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02004287 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
4288 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02004289
tiernof97fd272016-07-11 14:32:37 +02004290 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno42026a02017-02-10 15:13:40 +01004291
tiernob3d36742017-03-03 23:51:05 +01004292
tierno7edb6752016-03-21 17:37:52 +01004293def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
4294 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00004295 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02004296 if tenant_id == "any":
4297 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00004298 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02004299 try:
4300 if item=="networks":
4301 net = descriptor["network"]
4302 net_name = net.pop("name")
4303 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02004304 net_public = net.pop("shared", False)
4305 net_ipprofile = net.pop("ip_profile", None)
tiernoa7d34d02017-02-23 14:42:07 +01004306 net_vlan = net.pop("vlan", None)
4307 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 +02004308
4309 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
4310 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
4311 try:
4312 sdn_network = {}
4313 sdn_network['vlan'] = net_vlan
4314 sdn_network['type'] = net_type
4315 sdn_network['name'] = net_name
4316 ovim_content = ovim.new_network(sdn_network)
4317 except ovimException as e:
4318 self.logger.error("ovimException creating SDN network={} ".format(
4319 sdn_network) + str(e), exc_info=True)
4320 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
4321 HTTP_Internal_Server_Error)
4322
4323 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
4324 # use instance_scenario_id=None to distinguish from real instaces of nets
4325 correspondence = {'instance_scenario_id': None, 'sdn_net_id': ovim_content, 'vim_net_id': content}
4326 #obtain datacenter_tenant_id
4327 correspondence['datacenter_tenant_id'] = mydb.get_rows(SELECT=('uuid',), FROM='datacenter_tenants', WHERE={'datacenter_id': datacenter})[0]['uuid']
4328
4329 try:
4330 mydb.new_row('instance_nets', correspondence, add_uuid=True)
4331 except db_base_Exception as e:
4332 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
4333 str(e), HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02004334 elif item=="tenants":
4335 tenant = descriptor["tenant"]
4336 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
4337 else:
tierno42026a02017-02-10 15:13:40 +01004338 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02004339 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02004340 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02004341
tierno7edb6752016-03-21 17:37:52 +01004342 return vim_action_get(mydb, tenant_id, datacenter, item, content)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004343
4344def sdn_controller_create(mydb, tenant_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004345 data = ovim.new_of_controller(sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004346 logger.debug('New SDN controller created with uuid {}'.format(data))
4347 return data
4348
4349def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004350 data = ovim.edit_of_controller(controller_id, sdn_controller)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004351 msg = 'SDN controller {} updated'.format(data)
4352 logger.debug(msg)
4353 return msg
4354
4355def sdn_controller_list(mydb, tenant_id, controller_id=None):
4356 if controller_id == None:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004357 data = ovim.get_of_controllers()
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004358 else:
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004359 data = ovim.show_of_controller(controller_id)
4360
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004361 msg = 'SDN controller list:\n {}'.format(data)
4362 logger.debug(msg)
4363 return data
4364
4365def sdn_controller_delete(mydb, tenant_id, controller_id):
4366 select_ = ('uuid', 'config')
4367 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
4368 for datacenter in datacenters:
4369 if datacenter['config']:
4370 config = yaml.load(datacenter['config'])
4371 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
4372 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict)
4373
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004374 data = ovim.delete_of_controller(controller_id)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004375 msg = 'SDN controller {} deleted'.format(data)
4376 logger.debug(msg)
4377 return msg
4378
4379def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
4380 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
4381 if len(controller) < 1:
4382 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found)
4383
4384 try:
4385 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
4386 except:
4387 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request)
4388
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004389 sdn_controller = ovim.show_of_controller(sdn_controller_id)
4390 switch_dpid = sdn_controller["dpid"]
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004391
4392 maps = list()
4393 for compute_node in sdn_port_mapping:
4394 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
4395 element = dict()
4396 element["compute_node"] = compute_node["compute_node"]
4397 for port in compute_node["ports"]:
4398 element["pci"] = port.get("pci")
4399 element["switch_port"] = port.get("switch_port")
4400 element["switch_mac"] = port.get("switch_mac")
4401 if not element["pci"] or not (element["switch_port"] or element["switch_mac"]):
4402 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
4403 " or 'switch_mac'", HTTP_Bad_Request)
4404 maps.append(dict(element))
4405
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004406 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 +01004407
4408def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
Pablo Montes Moreno7e0e9c62017-03-27 12:42:32 +02004409 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004410
4411 result = {
4412 "sdn-controller": None,
4413 "datacenter-id": datacenter_id,
4414 "dpid": None,
4415 "ports_mapping": list()
4416 }
4417
4418 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
4419 if datacenter['config']:
4420 config = yaml.load(datacenter['config'])
4421 if 'sdn-controller' in config:
4422 controller_id = config['sdn-controller']
4423 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
4424 result["sdn-controller"] = controller_id
4425 result["dpid"] = sdn_controller["dpid"]
4426
Pablo Montes Moreno6aa0b2b2017-05-23 18:33:12 +02004427 if result["sdn-controller"] == None:
4428 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request)
4429 if result["dpid"] == None:
4430 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
4431 HTTP_Internal_Server_Error)
Pablo Montes Moreno3fbff9b2017-03-08 11:28:15 +01004432
4433 if len(maps) == 0:
4434 return result
4435
4436 ports_correspondence_dict = dict()
4437 for link in maps:
4438 if result["sdn-controller"] != link["ofc_id"]:
4439 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error)
4440 if result["dpid"] != link["switch_dpid"]:
4441 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error)
4442 element = dict()
4443 element["pci"] = link["pci"]
4444 if link["switch_port"]:
4445 element["switch_port"] = link["switch_port"]
4446 if link["switch_mac"]:
4447 element["switch_mac"] = link["switch_mac"]
4448
4449 if not link["compute_node"] in ports_correspondence_dict:
4450 content = dict()
4451 content["compute_node"] = link["compute_node"]
4452 content["ports"] = list()
4453 ports_correspondence_dict[link["compute_node"]] = content
4454
4455 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
4456
4457 for key in sorted(ports_correspondence_dict):
4458 result["ports_mapping"].append(ports_correspondence_dict[key])
4459
4460 return result
4461
4462def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
tierno639520f2017-04-05 19:55:36 +02004463 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
gcalvinoe580c7d2017-09-22 14:09:51 +02004464
4465def create_RO_keypair(tenant_id):
4466 """
4467 Creates a public / private keys for a RO tenant and returns their values
4468 Params:
4469 tenant_id: ID of the tenant
4470 Return:
4471 public_key: Public key for the RO tenant
4472 private_key: Encrypted private key for RO tenant
4473 """
4474
4475 bits = 2048
4476 key = RSA.generate(bits)
4477 try:
4478 public_key = key.publickey().exportKey('OpenSSH')
4479 if isinstance(public_key, ValueError):
4480 raise NfvoException("Unable to create public key: {}".format(public_key), HTTP_Internal_Server_Error)
4481 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
4482 except (ValueError, NameError) as e:
4483 raise NfvoException("Unable to create private key: {}".format(e), HTTP_Internal_Server_Error)
4484 return public_key, private_key
4485
4486def decrypt_key (key, tenant_id):
4487 """
4488 Decrypts an encrypted RSA key
4489 Params:
4490 key: Private key to be decrypted
4491 tenant_id: ID of the tenant
4492 Return:
4493 unencrypted_key: Unencrypted private key for RO tenant
4494 """
4495 try:
4496 key = RSA.importKey(key,tenant_id)
4497 unencrypted_key = key.exportKey('PEM')
4498 if isinstance(unencrypted_key, ValueError):
4499 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), HTTP_Internal_Server_Error)
4500 except ValueError as e:
4501 raise NfvoException("Unable to decrypt the private key: {}".format(e), HTTP_Internal_Server_Error)
4502 return unencrypted_key