blob: cc82068a632e5bc9d96615b4476f91bdf6a7bc58 [file] [log] [blame]
Kang Xi11d278c2018-04-06 16:56:04 -04001#! /usr/bin/python
2
3import sys
4import logging
5import requests
6import json
7from datetime import datetime
8import progressbar
9import time
10import csar_parser
11import preload
12from vcpecommon import *
13
14
15class SoUtils:
16 def __init__(self, vcpecommon, api_version):
17 """
18 :param vcpecommon:
19 :param api_version: must be 'v4' or 'v5'
20 """
21 self.logger = logging.getLogger(__name__)
22 self.vcpecommon = vcpecommon
23 if api_version not in self.vcpecommon.so_req_api_url:
24 self.logger.error('Incorrect SO API version: %s', api_version)
25 sys.exit()
26 self.service_req_api_url = self.vcpecommon.so_req_api_url[api_version]
27
28 def submit_create_req(self, req_json, req_type, service_instance_id=None, vnf_instance_id=None):
29 """
30 POST {serverRoot}/serviceInstances/v4
31 POST {serverRoot}/serviceInstances/v4/{serviceInstanceId}/vnfs
32 POST {serverRoot}/serviceInstances/v4/{serviceInstanceId}/networks
33 POST {serverRoot}/serviceInstances/v4/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules
34 :param req_json:
35 :param service_instance_id: this is required only for networks, vnfs, and vf modules
36 :param req_type:
37 :param vnf_instance_id:
38 :return: req_id, instance_id
39 """
40 if req_type == 'service':
41 url = self.service_req_api_url
42 elif req_type == 'vnf':
43 url = '/'.join([self.service_req_api_url, service_instance_id, 'vnfs'])
44 elif req_type == 'network':
45 url = '/'.join([self.service_req_api_url, service_instance_id, 'networks'])
46 elif req_type == 'vfmodule':
47 url = '/'.join([self.service_req_api_url, service_instance_id, 'vnfs', vnf_instance_id, 'vfModules'])
48 else:
49 self.logger.error('Invalid request type: {0}. Can only be service/vnf/network/vfmodule'.format(req_type))
50 return None, None
51
52 r = requests.post(url, headers=self.vcpecommon.so_headers, auth=self.vcpecommon.so_userpass, json=req_json)
53 response = r.json()
54
55 self.logger.debug('---------------------------------------------------------------')
56 self.logger.debug('------- Creation request submitted to SO, got response --------')
57 self.logger.debug(json.dumps(response, indent=4, sort_keys=True))
58 self.logger.debug('---------------------------------------------------------------')
59 req_id = response.get('requestReferences', {}).get('requestId', '')
60 instance_id = response.get('requestReferences', {}).get('instanceId', '')
61 return req_id, instance_id
62
63 def check_progress(self, req_id, eta=0, interval=5):
64 if not req_id:
65 self.logger.error('Error when checking SO request progress, invalid request ID: ' + req_id)
66 return False
67 duration = 0.0
68 bar = progressbar.ProgressBar(redirect_stdout=True)
69 url = self.vcpecommon.so_check_progress_api_url + '/' + req_id
70
71 while True:
72 time.sleep(interval)
73 r = requests.get(url, headers=self.vcpecommon.so_headers, auth=self.vcpecommon.so_userpass)
74 response = r.json()
75
76 duration += interval
77 if eta > 0:
78 percentage = min(95, 100 * duration / eta)
79 else:
80 percentage = int(response['request']['requestStatus']['percentProgress'])
81
82 if response['request']['requestStatus']['requestState'] == 'IN_PROGRESS':
83 self.logger.debug('------------------Request Status-------------------------------')
84 self.logger.debug(json.dumps(response, indent=4, sort_keys=True))
85 bar.update(percentage)
86 else:
87 self.logger.debug('---------------------------------------------------------------')
88 self.logger.debug('----------------- Creation Request Results --------------------')
89 self.logger.debug(json.dumps(response, indent=4, sort_keys=True))
90 self.logger.debug('---------------------------------------------------------------')
91 flag = response['request']['requestStatus']['requestState'] == 'COMPLETE'
92 if not flag:
93 self.logger.error('Request failed.')
94 self.logger.error(json.dumps(response, indent=4, sort_keys=True))
95 bar.update(100)
96 bar.finish()
97 return flag
98
99 def add_req_info(self, req_details, instance_name, product_family_id=None):
100 req_details['requestInfo'] = {
101 'instanceName': instance_name,
102 'source': 'VID',
103 'suppressRollback': 'true',
104 'requestorId': 'vCPE-Robot'
105 }
106 if product_family_id:
107 req_details['requestInfo']['productFamilyId'] = product_family_id
108
109 def add_related_instance(self, req_details, instance_id, instance_model):
110 instance = {"instanceId": instance_id, "modelInfo": instance_model}
111 if 'relatedInstanceList' not in req_details:
112 req_details['relatedInstanceList'] = [{"relatedInstance": instance}]
113 else:
114 req_details['relatedInstanceList'].append({"relatedInstance": instance})
115
116 def generate_vnf_or_network_request(self, req_type, instance_name, vnf_or_network_model, service_instance_id,
117 service_model):
118 req_details = {
119 'modelInfo': vnf_or_network_model,
120 'cloudConfiguration': {"lcpCloudRegionId": self.vcpecommon.os_region_name,
121 "tenantId": self.vcpecommon.os_tenant_id},
122 'requestParameters': {"userParams": []}
123 }
124 self.add_req_info(req_details, instance_name, self.vcpecommon.product_family_id)
125 self.add_related_instance(req_details, service_instance_id, service_model)
126 return {'requestDetails': req_details}
127
128 def generate_vfmodule_request(self, instance_name, vfmodule_model, service_instance_id,
129 service_model, vnf_instance_id, vnf_model):
130 req_details = {
131 'modelInfo': vfmodule_model,
132 'cloudConfiguration': {"lcpCloudRegionId": self.vcpecommon.os_region_name,
133 "tenantId": self.vcpecommon.os_tenant_id},
134 'requestParameters': {"usePreload": 'true'}
135 }
136 self.add_req_info(req_details, instance_name, self.vcpecommon.product_family_id)
137 self.add_related_instance(req_details, service_instance_id, service_model)
138 self.add_related_instance(req_details, vnf_instance_id, vnf_model)
139 return {'requestDetails': req_details}
140
141 def generate_service_request(self, instance_name, model):
142 req_details = {
143 'modelInfo': model,
144 'subscriberInfo': {'globalSubscriberId': self.vcpecommon.global_subscriber_id},
145 'requestParameters': {
146 "userParams": [],
147 "subscriptionServiceType": "vCPE",
148 "aLaCarte": 'true'
149 }
150 }
151 self.add_req_info(req_details, instance_name)
152 return {'requestDetails': req_details}
153
154 def generate_custom_service_request(self, instance_name, model, brg_mac):
155 req_details = {
156 'modelInfo': model,
157 'subscriberInfo': {'subscriberName': 'Kaneohe',
158 'globalSubscriberId': self.vcpecommon.global_subscriber_id},
159 'cloudConfiguration': {"lcpCloudRegionId": self.vcpecommon.os_region_name,
160 "tenantId": self.vcpecommon.os_tenant_id},
161 'requestParameters': {
162 "userParams": [
163 {
164 'name': 'BRG_WAN_MAC_Address',
165 'value': brg_mac
166 }
167 ],
168 "subscriptionServiceType": "vCPE",
169 'aLaCarte': 'false'
170 }
171 }
172 self.add_req_info(req_details, instance_name, self.vcpecommon.custom_product_family_id)
173 return {'requestDetails': req_details}
174
175 def create_custom_service(self, csar_file, brg_mac, name_suffix=None):
176 parser = csar_parser.CsarParser()
177 if not parser.parse_csar(csar_file):
178 return False
179
180 # yyyymmdd_hhmm
181 if not name_suffix:
182 name_suffix = '_' + datetime.now().strftime('%Y%m%d%H%M')
183
184 # create service
185 instance_name = '_'.join([self.vcpecommon.instance_name_prefix['service'],
186 parser.svc_model['modelName'], name_suffix])
187 instance_name = instance_name.lower()
188 req = self.generate_custom_service_request(instance_name, parser.svc_model, brg_mac)
189 self.logger.debug(json.dumps(req, indent=2, sort_keys=True))
190 self.logger.info('Creating custom service {0}.'.format(instance_name))
191 req_id, svc_instance_id = self.submit_create_req(req, 'service')
192 if not self.check_progress(req_id, 140):
193 return False
194 return True
195
196 def wait_for_aai(self, node_type, uuid):
197 self.logger.info('Waiting for AAI traversal to complete...')
198 bar = progressbar.ProgressBar()
199 for i in range(30):
200 time.sleep(1)
201 bar.update(i*100.0/30)
202 if self.vcpecommon.is_node_in_aai(node_type, uuid):
203 bar.update(100)
204 bar.finish()
205 return
206
207 self.logger.error("AAI traversal didn't finish in 30 seconds. Something is wrong. Type {0}, UUID {1}".format(
208 node_type, uuid))
209 sys.exit()
210
211 def create_entire_service(self, csar_file, vnf_template_file, preload_dict, name_suffix, heatbridge=False):
212 """
213 :param csar_file:
214 :param vnf_template_file:
215 :param preload_dict:
216 :param name_suffix:
217 :return: service instance UUID
218 """
219 self.logger.info('\n----------------------------------------------------------------------------------')
220 self.logger.info('Start to create entire service defined in csar: {0}'.format(csar_file))
221 parser = csar_parser.CsarParser()
222 self.logger.info('Parsing csar ...')
223 if not parser.parse_csar(csar_file):
224 self.logger.error('Cannot parse csar: {0}'.format(csar_file))
225 return None
226
227 # create service
228 instance_name = '_'.join([self.vcpecommon.instance_name_prefix['service'],
229 parser.svc_model['modelName'], name_suffix])
230 instance_name = instance_name.lower()
231 self.logger.info('Creating service instance: {0}.'.format(instance_name))
232 req = self.generate_service_request(instance_name, parser.svc_model)
233 self.logger.debug(json.dumps(req, indent=2, sort_keys=True))
234 req_id, svc_instance_id = self.submit_create_req(req, 'service')
235 if not self.check_progress(req_id, eta=2, interval=1):
236 return None
237
238 # wait for AAI to complete traversal
239 self.wait_for_aai('service', svc_instance_id)
240
241 # create networks
242 for model in parser.net_models:
243 base_name = model['modelCustomizationName'].lower().replace('mux_vg', 'mux_gw')
244 network_name = '_'.join([self.vcpecommon.instance_name_prefix['network'], base_name, name_suffix])
245 network_name = network_name.lower()
246 self.logger.info('Creating network: ' + network_name)
247 req = self.generate_vnf_or_network_request('network', network_name, model, svc_instance_id,
248 parser.svc_model)
249 self.logger.debug(json.dumps(req, indent=2, sort_keys=True))
250 req_id, net_instance_id = self.submit_create_req(req, 'network', svc_instance_id)
251 if not self.check_progress(req_id, eta=20):
252 return None
253
254 self.logger.info('Changing subnet name to ' + self.vcpecommon.network_name_to_subnet_name(network_name))
255 self.vcpecommon.set_network_name(network_name)
256 subnet_name_changed = False
257 for i in range(20):
258 time.sleep(3)
259 if self.vcpecommon.set_subnet_name(network_name):
260 subnet_name_changed = True
261 break
262
263 if not subnet_name_changed:
264 self.logger.error('Failed to change subnet name for ' + network_name)
265 return None
266
267
268 vnf_model = None
269 vnf_instance_id = None
270 # create VNF
271 if len(parser.vnf_models) == 1:
272 vnf_model = parser.vnf_models[0]
273 vnf_instance_name = '_'.join([self.vcpecommon.instance_name_prefix['vnf'],
274 vnf_model['modelCustomizationName'].split(' ')[0], name_suffix])
275 vnf_instance_name = vnf_instance_name.lower()
276 self.logger.info('Creating VNF: ' + vnf_instance_name)
277 req = self.generate_vnf_or_network_request('vnf', vnf_instance_name, vnf_model, svc_instance_id,
278 parser.svc_model)
279 self.logger.debug(json.dumps(req, indent=2, sort_keys=True))
280 req_id, vnf_instance_id = self.submit_create_req(req, 'vnf', svc_instance_id)
281 if not self.check_progress(req_id, eta=2, interval=1):
282 self.logger.error('Failed to create VNF {0}.'.format(vnf_instance_name))
283 return False
284
285 # wait for AAI to complete traversal
286 if not vnf_instance_id:
287 self.logger.error('No VNF instance ID returned!')
288 sys.exit()
289 self.wait_for_aai('vnf', vnf_instance_id)
290
291 preloader = preload.Preload(self.vcpecommon)
292 preloader.preload_vfmodule(vnf_template_file, svc_instance_id, parser.vnf_models[0], parser.vfmodule_models[0],
293 preload_dict, name_suffix)
294 # create VF Module
295 if len(parser.vfmodule_models) == 1:
296 if not vnf_instance_id or not vnf_model:
297 self.logger.error('Invalid VNF instance ID or VNF model!')
298 sys.exit()
299
300 model = parser.vfmodule_models[0]
301 vfmodule_instance_name = '_'.join([self.vcpecommon.instance_name_prefix['vfmodule'],
302 model['modelCustomizationName'].split('..')[0], name_suffix])
303 vfmodule_instance_name = vfmodule_instance_name.lower()
304 self.logger.info('Creating VF Module: ' + vfmodule_instance_name)
305 req = self.generate_vfmodule_request(vfmodule_instance_name, model, svc_instance_id, parser.svc_model,
306 vnf_instance_id, vnf_model)
307 self.logger.debug(json.dumps(req, indent=2, sort_keys=True))
308 req_id, vfmodule_instance_id = self.submit_create_req(req, 'vfmodule', svc_instance_id, vnf_instance_id)
309 if not self.check_progress(req_id, eta=70, interval=5):
310 self.logger.error('Failed to create VF Module {0}.'.format(vfmodule_instance_name))
311 return None
312
313 # run heatbridge
314 if heatbridge:
315 self.vcpecommon.headbridge(vfmodule_instance_name, svc_instance_id)
316 self.vcpecommon.save_vgmux_vnf_name(vnf_instance_name)
317
318 return svc_instance_id