3e0c9cf455faa3408bdca0e8be449eae50676f78
[functest.git] / 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 yaml
19
20 logger = ft_logger.Logger("tacker_utils").getLogger()
21
22
23 def get_tacker_client():
24     creds_tacker = os_utils.get_credentials('tacker')
25     return tackerclient.Client(**creds_tacker)
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         return resp[endpoint[1:]][0]['id']
37     except Exception, e:
38         logger.error("Error [get_id_from_name(tacker_client, "
39                      "resource_type, resource_name)]: %s" % e)
40         return None
41
42
43 def get_vnfd_id(tacker_client, vnfd_name):
44     return get_id_from_name(tacker_client, 'vnfd', vnfd_name)
45
46
47 def get_vnf_id(tacker_client, vnf_name):
48     return get_id_from_name(tacker_client, 'vnf', vnf_name)
49
50
51 def get_sfc_id(tacker_client, sfc_name):
52     return get_id_from_name(tacker_client, 'sfc', sfc_name)
53
54
55 def get_sfc_classifier_id(tacker_client, sfc_clf_name):
56     return get_id_from_name(tacker_client, 'sfc_classifier', sfc_clf_name)
57
58
59 def list_vnfds(tacker_client, verbose=False):
60     try:
61         vnfds = tacker_client.list_vnfds(retrieve_all=True)
62         if not verbose:
63             vnfds = [vnfd['id'] for vnfd in vnfds['vnfds']]
64         return vnfds
65     except Exception, e:
66         logger.error("Error [list_vnfds(tacker_client)]: %s" % e)
67         return None
68
69
70 def create_vnfd(tacker_client, tosca_file=None):
71     try:
72         vnfd_body = {}
73         if tosca_file is not None:
74             with open(tosca_file) as tosca_fd:
75                 vnfd_body = yaml.safe_load(tosca_fd)
76         return tacker_client.create_vnfd(body=vnfd_body)
77     except Exception, e:
78         logger.error("Error [create_vnfd(tacker_client, '%s')]: %s"
79                      % (tosca_file, e))
80         return None
81
82
83 def delete_vnfd(tacker_client, vnfd_id=None, vnfd_name=None):
84     try:
85         vnfd = vnfd_id
86         if vnfd is None:
87             if vnfd_name is None:
88                 raise Exception('You need to provide VNFD id or VNFD name')
89             vnfd = get_vnfd_id(tacker_client, vnfd_name)
90         return tacker_client.delete_vnfd(vnfd)
91     except Exception, e:
92         logger.error("Error [delete_vnfd(tacker_client, '%s', '%s')]: %s"
93                      % (vnfd_id, vnfd_name, e))
94         return None
95
96
97 def list_vnfs(tacker_client, verbose=False):
98     try:
99         vnfs = tacker_client.list_vnfs(retrieve_all=True)
100         if not verbose:
101             vnfs = [vnf['id'] for vnf in vnfs['vnfs']]
102         return vnfs
103     except Exception, e:
104         logger.error("Error [list_vnfs(tacker_client)]: %s" % e)
105         return None
106
107
108 def create_vnf(tacker_client, vnf_name, vnfd_id=None, vnfd_name=None):
109     try:
110         vnf_body = {
111             'vnf': {
112                 'attributes': {},
113                 'name': vnf_name
114             }
115         }
116         if vnfd_id is not None:
117             vnf_body['vnf']['vnfd_id'] = vnfd_id
118         else:
119             if vnfd_name is None:
120                 raise Exception('vnfd id or vnfd name is required')
121             vnf_body['vnf']['vnfd_id'] = get_vnfd_id(tacker_client, vnfd_name)
122         return tacker_client.create_vnf(body=vnf_body)
123     except Exception, e:
124         logger.error("error [create_vnf(tacker_client, '%s', '%s', '%s')]: %s"
125                      % (vnf_name, vnfd_id, vnfd_name, e))
126         return None
127
128
129 def delete_vnf(tacker_client, vnf_id=None, vnf_name=None):
130     try:
131         vnf = vnf_id
132         if vnf is None:
133             if vnf_name is None:
134                 raise Exception('You need to provide a VNF id or name')
135             vnf = get_vnf_id(tacker_client, vnf_name)
136         return tacker_client.delete_vnf(vnf)
137     except Exception, e:
138         logger.error("Error [delete_vnf(tacker_client, '%s', '%s')]: %s"
139                      % (vnf_id, vnf_name, e))
140         return None
141
142
143 def list_sfcs(tacker_client, verbose=False):
144     try:
145         sfcs = tacker_client.list_sfcs(retrieve_all=True)
146         if not verbose:
147             sfcs = [sfc['id'] for sfc in sfcs['sfcs']]
148         return sfcs
149     except Exception, e:
150         logger.error("Error [list_sfcs(tacker_client)]: %s" % e)
151         return None
152
153
154 def create_sfc(tacker_client, sfc_name,
155                chain_vnf_ids=None,
156                chain_vnf_names=None):
157     try:
158         sfc_body = {
159             'sfc': {
160                 'attributes': {},
161                 'name': sfc_name,
162                 'chain': []
163             }
164         }
165         if chain_vnf_ids is not None:
166             sfc_body['sfc']['chain'] = chain_vnf_ids
167         else:
168             if chain_vnf_names is None:
169                 raise Exception('You need to provide a chain of VNFs')
170             sfc_body['sfc']['chain'] = [get_vnf_id(tacker_client, name)
171                                         for name in chain_vnf_names]
172         return tacker_client.create_sfc(body=sfc_body)
173     except Exception, e:
174         logger.error("error [create_sfc(tacker_client, '%s', '%s', '%s')]: %s"
175                      % (sfc_name, chain_vnf_ids, chain_vnf_names, e))
176         return None
177
178
179 def delete_sfc(tacker_client, sfc_id=None, sfc_name=None):
180     try:
181         sfc = sfc_id
182         if sfc is None:
183             if sfc_name is None:
184                 raise Exception('You need to provide an SFC id or name')
185             sfc = get_sfc_id(tacker_client, sfc_name)
186         return tacker_client.delete_sfc(sfc)
187     except Exception, e:
188         logger.error("Error [delete_sfc(tacker_client, '%s', '%s')]: %s"
189                      % (sfc_id, sfc_name, e))
190         return None
191
192
193 def list_sfc_clasifiers(tacker_client, verbose=False):
194     try:
195         sfc_clfs = tacker_client.list_sfc_classifiers(retrieve_all=True)
196         if not verbose:
197             sfc_clfs = [sfc_clf['id']
198                         for sfc_clf in sfc_clfs['sfc_classifiers']]
199         return sfc_clfs
200     except Exception, e:
201         logger.error("Error [list_sfc_classifiers(tacker_client)]: %s" % e)
202         return None
203
204
205 def create_sfc_classifier(tacker_client, sfc_clf_name, sfc_id=None,
206                           sfc_name=None, match={}):
207     # Example match:
208     # match: {
209     #     "source_port": "0",
210     #     "protocol": "6",
211     #     "dest_port": "80"
212     # }
213     try:
214         sfc_clf_body = {
215             'sfc_classifier': {
216                 'attributes': {},
217                 'name': sfc_clf_name,
218                 'match': match,
219                 'chain': ''
220             }
221         }
222         if sfc_id is not None:
223             sfc_clf_body['sfc_classifier']['chain'] = sfc_id
224         else:
225             if sfc_name is None:
226                 raise Exception('You need to provide an SFC id or name')
227             sfc_clf_body['sfc']['chain'] = get_sfc_id(tacker_client, sfc_name)
228         return tacker_client.create_sfc_classifier(body=sfc_clf_body)
229     except Exception, e:
230         logger.error("error [create_sfc_classifier(tacker_client, '%s', '%s', "
231                      "'%s')]: %s" % (sfc_clf_name, sfc_id, sfc_name, match, e))
232         return None
233
234
235 def delete_sfc_classifier(tacker_client,
236                           sfc_clf_id=None,
237                           sfc_clf_name=None):
238     try:
239         sfc_clf = sfc_clf_id
240         if sfc_clf is None:
241             if sfc_clf_name is None:
242                 raise Exception('You need to provide an SFC'
243                                 'classifier id or name')
244             sfc_clf = get_sfc_classifier_id(tacker_client, sfc_clf_name)
245         return tacker_client.delete_sfc_classifier(sfc_clf)
246     except Exception, e:
247         logger.error("Error [delete_sfc_classifier(tacker_client, '%s', "
248                      "'%s')]: %s" % (sfc_clf_id, sfc_clf_name, e))
249         return None