X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=functest%2Futils%2Fopenstack_tacker.py;h=d5d7858c50385186e3c62c5079e74692a47ca4a1;hb=refs%2Fchanges%2F37%2F28837%2F2;hp=3e0c9cf455faa3408bdca0e8be449eae50676f78;hpb=aa3b9e80b769a083d41b41d61daf1cf7869aa98b;p=functest.git diff --git a/functest/utils/openstack_tacker.py b/functest/utils/openstack_tacker.py index 3e0c9cf45..d5d7858c5 100644 --- a/functest/utils/openstack_tacker.py +++ b/functest/utils/openstack_tacker.py @@ -15,14 +15,14 @@ from tackerclient.v1_0 import client as tackerclient import functest.utils.functest_logger as ft_logger import functest.utils.openstack_utils as os_utils -import yaml +import time logger = ft_logger.Logger("tacker_utils").getLogger() -def get_tacker_client(): - creds_tacker = os_utils.get_credentials('tacker') - return tackerclient.Client(**creds_tacker) +def get_tacker_client(other_creds={}): + sess = os_utils.get_session(other_creds) + return tackerclient.Client(session=sess) # ********************************************* @@ -33,6 +33,7 @@ def get_id_from_name(tacker_client, resource_type, resource_name): req_params = {'fields': 'id', 'name': resource_name} endpoint = '/{0}s'.format(resource_type) resp = tacker_client.get(endpoint, params=req_params) + endpoint = endpoint.replace('-', '_') return resp[endpoint[1:]][0]['id'] except Exception, e: logger.error("Error [get_id_from_name(tacker_client, " @@ -53,7 +54,7 @@ def get_sfc_id(tacker_client, sfc_name): def get_sfc_classifier_id(tacker_client, sfc_clf_name): - return get_id_from_name(tacker_client, 'sfc_classifier', sfc_clf_name) + return get_id_from_name(tacker_client, 'sfc-classifier', sfc_clf_name) def list_vnfds(tacker_client, verbose=False): @@ -72,8 +73,10 @@ def create_vnfd(tacker_client, tosca_file=None): vnfd_body = {} if tosca_file is not None: with open(tosca_file) as tosca_fd: - vnfd_body = yaml.safe_load(tosca_fd) - return tacker_client.create_vnfd(body=vnfd_body) + vnfd_body = tosca_fd.read() + logger.info('VNFD template:\n{0}'.format(vnfd_body)) + return tacker_client.create_vnfd( + body={"vnfd": {"attributes": {"vnfd": vnfd_body}}}) except Exception, e: logger.error("Error [create_vnfd(tacker_client, '%s')]: %s" % (tosca_file, e)) @@ -105,7 +108,8 @@ def list_vnfs(tacker_client, verbose=False): return None -def create_vnf(tacker_client, vnf_name, vnfd_id=None, vnfd_name=None): +def create_vnf(tacker_client, vnf_name, vnfd_id=None, + vnfd_name=None, param_file=None): try: vnf_body = { 'vnf': { @@ -113,6 +117,10 @@ def create_vnf(tacker_client, vnf_name, vnfd_id=None, vnfd_name=None): 'name': vnf_name } } + if param_file is not None: + with open(param_file) as f: + params = f.read() + vnf_body['vnf']['attributes']['param_values'] = params if vnfd_id is not None: vnf_body['vnf']['vnfd_id'] = vnfd_id else: @@ -121,11 +129,44 @@ def create_vnf(tacker_client, vnf_name, vnfd_id=None, vnfd_name=None): vnf_body['vnf']['vnfd_id'] = get_vnfd_id(tacker_client, vnfd_name) return tacker_client.create_vnf(body=vnf_body) except Exception, e: - logger.error("error [create_vnf(tacker_client, '%s', '%s', '%s')]: %s" + logger.error("error [create_vnf(tacker_client," + " '%s', '%s', '%s')]: %s" % (vnf_name, vnfd_id, vnfd_name, e)) return None +def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None): + try: + _id = None + if vnf_id is not None: + _id = vnf_id + elif vnf_name is not None: + while _id is None: + try: + _id = get_vnf_id(tacker_client, vnf_name) + except: + logger.error("Bazinga") + else: + raise Exception('You must specify vnf_id or vnf_name') + while True: + vnf = [v for v in list_vnfs(tacker_client, verbose=True)['vnfs'] + if v['id'] == _id] + vnf = vnf[0] + logger.info('Waiting for vnf {0}'.format(str(vnf))) + if vnf['status'] == 'ERROR': + raise Exception('Error when booting vnf %s' % _id) + elif vnf['status'] == 'PENDING_CREATE': + time.sleep(3) + continue + else: + break + return _id + except Exception, e: + logger.error("error [wait_for_vnf(tacker_client, '%s', '%s')]: %s" + % (vnf_id, vnf_name, e)) + return None + + def delete_vnf(tacker_client, vnf_id=None, vnf_name=None): try: vnf = vnf_id @@ -153,7 +194,8 @@ def list_sfcs(tacker_client, verbose=False): def create_sfc(tacker_client, sfc_name, chain_vnf_ids=None, - chain_vnf_names=None): + chain_vnf_names=None, + symmetrical=False): try: sfc_body = { 'sfc': { @@ -162,6 +204,8 @@ def create_sfc(tacker_client, sfc_name, 'chain': [] } } + if symmetrical: + sfc_body['sfc']['symmetrical'] = True if chain_vnf_ids is not None: sfc_body['sfc']['chain'] = chain_vnf_ids else: @@ -171,7 +215,8 @@ def create_sfc(tacker_client, sfc_name, for name in chain_vnf_names] return tacker_client.create_sfc(body=sfc_body) except Exception, e: - logger.error("error [create_sfc(tacker_client, '%s', '%s', '%s')]: %s" + logger.error("error [create_sfc(tacker_client," + " '%s', '%s', '%s')]: %s" % (sfc_name, chain_vnf_ids, chain_vnf_names, e)) return None @@ -190,7 +235,7 @@ def delete_sfc(tacker_client, sfc_id=None, sfc_name=None): return None -def list_sfc_clasifiers(tacker_client, verbose=False): +def list_sfc_classifiers(tacker_client, verbose=False): try: sfc_clfs = tacker_client.list_sfc_classifiers(retrieve_all=True) if not verbose: @@ -224,11 +269,13 @@ def create_sfc_classifier(tacker_client, sfc_clf_name, sfc_id=None, else: if sfc_name is None: raise Exception('You need to provide an SFC id or name') - sfc_clf_body['sfc']['chain'] = get_sfc_id(tacker_client, sfc_name) + sfc_clf_body['sfc_classifier']['chain'] = get_sfc_id( + tacker_client, sfc_name) return tacker_client.create_sfc_classifier(body=sfc_clf_body) except Exception, e: - logger.error("error [create_sfc_classifier(tacker_client, '%s', '%s', " - "'%s')]: %s" % (sfc_clf_name, sfc_id, sfc_name, match, e)) + logger.error("error [create_sfc_classifier(tacker_client," + " '%s', '%s','%s', '%s')]: '%s'" + % (sfc_clf_name, sfc_id, sfc_name, str(match), e)) return None