1 ###########################################################################
2 # Copyright (c) 2016 Ericsson AB and others.
3 # Author: George Paraskevopoulos <geopar@intracom-telecom.com>
5 # Wrappers for trozet's python-tackerclient v1.0
6 # (https://github.com/trozet/python-tackerclient)
8 # All rights reserved. This program and the accompanying materials
9 # are made available under the terms of the Apache License, Version 2.0
10 # which accompanies this distribution, and is available at
11 # http://www.apache.org/licenses/LICENSE-2.0
12 ##########################################################################
15 from tackerclient.v1_0 import client as tackerclient
16 import functest.utils.functest_logger as ft_logger
17 import functest.utils.openstack_utils as os_utils
20 logger = ft_logger.Logger("tacker_utils").getLogger()
23 def get_tacker_client():
24 creds_tacker = os_utils.get_credentials('tacker')
25 return tackerclient.Client(**creds_tacker)
28 # *********************************************
30 # *********************************************
31 def get_id_from_name(tacker_client, resource_type, resource_name):
33 req_params = {'fields': 'id', 'name': resource_name}
34 endpoint = '/{0}s'.format(resource_type)
35 resp = tacker_client.get(endpoint, params=req_params)
36 endpoint = endpoint.replace('-', '_')
37 return resp[endpoint[1:]][0]['id']
39 logger.error("Error [get_id_from_name(tacker_client, "
40 "resource_type, resource_name)]: %s" % e)
44 def get_vnfd_id(tacker_client, vnfd_name):
45 return get_id_from_name(tacker_client, 'vnfd', vnfd_name)
48 def get_vnf_id(tacker_client, vnf_name):
49 return get_id_from_name(tacker_client, 'vnf', vnf_name)
52 def get_sfc_id(tacker_client, sfc_name):
53 return get_id_from_name(tacker_client, 'sfc', sfc_name)
56 def get_sfc_classifier_id(tacker_client, sfc_clf_name):
57 return get_id_from_name(tacker_client, 'sfc-classifier', sfc_clf_name)
60 def list_vnfds(tacker_client, verbose=False):
62 vnfds = tacker_client.list_vnfds(retrieve_all=True)
64 vnfds = [vnfd['id'] for vnfd in vnfds['vnfds']]
67 logger.error("Error [list_vnfds(tacker_client)]: %s" % e)
71 def create_vnfd(tacker_client, tosca_file=None):
74 if tosca_file is not None:
75 with open(tosca_file) as tosca_fd:
76 vnfd_body = tosca_fd.read()
77 logger.error(vnfd_body)
78 return tacker_client.create_vnfd(
79 body={"vnfd": {"attributes": {"vnfd": vnfd_body}}})
81 logger.exception("Error [create_vnfd(tacker_client, '%s')]: %s"
86 def delete_vnfd(tacker_client, vnfd_id=None, vnfd_name=None):
91 raise Exception('You need to provide VNFD id or VNFD name')
92 vnfd = get_vnfd_id(tacker_client, vnfd_name)
93 return tacker_client.delete_vnfd(vnfd)
95 logger.error("Error [delete_vnfd(tacker_client, '%s', '%s')]: %s"
96 % (vnfd_id, vnfd_name, e))
100 def list_vnfs(tacker_client, verbose=False):
102 vnfs = tacker_client.list_vnfs(retrieve_all=True)
104 vnfs = [vnf['id'] for vnf in vnfs['vnfs']]
107 logger.error("Error [list_vnfs(tacker_client)]: %s" % e)
111 def create_vnf(tacker_client, vnf_name, vnfd_id=None, vnfd_name=None):
119 if vnfd_id is not None:
120 vnf_body['vnf']['vnfd_id'] = vnfd_id
122 if vnfd_name is None:
123 raise Exception('vnfd id or vnfd name is required')
124 vnf_body['vnf']['vnfd_id'] = get_vnfd_id(tacker_client, vnfd_name)
125 return tacker_client.create_vnf(body=vnf_body)
127 logger.error("error [create_vnf(tacker_client, '%s', '%s', '%s')]: %s"
128 % (vnf_name, vnfd_id, vnfd_name, e))
132 def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None):
135 if vnf_id is not None:
137 elif vnf_name is not None:
140 _id = get_vnf_id(tacker_client, vnf_name)
142 logger.error("Bazinga")
144 raise Exception('You must specify vnf_id or vnf_name')
146 vnf = [v for v in list_vnfs(tacker_client, verbose=True)['vnfs']
149 logger.info('Waiting for vnf {0}'.format(str(vnf)))
150 if vnf['status'] == 'ERROR':
151 raise Exception('Error when booting vnf %s' % _id)
152 elif vnf['status'] == 'PENDING_CREATE':
159 logger.error("error [wait_for_vnf(tacker_client, '%s', '%s')]: %s"
160 % (vnf_id, vnf_name, e))
164 def delete_vnf(tacker_client, vnf_id=None, vnf_name=None):
169 raise Exception('You need to provide a VNF id or name')
170 vnf = get_vnf_id(tacker_client, vnf_name)
171 return tacker_client.delete_vnf(vnf)
173 logger.error("Error [delete_vnf(tacker_client, '%s', '%s')]: %s"
174 % (vnf_id, vnf_name, e))
178 def list_sfcs(tacker_client, verbose=False):
180 sfcs = tacker_client.list_sfcs(retrieve_all=True)
182 sfcs = [sfc['id'] for sfc in sfcs['sfcs']]
185 logger.error("Error [list_sfcs(tacker_client)]: %s" % e)
189 def create_sfc(tacker_client, sfc_name,
191 chain_vnf_names=None):
200 if chain_vnf_ids is not None:
201 sfc_body['sfc']['chain'] = chain_vnf_ids
203 if chain_vnf_names is None:
204 raise Exception('You need to provide a chain of VNFs')
205 sfc_body['sfc']['chain'] = [get_vnf_id(tacker_client, name)
206 for name in chain_vnf_names]
207 return tacker_client.create_sfc(body=sfc_body)
209 logger.error("error [create_sfc(tacker_client, '%s', '%s', '%s')]: %s"
210 % (sfc_name, chain_vnf_ids, chain_vnf_names, e))
214 def delete_sfc(tacker_client, sfc_id=None, sfc_name=None):
219 raise Exception('You need to provide an SFC id or name')
220 sfc = get_sfc_id(tacker_client, sfc_name)
221 return tacker_client.delete_sfc(sfc)
223 logger.error("Error [delete_sfc(tacker_client, '%s', '%s')]: %s"
224 % (sfc_id, sfc_name, e))
228 def list_sfc_classifiers(tacker_client, verbose=False):
230 sfc_clfs = tacker_client.list_sfc_classifiers(retrieve_all=True)
232 sfc_clfs = [sfc_clf['id']
233 for sfc_clf in sfc_clfs['sfc_classifiers']]
236 logger.error("Error [list_sfc_classifiers(tacker_client)]: %s" % e)
240 def create_sfc_classifier(tacker_client, sfc_clf_name, sfc_id=None,
241 sfc_name=None, match={}):
244 # "source_port": "0",
252 'name': sfc_clf_name,
257 if sfc_id is not None:
258 sfc_clf_body['sfc_classifier']['chain'] = sfc_id
261 raise Exception('You need to provide an SFC id or name')
262 sfc_clf_body['sfc_classifier']['chain'] = get_sfc_id(
263 tacker_client, sfc_name)
264 return tacker_client.create_sfc_classifier(body=sfc_clf_body)
266 logger.error("error [create_sfc_classifier(tacker_client, '%s', '%s',"
267 " '%s', '%s')]: '%s'"
268 % (sfc_clf_name, sfc_id, sfc_name, str(match), e))
272 def delete_sfc_classifier(tacker_client,
278 if sfc_clf_name is None:
279 raise Exception('You need to provide an SFC'
280 'classifier id or name')
281 sfc_clf = get_sfc_classifier_id(tacker_client, sfc_clf_name)
282 return tacker_client.delete_sfc_classifier(sfc_clf)
284 logger.error("Error [delete_sfc_classifier(tacker_client, '%s', "
285 "'%s')]: %s" % (sfc_clf_id, sfc_clf_name, e))