[odl-sfc] Add symmetric option for sfc_create
[functest.git] / functest / utils / openstack_tacker.py
1 ###########################################################################
2 # Copyright (c) 2016 Ericsson AB and others.
3 # Author: George Paraskevopoulos <geopar@intracom-telecom.com>
4 #
5 # Wrappers for trozet's python-tackerclient v1.0
6 # (https://github.com/trozet/python-tackerclient)
7 #
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 ##########################################################################
13
14
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
18 import time
19
20 logger = ft_logger.Logger("tacker_utils").getLogger()
21
22
23 def get_tacker_client(other_creds={}):
24     sess = os_utils.get_session(other_creds)
25     return tackerclient.Client(session=sess)
26
27
28 # *********************************************
29 #   TACKER
30 # *********************************************
31 def get_id_from_name(tacker_client, resource_type, resource_name):
32     try:
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']
38     except Exception, e:
39         logger.error("Error [get_id_from_name(tacker_client, "
40                      "resource_type, resource_name)]: %s" % e)
41         return None
42
43
44 def get_vnfd_id(tacker_client, vnfd_name):
45     return get_id_from_name(tacker_client, 'vnfd', vnfd_name)
46
47
48 def get_vnf_id(tacker_client, vnf_name):
49     return get_id_from_name(tacker_client, 'vnf', vnf_name)
50
51
52 def get_sfc_id(tacker_client, sfc_name):
53     return get_id_from_name(tacker_client, 'sfc', sfc_name)
54
55
56 def get_sfc_classifier_id(tacker_client, sfc_clf_name):
57     return get_id_from_name(tacker_client, 'sfc-classifier', sfc_clf_name)
58
59
60 def list_vnfds(tacker_client, verbose=False):
61     try:
62         vnfds = tacker_client.list_vnfds(retrieve_all=True)
63         if not verbose:
64             vnfds = [vnfd['id'] for vnfd in vnfds['vnfds']]
65         return vnfds
66     except Exception, e:
67         logger.error("Error [list_vnfds(tacker_client)]: %s" % e)
68         return None
69
70
71 def create_vnfd(tacker_client, tosca_file=None):
72     try:
73         vnfd_body = {}
74         if tosca_file is not None:
75             with open(tosca_file) as tosca_fd:
76                 vnfd_body = tosca_fd.read()
77             logger.info('VNFD template:\n{0}'.format(vnfd_body))
78         return tacker_client.create_vnfd(
79             body={"vnfd": {"attributes": {"vnfd": vnfd_body}}})
80     except Exception, e:
81         logger.error("Error [create_vnfd(tacker_client, '%s')]: %s"
82                      % (tosca_file, e))
83         return None
84
85
86 def delete_vnfd(tacker_client, vnfd_id=None, vnfd_name=None):
87     try:
88         vnfd = vnfd_id
89         if vnfd is None:
90             if vnfd_name is 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)
94     except Exception, e:
95         logger.error("Error [delete_vnfd(tacker_client, '%s', '%s')]: %s"
96                      % (vnfd_id, vnfd_name, e))
97         return None
98
99
100 def list_vnfs(tacker_client, verbose=False):
101     try:
102         vnfs = tacker_client.list_vnfs(retrieve_all=True)
103         if not verbose:
104             vnfs = [vnf['id'] for vnf in vnfs['vnfs']]
105         return vnfs
106     except Exception, e:
107         logger.error("Error [list_vnfs(tacker_client)]: %s" % e)
108         return None
109
110
111 def create_vnf(tacker_client, vnf_name, vnfd_id=None,
112                vnfd_name=None, param_file=None):
113     try:
114         vnf_body = {
115             'vnf': {
116                 'attributes': {},
117                 'name': vnf_name
118             }
119         }
120         if param_file is not None:
121             with open(param_file) as f:
122                 params = f.read()
123             vnf_body['vnf']['attributes']['param_values'] = params
124         if vnfd_id is not None:
125             vnf_body['vnf']['vnfd_id'] = vnfd_id
126         else:
127             if vnfd_name is None:
128                 raise Exception('vnfd id or vnfd name is required')
129             vnf_body['vnf']['vnfd_id'] = get_vnfd_id(tacker_client, vnfd_name)
130         return tacker_client.create_vnf(body=vnf_body)
131     except Exception, e:
132         logger.error("error [create_vnf(tacker_client,"
133                      " '%s', '%s', '%s')]: %s"
134                      % (vnf_name, vnfd_id, vnfd_name, e))
135         return None
136
137
138 def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None):
139     try:
140         _id = None
141         if vnf_id is not None:
142             _id = vnf_id
143         elif vnf_name is not None:
144             while _id is None:
145                 try:
146                     _id = get_vnf_id(tacker_client, vnf_name)
147                 except:
148                     logger.error("Bazinga")
149         else:
150             raise Exception('You must specify vnf_id or vnf_name')
151         while True:
152             vnf = [v for v in list_vnfs(tacker_client, verbose=True)['vnfs']
153                    if v['id'] == _id]
154             vnf = vnf[0]
155             logger.info('Waiting for vnf {0}'.format(str(vnf)))
156             if vnf['status'] == 'ERROR':
157                 raise Exception('Error when booting vnf %s' % _id)
158             elif vnf['status'] == 'PENDING_CREATE':
159                 time.sleep(3)
160                 continue
161             else:
162                 break
163         return _id
164     except Exception, e:
165         logger.error("error [wait_for_vnf(tacker_client, '%s', '%s')]: %s"
166                      % (vnf_id, vnf_name, e))
167         return None
168
169
170 def delete_vnf(tacker_client, vnf_id=None, vnf_name=None):
171     try:
172         vnf = vnf_id
173         if vnf is None:
174             if vnf_name is None:
175                 raise Exception('You need to provide a VNF id or name')
176             vnf = get_vnf_id(tacker_client, vnf_name)
177         return tacker_client.delete_vnf(vnf)
178     except Exception, e:
179         logger.error("Error [delete_vnf(tacker_client, '%s', '%s')]: %s"
180                      % (vnf_id, vnf_name, e))
181         return None
182
183
184 def list_sfcs(tacker_client, verbose=False):
185     try:
186         sfcs = tacker_client.list_sfcs(retrieve_all=True)
187         if not verbose:
188             sfcs = [sfc['id'] for sfc in sfcs['sfcs']]
189         return sfcs
190     except Exception, e:
191         logger.error("Error [list_sfcs(tacker_client)]: %s" % e)
192         return None
193
194
195 def create_sfc(tacker_client, sfc_name,
196                chain_vnf_ids=None,
197                chain_vnf_names=None,
198                symmetrical=False):
199     try:
200         sfc_body = {
201             'sfc': {
202                 'attributes': {},
203                 'name': sfc_name,
204                 'chain': []
205             }
206         }
207         if symmetrical:
208             sfc_body['sfc']['symmetrical'] = True
209         if chain_vnf_ids is not None:
210             sfc_body['sfc']['chain'] = chain_vnf_ids
211         else:
212             if chain_vnf_names is None:
213                 raise Exception('You need to provide a chain of VNFs')
214             sfc_body['sfc']['chain'] = [get_vnf_id(tacker_client, name)
215                                         for name in chain_vnf_names]
216         return tacker_client.create_sfc(body=sfc_body)
217     except Exception, e:
218         logger.error("error [create_sfc(tacker_client,"
219                      " '%s', '%s', '%s')]: %s"
220                      % (sfc_name, chain_vnf_ids, chain_vnf_names, e))
221         return None
222
223
224 def delete_sfc(tacker_client, sfc_id=None, sfc_name=None):
225     try:
226         sfc = sfc_id
227         if sfc is None:
228             if sfc_name is None:
229                 raise Exception('You need to provide an SFC id or name')
230             sfc = get_sfc_id(tacker_client, sfc_name)
231         return tacker_client.delete_sfc(sfc)
232     except Exception, e:
233         logger.error("Error [delete_sfc(tacker_client, '%s', '%s')]: %s"
234                      % (sfc_id, sfc_name, e))
235         return None
236
237
238 def list_sfc_classifiers(tacker_client, verbose=False):
239     try:
240         sfc_clfs = tacker_client.list_sfc_classifiers(retrieve_all=True)
241         if not verbose:
242             sfc_clfs = [sfc_clf['id']
243                         for sfc_clf in sfc_clfs['sfc_classifiers']]
244         return sfc_clfs
245     except Exception, e:
246         logger.error("Error [list_sfc_classifiers(tacker_client)]: %s" % e)
247         return None
248
249
250 def create_sfc_classifier(tacker_client, sfc_clf_name, sfc_id=None,
251                           sfc_name=None, match={}):
252     # Example match:
253     # match: {
254     #     "source_port": "0",
255     #     "protocol": "6",
256     #     "dest_port": "80"
257     # }
258     try:
259         sfc_clf_body = {
260             'sfc_classifier': {
261                 'attributes': {},
262                 'name': sfc_clf_name,
263                 'match': match,
264                 'chain': ''
265             }
266         }
267         if sfc_id is not None:
268             sfc_clf_body['sfc_classifier']['chain'] = sfc_id
269         else:
270             if sfc_name is None:
271                 raise Exception('You need to provide an SFC id or name')
272             sfc_clf_body['sfc_classifier']['chain'] = get_sfc_id(
273                 tacker_client, sfc_name)
274         return tacker_client.create_sfc_classifier(body=sfc_clf_body)
275     except Exception, e:
276         logger.error("error [create_sfc_classifier(tacker_client,"
277                      " '%s', '%s','%s', '%s')]: '%s'"
278                      % (sfc_clf_name, sfc_id, sfc_name, str(match), e))
279         return None
280
281
282 def delete_sfc_classifier(tacker_client,
283                           sfc_clf_id=None,
284                           sfc_clf_name=None):
285     try:
286         sfc_clf = sfc_clf_id
287         if sfc_clf is None:
288             if sfc_clf_name is None:
289                 raise Exception('You need to provide an SFC'
290                                 'classifier id or name')
291             sfc_clf = get_sfc_classifier_id(tacker_client, sfc_clf_name)
292         return tacker_client.delete_sfc_classifier(sfc_clf)
293     except Exception, e:
294         logger.error("Error [delete_sfc_classifier(tacker_client, '%s', "
295                      "'%s')]: %s" % (sfc_clf_id, sfc_clf_name, e))
296         return None