aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDimitrios Markou <mardim@intracom-telecom.com>2017-08-25 17:57:26 +0300
committerDimitrios Markou <mardim@intracom-telecom.com>2017-09-01 11:29:49 +0300
commitb864bfbdff816dc621e5b4cd91431ec9ac4a47c0 (patch)
tree06db2daa7745bc63a3b7c8135e6e77ff6d84db80
parent350f6be81a887838e02748d2ee66ebf27d4341f8 (diff)
Add new Tacker library
Change-Id: I710e6f5da92ddfddd9111c6c53274b92d76d7449 Signed-off-by: Dimitrios Markou <mardim@intracom-telecom.com>
-rw-r--r--sfc/lib/openstack_tacker.py190
-rw-r--r--sfc/tests/functest/sfc_one_chain_two_service_functions.py2
-rw-r--r--sfc/tests/functest/sfc_symmetric_chain.py2
-rw-r--r--sfc/tests/functest/sfc_two_chains_SSH_and_HTTP.py2
4 files changed, 193 insertions, 3 deletions
diff --git a/sfc/lib/openstack_tacker.py b/sfc/lib/openstack_tacker.py
new file mode 100644
index 00000000..7b0f79a2
--- /dev/null
+++ b/sfc/lib/openstack_tacker.py
@@ -0,0 +1,190 @@
+import logging
+import os
+import time
+
+from tackerclient.tacker import client as tackerclient
+from functest.utils import openstack_utils as os_utils
+
+
+logger = logging.getLogger(__name__)
+
+DEFAULT_TACKER_API_VERSION = '1.0'
+
+
+def get_tacker_client_version():
+ api_version = os.getenv('OS_TACKER_API_VERSION')
+ if api_version is not None:
+ logger.info("OS_TACKER_API_VERSION is set in env as '%s'", api_version)
+ return api_version
+ return DEFAULT_TACKER_API_VERSION
+
+
+def get_tacker_client(other_creds={}):
+ sess = os_utils.get_session(other_creds)
+ return tackerclient.Client(get_tacker_client_version(), session=sess)
+
+
+def get_id_from_name(tacker_client, resource_type, resource_name):
+ try:
+ 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, "
+ "resource_type, resource_name)]: %s" % e)
+ return None
+
+
+def get_vnfd_id(tacker_client, vnfd_name):
+ return get_id_from_name(tacker_client, 'vnfd', vnfd_name)
+
+
+def get_vnf_id(tacker_client, vnf_name, timeout=5):
+ vnf_id = None
+ while vnf_id is None and timeout >= 0:
+ vnf_id = get_id_from_name(tacker_client, 'vnf', vnf_name)
+ if vnf_id is None:
+ logger.info("Could not retrieve ID for vnf with name [%s]."
+ " Retrying." % vnf_name)
+ time.sleep(1)
+ timeout -= 1
+ return vnf_id
+
+
+def list_vnfds(tacker_client, verbose=False):
+ try:
+ vnfds = tacker_client.list_vnfds(retrieve_all=True)
+ if not verbose:
+ vnfds = [vnfd['id'] for vnfd in vnfds['vnfds']]
+ return vnfds
+ except Exception, e:
+ logger.error("Error [list_vnfds(tacker_client)]: %s" % e)
+ return None
+
+
+def create_vnfd(tacker_client, tosca_file=None):
+ try:
+ vnfd_body = {}
+ if tosca_file is not None:
+ with open(tosca_file) as tosca_fd:
+ 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))
+ return None
+
+
+def delete_vnfd(tacker_client, vnfd_id=None, vnfd_name=None):
+ try:
+ vnfd = vnfd_id
+ if vnfd is None:
+ if vnfd_name is None:
+ raise Exception('You need to provide VNFD id or VNFD name')
+ vnfd = get_vnfd_id(tacker_client, vnfd_name)
+ return tacker_client.delete_vnfd(vnfd)
+ except Exception, e:
+ logger.error("Error [delete_vnfd(tacker_client, '%s', '%s')]: %s"
+ % (vnfd_id, vnfd_name, e))
+ return None
+
+
+def list_vnfs(tacker_client, verbose=False):
+ try:
+ vnfs = tacker_client.list_vnfs(retrieve_all=True)
+ if not verbose:
+ vnfs = [vnf['id'] for vnf in vnfs['vnfs']]
+ return vnfs
+ except Exception, e:
+ logger.error("Error [list_vnfs(tacker_client)]: %s" % e)
+ return None
+
+
+def create_vnf(tacker_client, vnf_name, vnfd_id=None,
+ vnfd_name=None, param_file=None):
+ try:
+ vnf_body = {
+ 'vnf': {
+ 'attributes': {},
+ 'name': vnf_name
+ }
+ }
+ if param_file is not None:
+ params = 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:
+ if vnfd_name is None:
+ raise Exception('vnfd id or vnfd name is required')
+ 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"
+ % (vnf_name, vnfd_id, vnfd_name, e))
+ return None
+
+
+def get_vnf(tacker_client, vnf_id=None, vnf_name=None):
+ try:
+ if vnf_id is None and vnf_name is None:
+ raise Exception('You must specify vnf_id or vnf_name')
+
+ _id = get_vnf_id(tacker_client, vnf_name) if vnf_id is None else vnf_id
+
+ if _id is not None:
+ all_vnfs = list_vnfs(tacker_client, verbose=True)['vnfs']
+ return next((vnf for vnf in all_vnfs if vnf['id'] == _id), None)
+ else:
+ raise Exception('Could not retrieve ID from name [%s]' % vnf_name)
+
+ except Exception, e:
+ logger.error("Could not retrieve VNF [vnf_id=%s, vnf_name=%s] - %s"
+ % (vnf_id, vnf_name, e))
+ return None
+
+
+def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None, timeout=60):
+ try:
+ vnf = get_vnf(tacker_client, vnf_id, vnf_name)
+ if vnf is None:
+ raise Exception("Could not retrieve VNF - id='%s', name='%s'"
+ % vnf_id, vnf_name)
+ logger.info('Waiting for vnf {0}'.format(str(vnf)))
+ while vnf['status'] != 'ACTIVE' and timeout >= 0:
+ if vnf['status'] == 'ERROR':
+ raise Exception('Error when booting vnf %s' % vnf['id'])
+ elif vnf['status'] == 'PENDING_CREATE':
+ time.sleep(3)
+ timeout -= 3
+ vnf = get_vnf(tacker_client, vnf_id, vnf_name)
+
+ if (timeout < 0):
+ raise Exception('Timeout when booting vnf %s' % vnf['id'])
+
+ return vnf['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
+ if vnf is None:
+ if vnf_name is None:
+ raise Exception('You need to provide a VNF id or name')
+ vnf = get_vnf_id(tacker_client, vnf_name)
+ return tacker_client.delete_vnf(vnf)
+ except Exception, e:
+ logger.error("Error [delete_vnf(tacker_client, '%s', '%s')]: %s"
+ % (vnf_id, vnf_name, e))
+ return None
diff --git a/sfc/tests/functest/sfc_one_chain_two_service_functions.py b/sfc/tests/functest/sfc_one_chain_two_service_functions.py
index 695c8b36..7d421efc 100644
--- a/sfc/tests/functest/sfc_one_chain_two_service_functions.py
+++ b/sfc/tests/functest/sfc_one_chain_two_service_functions.py
@@ -12,7 +12,7 @@ import sys
import threading
import logging
-import functest.utils.openstack_tacker as os_tacker
+import sfc.lib.openstack_tacker as os_tacker
import functest.utils.openstack_utils as os_utils
import opnfv.utils.ovs_logger as ovs_log
diff --git a/sfc/tests/functest/sfc_symmetric_chain.py b/sfc/tests/functest/sfc_symmetric_chain.py
index cdcd8d3a..f515fab3 100644
--- a/sfc/tests/functest/sfc_symmetric_chain.py
+++ b/sfc/tests/functest/sfc_symmetric_chain.py
@@ -14,7 +14,7 @@ import sys
import threading
import logging
-import functest.utils.openstack_tacker as os_tacker
+import sfc.lib.openstack_tacker as os_tacker
import functest.utils.openstack_utils as os_utils
import opnfv.utils.ovs_logger as ovs_log
from opnfv.deployment.factory import Factory as DeploymentFactory
diff --git a/sfc/tests/functest/sfc_two_chains_SSH_and_HTTP.py b/sfc/tests/functest/sfc_two_chains_SSH_and_HTTP.py
index f0c5844e..068c842d 100644
--- a/sfc/tests/functest/sfc_two_chains_SSH_and_HTTP.py
+++ b/sfc/tests/functest/sfc_two_chains_SSH_and_HTTP.py
@@ -13,7 +13,7 @@ import sys
import threading
import logging
-import functest.utils.openstack_tacker as os_tacker
+import sfc.lib.openstack_tacker as os_tacker
import functest.utils.openstack_utils as os_utils
import opnfv.utils.ovs_logger as ovs_log