1 # Copyright 2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
21 from lcm.pub.utils.values import ignore_case_get, set_opt_val
23 from .exceptions import VimException
25 logger = logging.getLogger(__name__)
27 RES_EXIST, RES_NEW = 0, 1
28 NET_PRIVATE, NET_SHSRED = 0, 1
29 VLAN_TRANSPARENT_NO, VLAN_TRANSPARENT_YES = 0, 1
31 DHCP_DISABLED, DHCP_ENABLED = 0, 1
32 OPT_CREATE_VOLUME = 20
33 OPT_CREATE_NETWORK = 30
34 OPT_CREATE_SUBNET = 40
36 OPT_CREATE_FLAVOR = 60
42 def create_vim_res(data, do_notify, do_rollback):
44 for vol in ignore_case_get(data, "volume_storages"):
45 create_volume(vol, do_notify, OPT_CREATE_VOLUME)
46 for network in ignore_case_get(data, "vls"):
47 create_network(network, do_notify, OPT_CREATE_NETWORK)
48 for subnet in ignore_case_get(data, "vls"):
49 create_subnet(subnet, do_notify, OPT_CREATE_SUBNET)
50 for port in ignore_case_get(data, "cps"):
51 create_port(port, do_notify, OPT_CREATE_PORT)
52 for flavor in ignore_case_get(data, "vdus"):
53 create_flavor(flavor, do_notify, OPT_CREATE_FLAVOR)
54 for vm in ignore_case_get(data, "vdus"):
55 create_vm(vm, do_notify, OPT_CREATE_VM)
56 do_notify(RES_END, {})
57 except VimException as e:
58 logger.error(e.message)
59 do_rollback(e.message)
61 logger.error(traceback.format_exc())
62 do_rollback(str(sys.exc_info()))
64 def create_volume(vol, do_notify, progress):
66 "tenant": vol["properties"]["location_info"]["tenant"],
67 "volumeName": vol["properties"]["volume_name"],
68 "volumeSize": int(ignore_case_get(vol["properties"], "size", "0"))
70 set_opt_val(param, "imageName", ignore_case_get(vol, "image_file"))
71 set_opt_val(param, "volumeType", ignore_case_get(vol["properties"], "custom_volume_type"))
72 vim_id = vol["properties"]["location_info"]["vimid"],
73 ret = api.create_volume(vim_id, param)
74 vol_id, vol_name, return_code = ret["id"], ret["name"], ret["returnCode"]
75 retry_count, max_retry_count = 0, 300
76 while retry_count < max_retry_count:
77 vol_info = api.get_volume(vim_id, vol_id)
78 if vol_info["status"].upper() == "AVAILABLE":
79 do_notify(progress, ret)
82 retry_count = retry_count + 1
83 if return_code == RES_NEW:
84 api.delete_volume(vim_id, vol_id)
85 raise VimException("Failed to create Volume(%s): Timeout." % vol_name)
87 def create_network(network, do_notify, progress):
89 "tenant": network["properties"]["location_info"]["tenant"],
90 "networkName": network["properties"]["network_name"],
91 "shared": NET_PRIVATE,
92 "networkType": network["properties"]["network_type"],
93 "physicalNetwork": ignore_case_get(network["properties"], "physical_network")
95 set_opt_val(param, "vlanTransparent",
96 ignore_case_get(network["properties"], "vlan_transparent"), VLAN_TRANSPARENT_YES)
97 set_opt_val(param, "segmentationId", ignore_case_get(network["properties"], "segmentation_id"))
98 vim_id = network["properties"]["location_info"]["vimid"],
99 ret = api.create_network(vim_id, param)
100 do_notify(progress, ret)
102 def create_subnet(subnet, do_notify, progress):
104 "tenant": subnet["properties"]["location_info"]["tenant"],
105 "networkName": subnet["properties"]["network_name"],
106 "subnetName": subnet["properties"]["name"],
107 "cidr": ignore_case_get(subnet["properties"], "cidr"),
108 "ipVersion": ignore_case_get(subnet["properties"], "ip_version", IP_V4)
110 set_opt_val(param, "enableDhcp",
111 ignore_case_get(subnet["properties"], "dhcp_enabled"), DHCP_ENABLED)
112 set_opt_val(param, "gatewayIp", ignore_case_get(subnet["properties"], "gateway_ip"))
113 set_opt_val(param, "dnsNameservers", ignore_case_get(subnet["properties"], "dns_nameservers"))
115 set_opt_val(allocation_pool, "start", ignore_case_get(subnet["properties"], "start_ip"))
116 set_opt_val(allocation_pool, "end", ignore_case_get(subnet["properties"], "end_ip"))
118 param["allocationPools"] = [allocation_pool]
119 set_opt_val(param, "hostRoutes", ignore_case_get(subnet["properties"], "host_routes"))
120 vim_id = network["properties"]["location_info"]["vimid"],
121 ret = api.create_subnet(vim_id, param)
122 do_notify(progress, ret)
124 def create_port(port, do_notify, progress):
126 "tenant": subnet["properties"]["location_info"]["tenant"],
127 "networkName": subnet["properties"]["network_name"],
128 "subnetName": subnet["properties"]["name"],
129 "portName": subnet["properties"]["name"]
131 vim_id = subnet["properties"]["location_info"]["vimid"],
132 ret = api.create_subnet(vim_id, param)
133 do_notify(progress, ret)
135 def create_flavor(flavor, do_notify, progress):
137 "tenant": flavor["properties"]["location_info"]["tenant"],
138 "vcpu": int(flavor["nfv_compute"]["num_cpus"]),
139 "memory": int(flavor["nfv_compute"]["mem_size"].replace('MB', '').strip())
141 set_opt_val(param, "extraSpecs", ignore_case_get(flavor["nfv_compute"], "flavor_extra_specs"))
142 vim_id = subnet["properties"]["location_info"]["vimid"],
143 ret = api.create_flavor(vim_id, param)
144 do_notify(progress, ret)
146 def create_vm(vm, do_notify, progress):
148 "tenant": vm["properties"]["location_info"]["tenant"],
149 "vmName": vm["properties"]["name"],
151 "type": BOOT_FROM_VOLUME,
152 "volumeName": vm["volume_storages"][0]["volume_storage_id"]
158 set_opt_val(param, "availabilityZone",
159 ignore_case_get(vm["properties"]["location_info"], "availability_zone"))
160 for inject_data in ignore_case_get(vm["properties"], "inject_data_list"):
161 param["contextArray"].append({
162 "fileName": inject_data["file_name"],
163 "fileData": inject_data["file_data"]
165 for vol_data in vm["volume_storages"]:
166 param["contextArray"].append(vol_data["volume_storage_id"])
168 vim_id = subnet["properties"]["location_info"]["vimid"],
169 ret = api.create_vm(vim_id, param)
170 vm_id, vm_name, return_code = ret["id"], ret["name"], ret["returnCode"]
171 opt_vm_status = "Timeout"
172 retry_count, max_retry_count = 0, 100
173 while retry_count < max_retry_count:
174 vm_info = api.get_vm(vim_id, vm_id)
175 if vm_info["status"].upper() == "ACTIVE":
176 do_notify(progress, ret)
178 if vm_info["status"].upper() == "ERROR":
179 opt_vm_status = vm_info["status"]
182 retry_count = retry_count + 1
183 if return_code == RES_NEW:
184 api.delete_vm(vim_id, vm_id)
185 raise VimException("Failed to create Vm(%s): %s." % (vm_name, opt_vm_status))