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