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 |
| 20 | |
| 21 | def get_parameters(file): |
| 22 | parameters = json.load(file) |
| 23 | return parameters |
| 24 | |
| 25 | def get_out_helper(in_string): |
| 26 | out_list = (((in_string.replace('-','')).replace('|', '')).replace('+', '')).split() |
| 27 | return out_list |
| 28 | |
| 29 | def get_out_helper_2(in_string): |
| 30 | out_list = ((in_string.replace('|', '')).replace('+', '')).split() |
| 31 | return out_list |
| 32 | |
| 33 | def set_open_cli_env(parameters): |
| 34 | os.environ["OPEN_CLI_PRODUCT_IN_USE"] = parameters["open_cli_product"] |
| 35 | os.environ["OPEN_CLI_HOME"] = parameters["open_cli_home"] |
| 36 | |
| 37 | def create_complex(parameters): |
| 38 | complex_create_string = "oclip complex-create -j {} -r {} -x {} -y {} -lt {} -l {} -i {} -lo {} \ |
| 39 | -S {} -la {} -g {} -w {} -z {} -k {} -o {} -q {} -m {} -u {} -p {}".format(parameters["street2"], \ |
| 40 | parameters["physical_location"], parameters["complex_name"], \ |
| 41 | parameters["data_center_code"], parameters["latitude"], parameters["region"], \ |
| 42 | parameters["street1"], parameters["longitude"], parameters["state"], \ |
| 43 | parameters["lata"], parameters["city"], parameters["postal-code"], \ |
| 44 | parameters["complex_name"], parameters["country"], parameters["elevation"], \ |
| 45 | parameters["identity_url"], parameters["aai_url"], parameters["aai_username"], \ |
| 46 | parameters["aai_password"]) |
| 47 | |
| 48 | os.system(complex_create_string) |
| 49 | |
| 50 | |
| 51 | def register_cloud_helper(cloud_region, values, parameters): |
| 52 | #Create Cloud |
| 53 | cloud_create_string = 'oclip cloud-create -e {} -b {} -I {{\\\\\\"openstack-region-id\\\\\\":\\\\\\"{}\\\\\\"}} \ |
| 54 | -x {} -y {} -j {} -w {} -l {} -url {} -n {} -q {} -r {} -Q {} -i {} -g {} -z {} -k {} -c {} -m {} -u {} -p {}'.format( |
| 55 | values.get("esr-system-info-id"), values.get("user-name"), cloud_region, parameters["cloud-owner"], \ |
| 56 | cloud_region, values.get("password"), values.get("cloud-region-version"), values.get("default-tenant"), \ |
| 57 | values.get("service-url"), parameters["complex_name"], values.get("cloud-type"), parameters["owner-defined-type"], \ |
| 58 | values.get("system-type"), values.get("identity-url"), parameters["cloud-zone"], values.get("ssl-insecure"), \ |
| 59 | values.get("system-status"), values.get("cloud-domain"), parameters["aai_url"], parameters["aai_username"], \ |
| 60 | parameters["aai_password"]) |
| 61 | |
| 62 | |
| 63 | os.system(cloud_create_string) |
| 64 | |
| 65 | #Associate Cloud with complex |
| 66 | complex_associate_string = "oclip complex-associate -x {} -y {} -z {} -m {} -u {} -p {}".format(parameters["complex_name"], \ |
| 67 | cloud_region, parameters["cloud-owner"], parameters["aai_url"], parameters["aai_username"], parameters["aai_password"]) |
| 68 | os.system(complex_associate_string) |
| 69 | |
| 70 | #Register Cloud with Multicloud |
| 71 | multicloud_register_string = "oclip multicloud-register-cloud -y {} -x {} -m {}".format(parameters["cloud-owner"], \ |
| 72 | cloud_region, parameters["multicloud_url"]) |
| 73 | os.system(multicloud_register_string) |
| 74 | |
| 75 | def register_all_clouds(parameters): |
| 76 | cloud_dictionary = parameters["cloud_region_data"] |
| 77 | for cloud_region, cloud_region_values in cloud_dictionary.iteritems(): |
| 78 | register_cloud_helper(cloud_region, cloud_region_values, parameters) |
| 79 | |
| 80 | #VNF Deployment Section |
| 81 | def add_policies(parameters): |
| 82 | resource_string = (os.popen("oclip get-resource-module-name -u {} -p {} -m {} |grep {}".format(\ |
| 83 | parameters["sdc_creator"], parameters["sdc_password"], parameters["sdc_catalog_url"], \ |
| 84 | parameters["service-model-name"] ))).read() |
| 85 | resource_module_name = (get_out_helper_2(resource_string))[1] |
| 86 | |
| 87 | #Put in the right resource module name in all policies located in parameters["policy_directory"] |
| 88 | os.system("find {}/ -type f -exec sed -i 's/{}/{}/g' {{}} \;".format( |
| 89 | parameters["policy_directory"], parameters["temp_resource_module_name"], resource_module_name)) |
| 90 | |
| 91 | #Upload policy models |
| 92 | for model in os.listdir(parameters["policy_models_directory"]): |
| 93 | os.system("oclip policy-type-create -x {} -u {} -p {} -m {}".format(model, parameters["policy_username"], \ |
| 94 | parameters["policy_password"], parameters["policy_url"])) |
| 95 | time.sleep(0.5) |
| 96 | |
| 97 | #print("Put in the resourceModuleName {} in your policy files in {}. ".format(resource_module_name, \ |
| 98 | #(parameters["policy_directory"]))) |
| 99 | #raw_input("Press Enter to continue...") |
| 100 | |
| 101 | |
| 102 | #Loop through policy, put in resource_model_name and create policies |
| 103 | for policy in os.listdir(parameters["policy_directory"]): |
| 104 | policy_name = "{}.{}".format(parameters["policy_scope"], os.path.splitext(policy)[0]) |
| 105 | policy_file = (os.path.join(parameters["policy_directory"], policy)) |
| 106 | #Create policy |
| 107 | os.system("oclip policy-create-outdated -m {} -u {} -p {} -x {} -S {} -T {} -o {} -b $(cat {})".format(parameters["policy_url"],\ |
| 108 | parameters["policy_username"], parameters["policy_password"], policy_name, parameters["policy_scope"], \ |
| 109 | parameters["policy_config_type"], parameters["policy_onapName"], policy_file)) |
| 110 | |
| 111 | #Push policy |
| 112 | os.system("oclip policy-push-outdated -m {} -u {} -p {} -x {} -b {} -c {}".format(parameters["policy_url"], \ |
| 113 | parameters["policy_username"], parameters["policy_password"], policy_name, parameters["policy_config_type"],\ |
| 114 | parameters["policy_pdp_group"])) |
| 115 | |
| 116 | def onboard_vnf(parameters): |
| 117 | ns_csars = parameters["vnd-csars"] |
| 118 | vnf_onboard_outs = {} |
| 119 | |
| 120 | for key, value in ns_csars.items(): |
| 121 | vnf_onboard_string = 'oclip vfc-catalog-onboard-vnf -c {}'.format(value) |
| 122 | vnf_onboard_outs["key"] = (os.popen(ns_onboard_string)).read() |
| 123 | return vnf_onboard_outs |
| 124 | |
| 125 | def onboard_ns(parameters): |
| 126 | ns_onboard_string = 'oclip vfc-catalog-onboard-ns -c {}'.format(parameters["ns-csar-id"]) |
| 127 | ns_onboard_out = (os.popen(ns_onboard_string)).read() |
| 128 | return ns_onboard_out |
| 129 | |
| 130 | def create_ns(parameters): |
| 131 | ns_create_string = 'oclip vfc-nslcm-create -c {} -n {}'.format(parameters["ns-csar-id"], \ |
| 132 | parameters["ns-csar-name"]) |
| 133 | ns_create_out = (os.popen(ns_create_string)).read() |
| 134 | ns_instance_id = (get_out_helper_2(ns_create_out))[1] |
| 135 | ns_model_dict["vnf_instance_id"] = ns_instance_id |
| 136 | return ns_model_dict |
| 137 | |
| 138 | def instantiate_ns(parameters, ns_model_dict): |
| 139 | ns_instance_id = ns_model_dict["ns_instance_id"] |
| 140 | ns_instantiate_string = 'oclip vfc-nslcm-instantiate -i {} -c {} -n {}'.format(ns_instance_id, \ |
| 141 | parameters["location-constraints"], parameters["sdc-controller-id"]) |
| 142 | ns_instantiate_out = (os.popen(ns_instantiate_string)).read() |
| 143 | return ns_instantiate_out |
| 144 | |
| 145 | #Run Functions |
| 146 | parser = argparse.ArgumentParser() |
| 147 | parser.add_argument('-f', action='store', dest='config_file_path', help='Store config file path') |
| 148 | |
| 149 | parser.add_argument('--version', action='version', version='%(prog)s 1.0') |
| 150 | |
| 151 | results = parser.parse_args() |
| 152 | |
| 153 | config_file_path = results.config_file_path |
| 154 | if config_file_path is None: |
| 155 | sys.exit(1) |
| 156 | config_file = open(config_file_path) |
| 157 | parameters = get_parameters(config_file) |
| 158 | |
| 159 | # 1.Set cli command envionment |
| 160 | set_open_cli_env(parameters) |
| 161 | |
| 162 | # 2.Create cloud complex |
| 163 | create_complex(parameters) |
| 164 | |
| 165 | # 3.FIXME:Because SDC internal API will change without notice, so I will maually design VNF and Service. |
| 166 | # SDC output data model is not align with VFC, we use an workaround method |
| 167 | # We just do run time automation |
| 168 | |
| 169 | # 4.Register all clouds |
| 170 | register_all_clouds(parameters) |
| 171 | |
| 172 | # 5.add_policies function not currently working, using curl commands |
| 173 | # add_policies(parameters) |
| 174 | |
| 175 | # 6. VFC part |
| 176 | vnf_onboard_output = onboard_vnf(parameters) |
| 177 | print vnf_onboard_output |
| 178 | ns_onboard_out = onboard_ns(parameters) |
| 179 | print ns_onboard_out |
| 180 | ns_model_dict = create_ns(parameters) |
| 181 | print ns_model_dict |
| 182 | instantiate_ns_output = instantiate_ns(parameters, ns_model_dict) |
| 183 | print instantiate_ns_output |