blob: b83d3eb753e32ddcbb871c6be3aea477990c5447 [file] [log] [blame]
Haibin Huang9b514232019-05-09 14:36:05 +08001#!/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
15import json
16import os
17import time
18import argparse
19import sys
Haibin Huangee3b43d2019-05-21 10:53:59 +080020import requests
Haibin Huang9b514232019-05-09 14:36:05 +080021
22def get_parameters(file):
23 parameters = json.load(file)
24 return parameters
25
26def get_out_helper(in_string):
27 out_list = (((in_string.replace('-','')).replace('|', '')).replace('+', '')).split()
28 return out_list
29
30def get_out_helper_2(in_string):
31 out_list = ((in_string.replace('|', '')).replace('+', '')).split()
32 return out_list
33
34def 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
38def 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
52def 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
76def 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 Huang48dc6812019-05-16 11:51:20 +000081def 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
91def 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 Huang9b514232019-05-09 14:36:05 +080097#VNF Deployment Section
98def 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
133def onboard_vnf(parameters):
Haibin Huangee3b43d2019-05-21 10:53:59 +0800134 vnfs = parameters["vnfs"]
135 vnf_onboard_outputs = {}
Haibin Huang9b514232019-05-09 14:36:05 +0800136
Haibin Huangee3b43d2019-05-21 10:53:59 +0800137 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 Huang9b514232019-05-09 14:36:05 +0800141
142def 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 Huangee3b43d2019-05-21 10:53:59 +0800147def 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 Huang9b514232019-05-09 14:36:05 +0800152 ns_create_out = (os.popen(ns_create_string)).read()
Haibin Huangee3b43d2019-05-21 10:53:59 +0800153 print ns_create_out
154 ns_instance_id = (get_out_helper_2(ns_create_out))[3]
155 return ns_instance_id
Haibin Huang9b514232019-05-09 14:36:05 +0800156
Haibin Huangee3b43d2019-05-21 10:53:59 +0800157def 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 Huang9b514232019-05-09 14:36:05 +0800162 ns_instantiate_out = (os.popen(ns_instantiate_string)).read()
163 return ns_instantiate_out
164
Haibin Huangee3b43d2019-05-21 10:53:59 +0800165def 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
173def 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
186def 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
194def 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 Huang9b514232019-05-09 14:36:05 +0800203#Run Functions
204parser = argparse.ArgumentParser()
205parser.add_argument('-f', action='store', dest='config_file_path', help='Store config file path')
Haibin Huangee3b43d2019-05-21 10:53:59 +0800206parser.add_argument('-t', action='store', dest='type', help='Store config file path')
Haibin Huang9b514232019-05-09 14:36:05 +0800207
208parser.add_argument('--version', action='version', version='%(prog)s 1.0')
209
210results = parser.parse_args()
211
212config_file_path = results.config_file_path
213if config_file_path is None:
214 sys.exit(1)
215config_file = open(config_file_path)
216parameters = get_parameters(config_file)
217
218# 1.Set cli command envionment
219set_open_cli_env(parameters)
220
221# 2.Create cloud complex
222create_complex(parameters)
223
Haibin Huang48dc6812019-05-16 11:51:20 +0000224# 3.Register all clouds
225register_all_clouds(parameters)
226
227# 4.Register vnfm
228register_vnfm(parameters)
229
Haibin Huangee3b43d2019-05-21 10:53:59 +0800230# 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 Huang9b514232019-05-09 14:36:05 +0800233# SDC output data model is not align with VFC, we use an workaround method
234# We just do run time automation
Haibin Huangee3b43d2019-05-21 10:53:59 +0800235ns_package_output = ""
236
237if 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
243else:
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 Huang9b514232019-05-09 14:36:05 +0800253
Haibin Huang48dc6812019-05-16 11:51:20 +0000254# 6.add_policies function not currently working, using curl commands
Haibin Huang9b514232019-05-09 14:36:05 +0800255# add_policies(parameters)
256
Haibin Huang48dc6812019-05-16 11:51:20 +0000257# 7. VFC part
Haibin Huangee3b43d2019-05-21 10:53:59 +0800258ns_instance_id = create_ns(parameters, ns_package_output)
259print ns_instance_id
260instantiate_ns_output = instantiate_ns(parameters, ns_instance_id)
Haibin Huang9b514232019-05-09 14:36:05 +0800261print instantiate_ns_output