blob: 4b024c6e2becfff2dc860cc2b7aa50b75ea1c75d [file] [log] [blame]
zhaoliping123906480b2020-04-22 11:52:56 +08001#!/usr/bin/python
2
3# Prerequisites for machine to run this
4# Put in required parameters in vcpe_config.json
5# Install python-pip (apt install python-pip)
6# Install requests
7# Install ONAP CLI
8# Must have connectivity to the ONAP, openstack is already configured.
9# Configuration File, the parameters will be modified according to the Lab env
10# Put in vnf and ns CSAR file under csar folder
11
12import json
13import os
14import uuid
15import requests
16import unittest
17import time
18import urllib3
19urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
20
21
22class VcpeToscaTest(unittest.TestCase):
23 def setUp(self):
24 file_path = os.path.dirname(os.path.abspath(__file__))
25 with open(file_path + "/config/vcpe_config.json", encoding='utf-8') as self.config_file:
26 self.config_params = self.get_parameters()
27 self.aai_header = {
28 "Accept": "application/json",
29 "Content-Type": "application/json",
30 'X-TransactionId': "9999",
31 'Real-Time': "true",
32 'X-FromAppId': "jimmy-postman",
33 "Authorization": "Basic QUFJOkFBSQ=="
34 }
35 self.base_url = self.config_params["msb_url"]
36 print("Set cli command environment--beginning")
37 os.environ["OPEN_CLI_PRODUCT_IN_USE"] = self.config_params["open_cli_product"]
38 os.environ["OPEN_CLI_HOME"] = self.config_params["open_cli_home"]
39 print("Set cli command environment--successful")
40
zhaoliping1230d9647b2020-08-14 16:43:04 +080041 self.complex_version = None
42 self.cloud_version = None
43 self.service_type_version = None
44 self.customer_version = None
45 self.tenant_id = None
46 self.subscription_version = None
47 self.esr_vnfm_version = self.esr_vnfm_id = None
48 self.ns_instance_id = None
49 self.ns_package_id = None
50 self.vnf_package_list = []
51
zhaoliping123906480b2020-04-22 11:52:56 +080052 print("Create cloud complex--beginning")
53 self.create_complex()
54 print("Create cloud complex--successful")
55
56 print("Register all clouds--beginning")
57 self.register_all_clouds()
58 print("Register all clouds--successful")
59 time.sleep(30)
60
zhaoliping1230d9647b2020-08-14 16:43:04 +080061 print("Create vCPE service")
zhaoliping123906480b2020-04-22 11:52:56 +080062 self.create_service_type()
63
zhaoliping1230d9647b2020-08-14 16:43:04 +080064 print("Create customer")
zhaoliping123906480b2020-04-22 11:52:56 +080065 self.create_customer()
66
67 print("Get tenant id")
68 self.get_tenant_id()
69
zhaoliping1230d9647b2020-08-14 16:43:04 +080070 print("Add customer and subscription")
zhaoliping123906480b2020-04-22 11:52:56 +080071 self.add_customer_subscription()
72
73 print("Register vnfm")
74 self.register_vnfm()
75
76 def tearDown(self):
77 if self.ns_instance_id:
78 self.terminateNs()
79 self.deleteNs()
80
81 if self.ns_package_id:
82 self.delete_ns_package()
83
84 if self.vnf_package_list:
85 self.delete_vnf_package()
86
87 if self.esr_vnfm_id and self.esr_vnfm_version:
88 self.unregister_vnfm()
89
90 if self.subscription_version:
91 print("Remove service subscription")
92 self.remove_customer_subscription()
93
94 if self.customer_version:
95 print("Remove customer %s" % self.config_params["customer_name"])
96 self.delete_customer()
97
98 if self.service_type_version:
99 print("Remove service type %s" % self.config_params["service_name"])
100 self.delete_service_type()
101
102 if self.cloud_version:
103 print("Remove cloud %s" % self.config_params["cloud-owner"])
104 self.delete_cloud_helper()
105
zhaoliping123906480b2020-04-22 11:52:56 +0800106 if self.complex_version:
107 self.get_complex_resource_version()
108 print("Remove complex %s" % self.config_params["complex_name"])
109 self.delete_complex()
110
111 def get_parameters(self):
112 parameters = json.load(self.config_file)
113 return parameters
114
115 @staticmethod
116 def get_out_helper_2(in_string):
117 out_list = ((in_string.replace('|', '')).replace('+', '')).split()
118 return out_list
119
120 def create_complex(self):
zhaoliping123906480b2020-04-22 11:52:56 +0800121 complex_create_string = "oclip complex-create -j {} -r {} -x {} -y {} -lt {} -l {} -i {} -lo {} \
122 -S {} -la {} -g {} -w {} -z {} -k {} -o {} -q {} -m {} -u {} -p {}".format(
123 self.config_params["street2"], self.config_params["physical_location"],
124 self.config_params["complex_name"], self.config_params["data_center_code"],
125 self.config_params["latitude"], self.config_params["region"],
126 self.config_params["street1"], self.config_params["longitude"],
127 self.config_params["state"], self.config_params["lata"],
128 self.config_params["city"], self.config_params["postal-code"],
129 self.config_params["complex_name"], self.config_params["country"],
130 self.config_params["elevation"], self.config_params["identity_url"],
131 self.config_params["aai_url"], self.config_params["aai_username"],
132 self.config_params["aai_password"])
133 os.system(complex_create_string)
134
135 self.get_complex_resource_version()
136
137 def get_complex_resource_version(self):
138 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
139 complex_url = self.base_url + "/aai/v11/cloud-infrastructure/complexes"
140 complex_list_response = requests.get(url=complex_url, headers=self.aai_header, verify=False)
141 if complex_list_response.status_code == 200:
142 for complex in (complex_list_response.json())["complex"]:
143 if complex['physical-location-id'] == self.config_params["complex_name"]:
144 self.complex_version = complex['resource-version']
145 print("Complex %s resource-version is %s."
146 % (self.config_params["complex_name"], self.complex_version))
147
148 def delete_complex(self):
149 complex_delete_string = 'oclip complex-delete -x {} -y {} -m {} -u {} -p {}'.format(
150 self.config_params["complex_name"], self.complex_version, self.config_params["aai_url"],
151 self.config_params["aai_username"], self.config_params["aai_password"])
152 os.system(complex_delete_string)
153 print("Delete complex--successful")
154 self.complex_version = None
155
156 def register_cloud_helper(self, cloud_region, values):
157 print("Create Cloud--beginning")
zhaoliping123906480b2020-04-22 11:52:56 +0800158 cloud_create_string = 'oclip cloud-create -e {} -b {} ' \
159 '-x {} -y {} -j {} -w {} -l {} -url {} -n {} -q {} -r {} -Q {} -i {} -g {} \
160 -z {} -k {} -c {} -m {} -u {} -p {}' \
161 .format(values.get("esr-system-info-id"), values.get("user-name"),
162 self.config_params["cloud-owner"],
163 cloud_region, values.get("password"),
164 values.get("cloud-region-version"), values.get("default-tenant"),
165 values.get("service-url"), self.config_params["complex_name"],
166 values.get("cloud-type"), self.config_params["owner-defined-type"],
167 values.get("system-type"), values.get("identity-url"),
168 self.config_params["cloud-zone"], values.get("ssl-insecure"),
169 values.get("system-status"), values.get("cloud-domain"),
170 self.config_params["aai_url"],
171 self.config_params["aai_username"],
172 self.config_params["aai_password"])
173
174 os.system(cloud_create_string)
175 print("Create Cloud--successful")
176
177 print("Associate Cloud with complex--beginning")
178 complex_associate_string = "oclip complex-associate -x {} -y {} -z {} -m {} -u {} -p {}".format(
179 self.config_params["complex_name"],
180 cloud_region, self.config_params["cloud-owner"], self.config_params["aai_url"],
181 self.config_params["aai_username"],
182 self.config_params["aai_password"])
183 os.system(complex_associate_string)
184 print("Associate Cloud with complex--successful")
185
186 print("Register Cloud with Multicloud--beginning")
187 multicloud_register_string = "oclip multicloud-register-cloud -y {} -x {} -m {}".format(
188 self.config_params["cloud-owner"], cloud_region, self.config_params["multicloud_url"])
189 os.system(multicloud_register_string)
190 print("Register Cloud with Multicloud--successful")
191
192 cloud_url = self.base_url + "/aai/v11/cloud-infrastructure/cloud-regions"
193 cloud_list_response = requests.get(url=cloud_url, headers=self.aai_header, verify=False)
194 if cloud_list_response.status_code == 200:
195 for cloud in (cloud_list_response.json())["cloud-region"]:
196 if cloud['cloud-owner'] == self.config_params["cloud-owner"]:
197 self.cloud_version = cloud['resource-version']
198 print("Cloud %s resource-version is %s."
199 % (self.config_params["cloud-owner"], self.cloud_version))
200
201 def register_all_clouds(self):
202 cloud_dictionary = self.config_params["cloud_region_data"]
203 for cloud_region, cloud_region_values in cloud_dictionary.items():
204 self.register_cloud_helper(cloud_region, cloud_region_values)
205
206 def delete_cloud_helper(self):
207 print("Multicloud-cloud-delete--beginning")
208 cloud_region = list(self.config_params["cloud_region_data"].keys())[0]
209 header = {'content-type': 'application/json', 'accept': 'application/json'}
210 multicloud_url = self.base_url + "/api/multicloud-titaniumcloud/v1/{}/{}" \
211 .format(self.config_params["cloud-owner"], cloud_region)
212 requests.delete(url=multicloud_url, headers=header, verify=False)
zhaoliping123c981fa02020-05-08 18:33:20 +0800213 cloud_url = self.base_url + "/aai/v11/cloud-infrastructure/cloud-regions"
214 n = 60
215 while n > 0:
216 cloud_flag = False
217 cloud_list_response = requests.get(url=cloud_url, headers=self.aai_header, verify=False)
218 n = n - 1
219 if cloud_list_response.status_code == 200:
220 for cloud in (cloud_list_response.json()).get("cloud-region"):
221 if cloud['cloud-owner'] == self.config_params["cloud-owner"]:
222 cloud_flag = True
223 break
224 if not cloud_flag:
225 break
226 else:
227 time.sleep(1)
zhaoliping123906480b2020-04-22 11:52:56 +0800228 print("Multicloud-cloud-delete----successful")
zhaoliping123c981fa02020-05-08 18:33:20 +0800229 self.cloud_version = None
zhaoliping123906480b2020-04-22 11:52:56 +0800230
231 def create_service_type(self):
zhaoliping123906480b2020-04-22 11:52:56 +0800232 create_string = "oclip service-type-create -x {} -y {} -m {} -u {} -p {}".format(
233 self.config_params["service_name"], self.config_params["service_name"], self.config_params["aai_url"],
234 self.config_params["aai_username"], self.config_params["aai_password"])
235 os.system(create_string)
236
237 service_tpe_list_url = self.base_url + "/aai/v11/service-design-and-creation/services"
238 service_type_list_response = requests.get(url=service_tpe_list_url, headers=self.aai_header, verify=False)
239 if service_type_list_response.status_code == 200:
240 for service in (service_type_list_response.json())["service"]:
241 if service["service-id"] == self.config_params["service_name"]:
242 self.service_type_version = service['resource-version']
243 print("Service type %s resource-version is %s."
244 % (self.config_params["service_name"], self.service_type_version))
245
246 def delete_service_type(self):
247 print("delete service type--beginning")
248 service_delete_string = 'oclip service-type-delete -x {} -y {} -m {} -u {} -p {}'.format(
249 self.config_params["service_name"], self.service_type_version, self.config_params["aai_url"],
250 self.config_params["aai_username"], self.config_params["aai_password"])
251 os.system(service_delete_string)
252 print("delete service type--successful")
253 self.service_type_version = None
254
255 def create_customer(self):
zhaoliping123906480b2020-04-22 11:52:56 +0800256 create_string = "oclip customer-create -x {} -y {} -m {} -u {} -p {}".format(
257 self.config_params["customer_name"],
258 self.config_params["subscriber_name"],
259 self.config_params["aai_url"],
260 self.config_params["aai_username"],
261 self.config_params["aai_password"])
262 os.system(create_string)
263
264 customer_list_url = self.base_url + "/aai/v11/business/customers"
265 customer_list_response = requests.get(url=customer_list_url, headers=self.aai_header, verify=False)
266 if customer_list_response.status_code == 200:
267 for cutsomer in (customer_list_response.json())["customer"]:
268 if cutsomer['global-customer-id'] == self.config_params["customer_name"]:
269 self.customer_version = cutsomer['resource-version']
270 print("Customer %s resource-version is %s."
271 % (self.config_params["customer_name"], self.customer_version))
272
273 def delete_customer(self):
274 print("delete customer--beginning")
275 customer_delete_string = 'oclip customer-delete -x {} -y {} -m {} -u {} -p {}'.format(
276 self.config_params["customer_name"], self.customer_version, self.config_params["aai_url"],
277 self.config_params["aai_username"], self.config_params["aai_password"])
278 os.system(customer_delete_string)
279 print("delete customer--successful")
280 self.customer_version = None
281
282 def get_tenant_id(self):
283 print("Get tenant id--beginning")
zhaoliping123906480b2020-04-22 11:52:56 +0800284 cloud_dictionary = self.config_params["cloud_region_data"]
285 cloud_region = list(self.config_params["cloud_region_data"].keys())[0]
286
287 tenant_list_url = self.base_url + "/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/{}/{}/tenants" \
288 .format(self.config_params["cloud-owner"], cloud_region)
289
290 for cloud_region, cloud_region_values in cloud_dictionary.items():
291 tenant_name = cloud_region_values.get("default-tenant")
292 tenant_list_response = requests.get(url=tenant_list_url, headers=self.aai_header, verify=False)
293 if tenant_list_response.status_code == 200:
294 for tenant in (tenant_list_response.json())["tenant"]:
295 if tenant['tenant-name'] == tenant_name:
296 self.tenant_id = tenant['tenant-id']
297 print("Tenant id is %s ." % self.tenant_id)
298
299 def add_customer_subscription(self):
zhaoliping123906480b2020-04-22 11:52:56 +0800300 subscription_check = 0
301 for cloud_region, cloud_region_values in (self.config_params["cloud_region_data"]).items():
302 if subscription_check == 0:
303 subscription_string = "oclip subscription-create -x {} -c {} -z {} -e {} " \
304 "-y {} -r {} -m {} -u {} -p {}" \
305 .format(self.config_params["customer_name"],
306 self.tenant_id,
307 self.config_params["cloud-owner"],
308 self.config_params["service_name"],
309 cloud_region_values.get("default-tenant"),
310 cloud_region, self.config_params["aai_url"],
311 self.config_params["aai_username"],
312 self.config_params["aai_password"])
313 else:
314 subscription_string = "oclip subscription-cloud-add -x {} -c {} " \
315 "-z {} -e {} -y {} -r {} -m {} -u {} -p {}" \
316 .format(self.config_params["customer_name"], self.tenant_id,
317 self.config_params["cloud-owner"], self.config_params["service_name"],
318 cloud_region_values.get("default-tenant"), cloud_region,
319 self.config_params["aai_url"],
320 self.config_params["aai_username"],
321 self.config_params["aai_password"])
322 os.system(subscription_string)
323 subscription_check += 1
324
325 subscription_url = self.base_url + "/aai/v11/business/customers/customer/{}" \
326 "/service-subscriptions/service-subscription/{}" \
327 .format(self.config_params["customer_name"], self.config_params["service_name"])
328 resp = requests.get(url=subscription_url, headers=self.aai_header, verify=False)
329 if resp.status_code == 200:
330 self.subscription_version = resp.json()['resource-version']
331 print("Subscription resource-version is %s." % self.subscription_version)
332
333 def remove_customer_subscription(self):
334 print("Remove subscription--beginning")
335 subscription_delete_string = 'oclip subscription-delete -x {} -y {} -g {} -m {} -u {} -p {}'.format(
336 self.config_params["customer_name"], self.config_params["service_name"], self.subscription_version,
337 self.config_params["aai_url"],
338 self.config_params["aai_username"], self.config_params["aai_password"])
339 os.system(subscription_delete_string)
340 print("Delete subscription--successful")
341
342 def register_vnfm_helper(self, vnfm_key, values):
343 print("Create vnfm--beginning")
zhaoliping123906480b2020-04-22 11:52:56 +0800344 self.esr_vnfm_id = str(uuid.uuid4())
345 vnfm_create_string = 'oclip vnfm-create -b {} -c {} -e {} -v {} -g {} -x {} ' \
346 '-y {} -i {} -j {} -q {} -m {} -u {} -p {}' \
347 .format(vnfm_key, values.get("type"), values.get("vendor"),
348 values.get("version"), values.get("url"), values.get("vim-id"),
349 self.esr_vnfm_id, values.get("user-name"), values.get("user-password"),
350 values.get("vnfm-version"), self.config_params["aai_url"],
351 self.config_params["aai_username"], self.config_params["aai_password"])
352
353 os.system(vnfm_create_string)
354 print("Create vnfm--successful")
355
356 vnfm_url = self.base_url + "/aai/v11/external-system/esr-vnfm-list"
357 resp = requests.get(url=vnfm_url, headers=self.aai_header, verify=False)
358 if resp.status_code == 200:
359 for vnfm in (resp.json())["esr-vnfm"]:
360 if vnfm['vnfm-id'] == self.esr_vnfm_id:
361 self.esr_vnfm_version = vnfm['resource-version']
362 print("Vnfm %s resource-version is %s."
363 % (self.esr_vnfm_id, self.esr_vnfm_version))
364
365 def register_vnfm(self):
366 vnfm_params = self.config_params["vnfm_params"]
367 for vnfm_key, vnfm_values in vnfm_params.items():
368 self.register_vnfm_helper(vnfm_key, vnfm_values)
369
370 def unregister_vnfm(self):
371 print("Delete vnfm %s" % self.esr_vnfm_id)
372 print("Delete vnfm--beginning")
373 vnfm_delete_string = 'oclip vnfm-delete -x {} -y {} -m {} -u {} -p {}'.format(
374 self.esr_vnfm_id, self.esr_vnfm_version, self.config_params["aai_url"],
375 self.config_params["aai_username"], self.config_params["aai_password"])
376 os.system(vnfm_delete_string)
377 self.esr_vnfm_version = self.esr_vnfm_id = None
378 print("Delete vnfm--successful")
379
380 def create_ns(self):
381 ns = self.config_params["ns"]
382 data = {
383 "context": {
384 "globalCustomerId": self.config_params["customer_name"],
385 "serviceType": self.config_params["service_name"]
386 },
387 "csarId": self.ns_package_id,
388 "nsName": ns.get("name"),
389 "description": "description"
390 }
391 ns_header = {'content-type': 'application/json', 'accept': 'application/json'}
392 ns_url = self.base_url + "/api/nslcm/v1/ns"
393 ns_resp = requests.post(ns_url, data=json.dumps(data), headers=ns_header, verify=False)
394 if 201 == ns_resp.status_code:
395 ns_instance_id = ns_resp.json().get("nsInstanceId")
396 print("create ns successfully, the ns instance id is %s" % ns_instance_id)
397 return ns_instance_id
398 else:
399 raise Exception("Create ns failed.")
400
401 def instantiate_ns(self):
402 print("Instantiate ns beginning")
403 constraints = [
404 {
405 "vnfProfileId": x,
406 "locationConstraints": {
407 "vimId": self.config_params["location"]
408 }
409 } for x in self.vnfdId_list]
410 data = {
411 "additionalParamForNs": {
412 "sdnControllerId": self.config_params["sdc-controller-id"]
413 },
414 "locationConstraints": constraints
415 }
416
417 header = {'content-type': 'application/json', 'accept': 'application/json'}
418 instance_url = self.base_url + "/api/nslcm/v1/ns/" + self.ns_instance_id + "/instantiate"
419 instance_resp = requests.post(instance_url, data=json.dumps(data), headers=header, verify=False)
420 if 200 == instance_resp.status_code:
421 ns_instance_jod_id = instance_resp.json().get("jobId")
422 print("Instantiate ns successfully, the job id is %s" % ns_instance_jod_id)
423 return ns_instance_jod_id
424 else:
425 raise Exception("Instantiate ns failed.")
426
427 def create_ns_package(self):
428 print("Create ns package is beginning")
429 ns = self.config_params["ns"]
430 ns_url = self.base_url + "/api/nsd/v1/ns_descriptors"
431 ns_headers = {'content-type': 'application/json', 'accept': 'application/json'}
432 ns_data = {'userDefinedData': {ns.get("key"): ns.get("value")}}
433 ns_package_reps = requests.post(ns_url, data=json.dumps(ns_data), headers=ns_headers, verify=False)
434 if 201 == ns_package_reps.status_code:
435 print("Create ns package successful, the ns package id is %s"
436 % (ns_package_reps.json()["id"]))
437 return ns_package_reps.json()["id"]
438 else:
zhaoliping1230d9647b2020-08-14 16:43:04 +0800439 raise Exception("Create ns package failed.")
zhaoliping123906480b2020-04-22 11:52:56 +0800440
441 def delete_ns_package(self):
442 print("Delete ns package %s is beginning" % self.ns_package_id)
443 vnf_url = self.base_url + "/api/nsd/v1/ns_descriptors/%s" % self.ns_package_id
444 resp = requests.delete(url=vnf_url, verify=False)
445 if 204 == resp.status_code:
446 print("Delete ns package %s successfully." % self.ns_package_id)
447 self.ns_package_id = None
448 else:
449 print("Delete ns package %s failed." % self.ns_package_id)
450
451 def create_upload_vnf_package(self):
452 print("Create vnf package is beginning")
zhaoliping123906480b2020-04-22 11:52:56 +0800453 vnfs = self.config_params["vnfs"]
454 vnf_url = self.base_url + "/api/vnfpkgm/v1/vnf_packages"
455 header = {'content-type': 'application/json', 'accept': 'application/json'}
456 for vnf_values in vnfs.values():
457 vnf_data = {'userDefinedData': {vnf_values.get("key"): vnf_values.get("value")}}
458 vnf_package_reps = requests.post(vnf_url, data=json.dumps(vnf_data), headers=header, verify=False)
459 if 201 == vnf_package_reps.status_code:
460 print("Create vnf package successful, the vnf package id is %s"
461 % (vnf_package_reps.json()["id"]))
462 package_id = vnf_package_reps.json()["id"]
zhaoliping1230d9647b2020-08-14 16:43:04 +0800463 self.vnf_package_list.append(package_id)
zhaoliping123906480b2020-04-22 11:52:56 +0800464 vnf_upload_url = '{}/api/vnfpkgm/v1/vnf_packages/{}/package_content' \
465 .format(self.config_params["vfc-url"], package_id)
466 file_path = os.path.dirname(os.path.abspath(__file__))
467 csar_file = file_path + "/" + vnf_values.get("path")
468 with open(csar_file, 'rb') as vnf_file:
469 for i in range(10):
470 resp = requests.put(vnf_upload_url, files={'file': vnf_file}, verify=False)
471 if 202 == resp.status_code:
472 break
zhaoliping1230d9647b2020-08-14 16:43:04 +0800473 if 500 == resp.status_code:
474 raise Exception("Upload vnf package failed. %s" % resp.json())
zhaoliping123906480b2020-04-22 11:52:56 +0800475 else:
476 time.sleep(i)
zhaoliping1230d9647b2020-08-14 16:43:04 +0800477 else:
478 print("Create vnf package failed.")
zhaoliping123906480b2020-04-22 11:52:56 +0800479
480 def delete_vnf_package(self):
481 print("Delete vnf package is beginning")
482 for vnf_package_id in self.vnf_package_list:
483 vnf_url = self.base_url + "/api/vnfpkgm/v1/vnf_packages/%s" % vnf_package_id
484 resp = requests.delete(url=vnf_url, verify=False)
485 if 204 == resp.status_code:
486 print("Delete vnf package %s successfully." % vnf_package_id)
487 else:
488 print("Delete vnf package %s failed." % vnf_package_id)
zhaoliping1230d9647b2020-08-14 16:43:04 +0800489 self.vnf_package_list = []
zhaoliping123906480b2020-04-22 11:52:56 +0800490
491 def upload_ns_package(self):
492 ns = self.config_params["ns"]
493 ns_upload_url = '{}/api/nsd/v1/ns_descriptors/{}/nsd_content'.format(self.config_params["vfc-url"],
494 self.ns_package_id)
495 file_path = os.path.dirname(os.path.abspath(__file__))
496 ns_file_path = file_path + "/" + ns["path"]
497 with open(ns_file_path, 'rb') as ns_file:
498 for i in range(10):
499 resp = requests.put(ns_upload_url, files={'file': ns_file}, verify=False)
500 if 204 == resp.status_code:
501 break
zhaoliping1230d9647b2020-08-14 16:43:04 +0800502 if 500 == resp.status_code:
503 raise Exception("Upload ns package failed.")
zhaoliping123906480b2020-04-22 11:52:56 +0800504 else:
505 time.sleep(i)
506
507 def get_vnf_package(self):
508 vnfdid_list = []
509 for vnf_package_id in self.vnf_package_list:
zhaoliping123c981fa02020-05-08 18:33:20 +0800510 n = 60
511 while n > 0:
512 vnf_package_url = self.base_url + '/api/vnfpkgm/v1/vnf_packages/%s' % vnf_package_id
513 vnf_resp = requests.get(vnf_package_url, verify=False)
514 n = n - 1
515 if 200 == vnf_resp.status_code:
516 vnfdId = vnf_resp.json().get("vnfdId")
517 if vnfdId is None:
518 time.sleep(1)
519 else:
520 print("vnfdId is %s" % vnfdId)
521 vnfdid_list.append(vnfdId)
522 break
zhaoliping123906480b2020-04-22 11:52:56 +0800523 return vnfdid_list
524
525 def getVnf(self, vnfs):
526 vnf_list = []
527 for vnf in vnfs:
528 if 'relationship-list' in vnf:
529 for relation in vnf["relationship-list"]["relationship"]:
530 if "service-instance" == relation["related-to"]:
531 if self.ns_instance_id in relation["related-link"]:
532 vnf_list.append(vnf)
533 return vnf_list
534
535 @staticmethod
536 def findVserver(vnf_list):
537 vserver_list = []
538 for vnf in vnf_list:
539 if 'relationship-list' in vnf:
540 for relation in vnf["relationship-list"]["relationship"]:
541 if "vserver" == relation["related-to"]:
542 for relationData in relation["relationship-data"]:
543 if "vserver.vserver-id" == relationData["relationship-key"]:
544 vserver_list.append(relationData["relationship-value"])
545 return vserver_list
546
547 def waitProcessFinished(self, job_id, action):
548 print("Wait for the %s ns finished." % action)
549 job_url = self.base_url + "/api/nslcm/v1/jobs/%s" % job_id
550 progress = 0
551 n = 6000
552 while n > 0:
553 job_resp = requests.get(url=job_url, verify=False)
554 n = n - 1
555 if 200 == job_resp.status_code:
556 if "responseDescriptor" in job_resp.json():
557 progress_rep = (job_resp.json())["responseDescriptor"]["progress"]
558 if 100 != progress_rep:
559 if 255 == progress_rep:
560 print("Ns %s %s failed." % (self.ns_instance_id, action))
zhaoliping1230d9647b2020-08-14 16:43:04 +0800561 raise Exception("%s ns failed." % action)
zhaoliping123906480b2020-04-22 11:52:56 +0800562 elif progress_rep != progress:
563 progress = progress_rep
564 print("Ns %s %s process is %s." % (self.ns_instance_id, action, progress))
565 time.sleep(0.2)
566 else:
567 print("Ns %s %s process is %s." % (self.ns_instance_id, action, progress_rep))
568 print("Ns %s %s successfully." % (self.ns_instance_id, action))
569 break
570
571 def terminateNs(self):
572 print("Terminate ns--beginning")
573 ns_url = self.base_url + "/api/nslcm/v1/ns/%s" % self.ns_instance_id
574 d = {
575 "gracefulTerminationTimeout": 600,
576 "terminationType": "FORCEFUL"
577 }
zhaoliping1230d9647b2020-08-14 16:43:04 +0800578 try:
579 res = requests.post(url=ns_url + "/terminate", data=d, verify=False)
580 if 202 == res.status_code:
581 terminate_ns_job_id = res.json()["jobId"]
582 print("Terminate job is %s" % terminate_ns_job_id)
583 else:
584 raise Exception("Instantiate ns failed.")
585 self.waitProcessFinished(terminate_ns_job_id, "terminate")
586 except Exception as e:
587 print(e.args[0])
zhaoliping123906480b2020-04-22 11:52:56 +0800588
589 def deleteNs(self):
590 print("Delete ns %s --beginning" % self.ns_instance_id)
591 ns_url = self.base_url + "/api/nslcm/v1/ns/%s" % self.ns_instance_id
592 res = requests.delete(ns_url, verify=False)
593 if 204 == res.status_code:
594 print("Ns %s delete successfully." % self.ns_instance_id)
595 self.ns_instance_id = None
596
597 def testNs(self):
598 print("Use csar file is uploaded by local")
zhaoliping123906480b2020-04-22 11:52:56 +0800599 try:
zhaoliping1230d9647b2020-08-14 16:43:04 +0800600 self.create_upload_vnf_package()
601 self.ns_package_id = self.create_ns_package()
602 print("Get vnfdId list.")
603 self.vnfdId_list = self.get_vnf_package()
604 if len(self.vnfdId_list) < 5:
605 raise Exception("Upload vnf package failed. "
606 "Please check vnf package(b1bb0ce7-1111-4fa7-95ed-4840d70a1177, "
607 "b1bb0ce7-2222-4fa7-95ed-4840d70a1177, "
608 "b1bb0ce7-3333-4fa7-95ed-4840d70a1177, "
609 "b1bb0ce7-4444-4fa7-95ed-4840d70a1177, "
610 "b1bb0ce7-5555-4fa7-95ed-4840d70a1177) "
611 "and delete them and then upload again.")
612 print("Upload ns package from csar beginning")
613 self.upload_ns_package()
614 print("Upload ns package from csar successfully")
615
616 print("Create ns beginning")
617
zhaoliping123906480b2020-04-22 11:52:56 +0800618 self.ns_instance_id = self.create_ns()
619 self.assertIsNotNone(self.ns_instance_id)
620 self.ns_instance_jod_id = self.instantiate_ns()
zhaoliping1230d9647b2020-08-14 16:43:04 +0800621 print("NS %s instantiate job is %s" % (self.ns_instance_id, self.ns_instance_jod_id))
622 self.assertIsNotNone(self.ns_instance_jod_id)
623 self.waitProcessFinished(self.ns_instance_jod_id, "instantiate")
zhaoliping123906480b2020-04-22 11:52:56 +0800624 except Exception as e:
625 print(e.args[0])
626
zhaoliping123906480b2020-04-22 11:52:56 +0800627 vnf_aai_url = self.base_url + "/aai/v11/network/generic-vnfs"
628 vnf_resp = requests.get(url=vnf_aai_url, headers=self.aai_header, verify=False)
629 self.assertEqual(200, vnf_resp.status_code)
630
631 vnfs = vnf_resp.json()["generic-vnf"]
632 vnf_list = self.getVnf(vnfs)
633 self.assertEqual(5, len(vnf_list))
634 print("There are %s vnfs are created." % len(vnf_list))
635 for vnf in vnf_list:
636 print("The vnf %s are created successfully." % vnf.get("vnf-id"))
637
638 vserver_list = self.findVserver(vnf_list)
639 print("The vserver %s is created successfully." % len(vserver_list))
640 self.assertEqual(8, len(vserver_list))
641
642 cloud_region_id = list(self.config_params["cloud_region_data"].keys())[0]
643
644 for vserver_id in vserver_list:
645 vserver_aai_url = self.base_url + "/aai/v11/cloud-infrastructure/cloud-regions/cloud-region" \
646 "/{}/{}/tenants/tenant/{}/vservers/vserver/{}?depth=all" \
647 .format(self.config_params["cloud-owner"], cloud_region_id, self.tenant_id, vserver_id)
648
649 vserver_resp = requests.get(url=vserver_aai_url, headers=self.aai_header, verify=False)
650 self.assertEqual(200, vserver_resp.status_code)
651 print("The vserver %s is created successfully." % vserver_id)