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.
20 from lcm.pub.utils.values import ignore_case_get, set_opt_val
22 from .exceptions import VimException
24 logger = logging.getLogger(__name__)
26 RES_EXIST, RES_NEW = 0, 1
28 DHCP_DISABLED, DHCP_ENABLED = 0, 1
30 RES_NETWORK = "network"
39 def get_tenant_id(vim_cache, vim_id, tenant_name):
40 if vim_id not in vim_cache:
41 tenants = api.list_tenant(vim_id)
42 vim_cache[vim_id] = {}
43 for tenant in tenants["tenants"]:
44 id, name = tenant["id"], tenant["name"]
45 vim_cache[vim_id][name] = id
46 if tenant_name not in vim_cache[vim_id]:
47 raise VimException("Tenant(%s) not found in vim(%s)" % (tenant_name, vim_id), "500")
48 return vim_cache[vim_id][tenant_name]
50 def create_vim_res(data, do_notify):
51 vim_cache, res_cache = {}, {}
52 for vol in ignore_case_get(data, "volume_storages"):
53 create_volume(vim_cache, res_cache, vol, do_notify, RES_VOLUME)
54 for network in ignore_case_get(data, "vls"):
55 create_network(vim_cache, res_cache, network, do_notify, RES_NETWORK)
56 for subnet in ignore_case_get(data, "vls"):
57 create_subnet(vim_cache, res_cache, subnet, do_notify, RES_SUBNET)
58 for port in ignore_case_get(data, "cps"):
59 create_port(vim_cache, res_cache, port, do_notify, RES_PORT)
60 for flavor in ignore_case_get(data, "vdus"):
61 create_flavor(vim_cache, res_cache, flavor, do_notify, RES_FLAVOR)
62 for vm in ignore_case_get(data, "vdus"):
63 create_vm(vim_cache, res_cache, vm, do_notify, RES_VM)
65 def delete_vim_res(data, do_notify):
66 res_types = [RES_VM, RES_FLAVOR, RES_PORT, RES_SUBNET, RES_NETWORK, RES_VOLUME]
67 res_del_funs = [api.delete_vm, api.delete_flavor, api.delete_port,
68 api.delete_subnet, api.delete_network, api.delete_volume]
69 for res_type, res_del_fun in zip(res_types, res_del_funs):
70 for res in ignore_case_get(data, res_type):
72 res_del_fun(res["vim_id"], res["tenant_id"], res["res_id"])
73 except VimException as e:
74 logger.error("Failed to delete %s(%s)", res_type, res["res_id"])
75 logger.error("%s:%s", e.http_code, e.message)
76 do_notify(res_type, res["res_id"])
78 def create_volume(vim_cache, res_cache, vol, do_notify, progress):
80 "name": vol["properties"]["volume_name"],
81 "volumeSize": int(ignore_case_get(vol["properties"], "size", "0"))
83 location_info = vol["properties"]["location_info"]
84 set_opt_val(param, "imageName", ignore_case_get(vol, "image_file"))
85 set_opt_val(param, "volumeType", ignore_case_get(vol["properties"], "custom_volume_type"))
86 set_opt_val(param, "availabilityZone", ignore_case_get(location_info, "availability_zone"))
87 vim_id = location_info["vimid"]
88 tenant_name = location_info["tenant"]
89 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
90 ret = api.create_volume(vim_id, tenant_id, param)
91 do_notify(progress, ret)
92 vol_id, vol_name, return_code = ret["id"], ret["name"], ret["returnCode"]
93 retry_count, max_retry_count = 0, 300
94 while retry_count < max_retry_count:
95 vol_info = api.get_volume(vim_id, tenant_id, vol_id)
96 if vol_info["status"].upper() == "AVAILABLE":
97 logger.debug("Volume(%s) is available", vol_id)
100 retry_count = retry_count + 1
101 raise VimException("Failed to create Volume(%s): Timeout." % vol_name, "500")
103 def create_network(vim_cache, res_cache, network, do_notify, progress):
105 "name": network["properties"]["network_name"],
107 "networkType": network["properties"]["network_type"],
108 "physicalNetwork": ignore_case_get(network["properties"], "physical_network")
110 location_info = network["properties"]["location_info"]
111 set_opt_val(param, "vlanTransparent", ignore_case_get(network["properties"], "vlan_transparent"))
112 set_opt_val(param, "segmentationId", int(ignore_case_get(network["properties"], "segmentation_id", "0")))
113 set_opt_val(param, "routerExternal", ignore_case_get(network, "route_external"))
114 vim_id = location_info["vimid"]
115 tenant_name = location_info["tenant"]
116 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
117 ret = api.create_network(vim_id, tenant_id, param)
118 do_notify(progress, ret)
120 def create_subnet(vim_cache, res_cache, subnet, do_notify, progress):
122 "tenant": subnet["properties"]["location_info"]["tenant"],
123 "networkName": subnet["properties"]["network_name"],
124 "subnetName": subnet["properties"]["name"],
125 "cidr": ignore_case_get(subnet["properties"], "cidr"),
126 "ipVersion": ignore_case_get(subnet["properties"], "ip_version", IP_V4)
128 set_opt_val(param, "enableDhcp", ignore_case_get(subnet["properties"], "dhcp_enabled"))
129 set_opt_val(param, "gatewayIp", ignore_case_get(subnet["properties"], "gateway_ip"))
130 set_opt_val(param, "dnsNameservers", ignore_case_get(subnet["properties"], "dns_nameservers"))
132 set_opt_val(allocation_pool, "start", ignore_case_get(subnet["properties"], "start_ip"))
133 set_opt_val(allocation_pool, "end", ignore_case_get(subnet["properties"], "end_ip"))
135 param["allocationPools"] = [allocation_pool]
136 set_opt_val(param, "hostRoutes", ignore_case_get(subnet["properties"], "host_routes"))
137 vim_id = subnet["properties"]["location_info"]["vimid"]
138 ret = api.create_subnet(vim_id, param)
139 do_notify(progress, ret)
141 def create_port(vim_cache, res_cache, port, do_notify, progress):
143 "tenant": port["properties"]["location_info"]["tenant"],
144 "networkName": port["properties"]["network_name"],
145 "subnetName": port["properties"]["name"],
146 "portName": port["properties"]["name"]
148 vim_id = port["properties"]["location_info"]["vimid"]
149 ret = api.create_subnet(vim_id, param)
150 do_notify(progress, ret)
152 def create_flavor(vim_cache, res_cache, flavor, do_notify, progress):
154 "tenant": flavor["properties"]["location_info"]["tenant"],
155 "vcpu": int(flavor["nfv_compute"]["num_cpus"]),
156 "memory": int(flavor["nfv_compute"]["mem_size"].replace('MB', '').strip())
158 set_opt_val(param, "extraSpecs", ignore_case_get(flavor["nfv_compute"], "flavor_extra_specs"))
159 vim_id = flavor["properties"]["location_info"]["vimid"]
160 ret = api.create_flavor(vim_id, param)
161 do_notify(progress, ret)
163 def create_vm(vim_cache, res_cache, vm, do_notify, progress):
165 "tenant": vm["properties"]["location_info"]["tenant"],
166 "vmName": vm["properties"]["name"],
168 "type": BOOT_FROM_VOLUME,
169 "volumeName": vm["volume_storages"][0]["volume_storage_id"]
175 set_opt_val(param, "availabilityZone",
176 ignore_case_get(vm["properties"]["location_info"], "availability_zone"))
177 for inject_data in ignore_case_get(vm["properties"], "inject_data_list"):
178 param["contextArray"].append({
179 "fileName": inject_data["file_name"],
180 "fileData": inject_data["file_data"]
182 for vol_data in vm["volume_storages"]:
183 param["contextArray"].append(vol_data["volume_storage_id"])
185 vim_id = vm["properties"]["location_info"]["vimid"]
186 ret = api.create_vm(vim_id, param)
187 do_notify(progress, ret)
188 vm_id, vm_name, return_code = ret["id"], ret["name"], ret["returnCode"]
189 opt_vm_status = "Timeout"
190 retry_count, max_retry_count = 0, 100
191 while retry_count < max_retry_count:
192 vm_info = api.get_vm(vim_id, vm_id)
193 if vm_info["status"].upper() == "ACTIVE":
194 logger.debug("Vm(%s) is active", vim_id)
196 if vm_info["status"].upper() == "ERROR":
197 opt_vm_status = vm_info["status"]
200 retry_count = retry_count + 1
201 raise VimException("Failed to create Vm(%s): %s." % (vm_name, opt_vm_status), "500")