Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | #Prerequisites for machine to run this |
| 4 | #Put in required parameters in hpa_automation_config.json |
| 5 | #Install python-pip (apt install python-pip) |
| 6 | #Install python mysql.connector (pip install mysql-connector-python) |
| 7 | #Install ONAP CLI |
| 8 | #Must have connectivity to the ONAP, a k8s vm already running is recommended |
| 9 | #Create Preload File, the script will modify the parameters required from serivce model, service instance |
| 10 | #and vnf instance |
| 11 | #Create policies for homing |
| 12 | #Put in CSAR file |
| 13 | #modify so-bpmn configmap and change version to v2 |
| 14 | |
| 15 | import json |
| 16 | import os |
| 17 | import time |
| 18 | import argparse |
| 19 | import sys |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 20 | import requests |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 21 | |
| 22 | def get_parameters(file): |
| 23 | parameters = json.load(file) |
| 24 | return parameters |
| 25 | |
| 26 | def get_out_helper(in_string): |
| 27 | out_list = (((in_string.replace('-','')).replace('|', '')).replace('+', '')).split() |
| 28 | return out_list |
| 29 | |
| 30 | def get_out_helper_2(in_string): |
| 31 | out_list = ((in_string.replace('|', '')).replace('+', '')).split() |
| 32 | return out_list |
| 33 | |
| 34 | def set_open_cli_env(parameters): |
| 35 | os.environ["OPEN_CLI_PRODUCT_IN_USE"] = parameters["open_cli_product"] |
| 36 | os.environ["OPEN_CLI_HOME"] = parameters["open_cli_home"] |
| 37 | |
| 38 | def create_complex(parameters): |
| 39 | complex_create_string = "oclip complex-create -j {} -r {} -x {} -y {} -lt {} -l {} -i {} -lo {} \ |
| 40 | -S {} -la {} -g {} -w {} -z {} -k {} -o {} -q {} -m {} -u {} -p {}".format(parameters["street2"], \ |
| 41 | parameters["physical_location"], parameters["complex_name"], \ |
| 42 | parameters["data_center_code"], parameters["latitude"], parameters["region"], \ |
| 43 | parameters["street1"], parameters["longitude"], parameters["state"], \ |
| 44 | parameters["lata"], parameters["city"], parameters["postal-code"], \ |
| 45 | parameters["complex_name"], parameters["country"], parameters["elevation"], \ |
| 46 | parameters["identity_url"], parameters["aai_url"], parameters["aai_username"], \ |
| 47 | parameters["aai_password"]) |
| 48 | |
| 49 | os.system(complex_create_string) |
| 50 | |
| 51 | |
| 52 | def register_cloud_helper(cloud_region, values, parameters): |
| 53 | #Create Cloud |
| 54 | cloud_create_string = 'oclip cloud-create -e {} -b {} -I {{\\\\\\"openstack-region-id\\\\\\":\\\\\\"{}\\\\\\"}} \ |
| 55 | -x {} -y {} -j {} -w {} -l {} -url {} -n {} -q {} -r {} -Q {} -i {} -g {} -z {} -k {} -c {} -m {} -u {} -p {}'.format( |
| 56 | values.get("esr-system-info-id"), values.get("user-name"), cloud_region, parameters["cloud-owner"], \ |
| 57 | cloud_region, values.get("password"), values.get("cloud-region-version"), values.get("default-tenant"), \ |
| 58 | values.get("service-url"), parameters["complex_name"], values.get("cloud-type"), parameters["owner-defined-type"], \ |
| 59 | values.get("system-type"), values.get("identity-url"), parameters["cloud-zone"], values.get("ssl-insecure"), \ |
| 60 | values.get("system-status"), values.get("cloud-domain"), parameters["aai_url"], parameters["aai_username"], \ |
| 61 | parameters["aai_password"]) |
| 62 | |
| 63 | |
| 64 | os.system(cloud_create_string) |
| 65 | |
| 66 | #Associate Cloud with complex |
| 67 | complex_associate_string = "oclip complex-associate -x {} -y {} -z {} -m {} -u {} -p {}".format(parameters["complex_name"], \ |
| 68 | cloud_region, parameters["cloud-owner"], parameters["aai_url"], parameters["aai_username"], parameters["aai_password"]) |
| 69 | os.system(complex_associate_string) |
| 70 | |
| 71 | #Register Cloud with Multicloud |
| 72 | multicloud_register_string = "oclip multicloud-register-cloud -y {} -x {} -m {}".format(parameters["cloud-owner"], \ |
| 73 | cloud_region, parameters["multicloud_url"]) |
| 74 | os.system(multicloud_register_string) |
| 75 | |
| 76 | def register_all_clouds(parameters): |
| 77 | cloud_dictionary = parameters["cloud_region_data"] |
| 78 | for cloud_region, cloud_region_values in cloud_dictionary.iteritems(): |
| 79 | register_cloud_helper(cloud_region, cloud_region_values, parameters) |
| 80 | |
Haibin Huang | 48dc681 | 2019-05-16 11:51:20 +0000 | [diff] [blame] | 81 | def register_vnfm_helper(vnfm_key, values, parameters): |
| 82 | #Create vnfm |
| 83 | vnfm_create_string = 'oclip vnfm-create -b {} -c {} -e {} -v {} -g {} -x {} -i {} -j {} -q {} \ |
| 84 | -m {} -u {} -p {}'.format(vnfm_key, values.get("type"), values.get("vendor"), \ |
| 85 | values.get("version"), values.get("url"), values.get("vim-id"), \ |
| 86 | values.get("user-name"), values.get("user-password"), values.get("vnfm-version"), \ |
| 87 | parameters["aai_url"], parameters["aai_username"], parameters["aai_password"]) |
| 88 | |
| 89 | os.system(vnfm_create_string) |
| 90 | |
| 91 | def register_vnfm(parameters): |
| 92 | vnfm_params = parameters["vnfm_params"] |
| 93 | for vnfm_key, vnfm_values in vnfm_params.iteritems(): |
| 94 | register_vnfm_helper(vnfm_key, vnfm_values, parameters) |
| 95 | |
| 96 | |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 97 | #VNF Deployment Section |
| 98 | def add_policies(parameters): |
| 99 | resource_string = (os.popen("oclip get-resource-module-name -u {} -p {} -m {} |grep {}".format(\ |
| 100 | parameters["sdc_creator"], parameters["sdc_password"], parameters["sdc_catalog_url"], \ |
| 101 | parameters["service-model-name"] ))).read() |
| 102 | resource_module_name = (get_out_helper_2(resource_string))[1] |
| 103 | |
| 104 | #Put in the right resource module name in all policies located in parameters["policy_directory"] |
| 105 | os.system("find {}/ -type f -exec sed -i 's/{}/{}/g' {{}} \;".format( |
| 106 | parameters["policy_directory"], parameters["temp_resource_module_name"], resource_module_name)) |
| 107 | |
| 108 | #Upload policy models |
| 109 | for model in os.listdir(parameters["policy_models_directory"]): |
| 110 | os.system("oclip policy-type-create -x {} -u {} -p {} -m {}".format(model, parameters["policy_username"], \ |
| 111 | parameters["policy_password"], parameters["policy_url"])) |
| 112 | time.sleep(0.5) |
| 113 | |
| 114 | #print("Put in the resourceModuleName {} in your policy files in {}. ".format(resource_module_name, \ |
| 115 | #(parameters["policy_directory"]))) |
| 116 | #raw_input("Press Enter to continue...") |
| 117 | |
| 118 | |
| 119 | #Loop through policy, put in resource_model_name and create policies |
| 120 | for policy in os.listdir(parameters["policy_directory"]): |
| 121 | policy_name = "{}.{}".format(parameters["policy_scope"], os.path.splitext(policy)[0]) |
| 122 | policy_file = (os.path.join(parameters["policy_directory"], policy)) |
| 123 | #Create policy |
| 124 | os.system("oclip policy-create-outdated -m {} -u {} -p {} -x {} -S {} -T {} -o {} -b $(cat {})".format(parameters["policy_url"],\ |
| 125 | parameters["policy_username"], parameters["policy_password"], policy_name, parameters["policy_scope"], \ |
| 126 | parameters["policy_config_type"], parameters["policy_onapName"], policy_file)) |
| 127 | |
| 128 | #Push policy |
| 129 | os.system("oclip policy-push-outdated -m {} -u {} -p {} -x {} -b {} -c {}".format(parameters["policy_url"], \ |
| 130 | parameters["policy_username"], parameters["policy_password"], policy_name, parameters["policy_config_type"],\ |
| 131 | parameters["policy_pdp_group"])) |
| 132 | |
| 133 | def onboard_vnf(parameters): |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 134 | vnfs = parameters["vnfs"] |
| 135 | vnf_onboard_outputs = {} |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 136 | |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 137 | for key, value in vnfs.items(): |
| 138 | vnf_onboard_string = 'oclip vfc-catalog-onboard-vnf -c {}'.format(value.get("csar-id")) |
| 139 | vnf_onboard_outs[key] = (os.popen(ns_onboard_string)).read() |
| 140 | return vnf_onboard_outputs |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 141 | |
| 142 | def onboard_ns(parameters): |
| 143 | ns_onboard_string = 'oclip vfc-catalog-onboard-ns -c {}'.format(parameters["ns-csar-id"]) |
| 144 | ns_onboard_out = (os.popen(ns_onboard_string)).read() |
| 145 | return ns_onboard_out |
| 146 | |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 147 | def create_ns(parameters, csar_id): |
| 148 | ns = parameters["ns"] |
| 149 | ns_create_string = 'oclip vfc-nslcm-create -m {} -c {} -n {}'.format(parameters["vfc-url"], \ |
| 150 | csar_id, ns.get("name")) |
| 151 | print ns_create_string |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 152 | ns_create_out = (os.popen(ns_create_string)).read() |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 153 | print ns_create_out |
| 154 | ns_instance_id = (get_out_helper_2(ns_create_out))[3] |
| 155 | return ns_instance_id |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 156 | |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 157 | def instantiate_ns(parameters, ns_instance_id): |
| 158 | ns_instantiate_string = 'oclip vfc-nslcm-instantiate -m {} -i {} -c {} -n {}'.format(parameters["vfc-url"], \ |
| 159 | ns_instance_id, parameters["location"], parameters["sdc-controller-id"]) |
| 160 | print ns_instantiate_string |
| 161 | |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 162 | ns_instantiate_out = (os.popen(ns_instantiate_string)).read() |
| 163 | return ns_instantiate_out |
| 164 | |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 165 | def create_ns_package(parameters): |
| 166 | ns = parameters["ns"] |
| 167 | create_ns_string = 'oclip vfc-catalog-create-ns -m {} -c {} -e {}'.format(parameters["vfc-url"], \ |
| 168 | ns.get("key"), ns.get("value")) |
| 169 | cmd_out = (os.popen(create_ns_string)).read() |
| 170 | out_list = get_out_helper_2(cmd_out) |
| 171 | return out_list[4] |
| 172 | |
| 173 | def create_vnf_package(parameters): |
| 174 | vnfs = parameters["vnfs"] |
| 175 | outputs = {} |
| 176 | |
| 177 | for vnf_key, vnf_values in vnfs.iteritems(): |
| 178 | create_vnf_string = 'oclip vfc-catalog-create-vnf -m {} -c {} -e {}'.format(parameters["vfc-url"], \ |
| 179 | vnf_values.get("key"), vnf_values.get("value")) |
| 180 | cmd_out = (os.popen(create_vnf_string)).read() |
| 181 | out_list = get_out_helper_2(cmd_out) |
| 182 | outputs[vnf_key] = out_list[4] |
| 183 | |
| 184 | return outputs |
| 185 | |
| 186 | def upload_ns_package(parameters, ns_package_output): |
| 187 | ns = parameters["ns"] |
| 188 | ns_upload_string = '{}/api/nsd/v1/ns_descriptors/{}/nsd_content'.format(parameters["vfc-url"], ns_package_output) |
| 189 | print ns_upload_string |
| 190 | print ns.get("path") |
| 191 | resp = requests.put(ns_upload_string, files={'file': open(ns.get("path"), 'rb')}) |
| 192 | return resp |
| 193 | |
| 194 | def upload_vnf_package(parameters, vnf_package_output): |
| 195 | vnfs = parameters["vnfs"] |
| 196 | for vnf_key, vnf_values in vnfs.iteritems(): |
| 197 | vnf_upload_str = '{}/api/vnfpkgm/v1/vnf_packages/{}/package_content'.format(parameters["vfc-url"], \ |
| 198 | vnf_package_output[vnf_key], vnf_package_output[vnf_key]) |
| 199 | resp = requests.put(vnf_upload_str, files={'file': open(vnf_values.get("path"), 'rb')}) |
| 200 | return resp |
| 201 | |
| 202 | |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 203 | #Run Functions |
| 204 | parser = argparse.ArgumentParser() |
| 205 | parser.add_argument('-f', action='store', dest='config_file_path', help='Store config file path') |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 206 | parser.add_argument('-t', action='store', dest='type', help='Store config file path') |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 207 | |
| 208 | parser.add_argument('--version', action='version', version='%(prog)s 1.0') |
| 209 | |
| 210 | results = parser.parse_args() |
| 211 | |
| 212 | config_file_path = results.config_file_path |
| 213 | if config_file_path is None: |
| 214 | sys.exit(1) |
| 215 | config_file = open(config_file_path) |
| 216 | parameters = get_parameters(config_file) |
| 217 | |
| 218 | # 1.Set cli command envionment |
| 219 | set_open_cli_env(parameters) |
| 220 | |
| 221 | # 2.Create cloud complex |
| 222 | create_complex(parameters) |
| 223 | |
Haibin Huang | 48dc681 | 2019-05-16 11:51:20 +0000 | [diff] [blame] | 224 | # 3.Register all clouds |
| 225 | register_all_clouds(parameters) |
| 226 | |
| 227 | # 4.Register vnfm |
| 228 | register_vnfm(parameters) |
| 229 | |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 230 | # 5.create csar file |
| 231 | # 5.1 upload csar file to catalog |
| 232 | # 5.2 FIXME:Because SDC internal API will change without notice, so I will maually design VNF and Service. |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 233 | # SDC output data model is not align with VFC, we use an workaround method |
| 234 | # We just do run time automation |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 235 | ns_package_output = "" |
| 236 | |
| 237 | if type == "sdc": |
| 238 | print "use csar file is distributed by sdc" |
| 239 | # vnf_onboard_output = onboard_vnf(parameters) |
| 240 | # print vnf_onboard_output |
| 241 | # ns_onboard_out = onboard_ns(parameters) |
| 242 | # print ns_onboard_out |
| 243 | else: |
| 244 | print "use csar file is uploaded by local" |
| 245 | vnf_package_output = create_vnf_package(parameters) |
| 246 | print vnf_package_output |
| 247 | ns_package_output = create_ns_package(parameters) |
| 248 | print ns_package_output |
| 249 | upload_vnf_out = upload_vnf_package(parameters, vnf_package_output) |
| 250 | print upload_vnf_out |
| 251 | upload_ns_out = upload_ns_package(parameters, ns_package_output) |
| 252 | print upload_ns_out |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 253 | |
Haibin Huang | 48dc681 | 2019-05-16 11:51:20 +0000 | [diff] [blame] | 254 | # 6.add_policies function not currently working, using curl commands |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 255 | # add_policies(parameters) |
| 256 | |
Haibin Huang | 48dc681 | 2019-05-16 11:51:20 +0000 | [diff] [blame] | 257 | # 7. VFC part |
Haibin Huang | ee3b43d | 2019-05-21 10:53:59 +0800 | [diff] [blame^] | 258 | ns_instance_id = create_ns(parameters, ns_package_output) |
| 259 | print ns_instance_id |
| 260 | instantiate_ns_output = instantiate_ns(parameters, ns_instance_id) |
Haibin Huang | 9b51423 | 2019-05-09 14:36:05 +0800 | [diff] [blame] | 261 | print instantiate_ns_output |