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
27 NET_PRIVATE, NET_SHSRED = 0, 1
28 VLAN_TRANSPARENT_NO, VLAN_TRANSPARENT_YES = 0, 1
30 DHCP_DISABLED, DHCP_ENABLED = 0, 1
31 OPT_CREATE_VOLUME = 20
32 OPT_CREATE_NETWORK = 30
33 OPT_CREATE_SUBNET = 40
35 OPT_CREATE_FLAVOR = 60
40 def get_tenant_id(vim_cache, vim_id, tenant_name):
41 if vim_id not in vim_cache:
42 tenants = api.list_tenant(vim_id)
43 vim_cache[vim_id] = {}
44 for tenant in tenants["tenants"]:
45 id, name = tenant["id"], tenant["name"]
46 vim_cache[vim_id][name] = id
47 if tenant_name not in vim_cache[vim_id]:
48 raise VimException("Tenant(%s) not found in vim(%s)" % (tenant_name, vim_id), "500")
49 return vim_cache[vim_id][tenant_name]
51 def create_vim_res(data, do_notify):
53 for vol in ignore_case_get(data, "volume_storages"):
54 create_volume(vim_cache, vol, do_notify, OPT_CREATE_VOLUME)
55 for network in ignore_case_get(data, "vls"):
56 create_network(vim_cache, network, do_notify, OPT_CREATE_NETWORK)
57 for subnet in ignore_case_get(data, "vls"):
58 create_subnet(vim_cache, subnet, do_notify, OPT_CREATE_SUBNET)
59 for port in ignore_case_get(data, "cps"):
60 create_port(vim_cache, port, do_notify, OPT_CREATE_PORT)
61 for flavor in ignore_case_get(data, "vdus"):
62 create_flavor(vim_cache, flavor, do_notify, OPT_CREATE_FLAVOR)
63 for vm in ignore_case_get(data, "vdus"):
64 create_vm(vim_cache, vm, do_notify, OPT_CREATE_VM)
66 def delete_vim_res(data, do_notify):
67 res_types = ["vm", "flavor", "port", "subnet", "network", "volume"]
68 res_del_funs = [api.delete_vm, api.delete_flavor, api.delete_port,
69 api.delete_subnet, api.delete_network, api.delete_volume]
70 for res_type, res_del_fun in zip(res_types, res_del_funs):
71 for res in ignore_case_get(data, res_type):
73 res_del_fun(res["vim_id"], res["tenant_id"], res["res_id"])
74 except VimException as e:
75 logger.error("Failed to delete %s(%s)", res_type, res["res_id"])
76 logger.error("%s:%s", e.http_code, e.message)
77 do_notify(res_type, res["res_id"])
79 def create_volume(vim_cache, vol, do_notify, progress):
81 "name": vol["properties"]["volume_name"],
82 "volumeSize": int(ignore_case_get(vol["properties"], "size", "0"))
84 location_info = vol["properties"]["location_info"]
85 set_opt_val(param, "imageName", ignore_case_get(vol, "image_file"))
86 set_opt_val(param, "volumeType", ignore_case_get(vol["properties"], "custom_volume_type"))
87 set_opt_val(param, "availabilityZone", ignore_case_get(location_info, "availability_zone"))
88 vim_id = location_info["vimid"]
89 tenant_name = location_info["tenant"]
90 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
91 ret = api.create_volume(vim_id, tenant_id, param)
92 do_notify(progress, ret)
93 vol_id, vol_name, return_code = ret["id"], ret["name"], ret["returnCode"]
94 retry_count, max_retry_count = 0, 300
95 while retry_count < max_retry_count:
96 vol_info = api.get_volume(vim_id, tenant_id, vol_id)
97 if vol_info["status"].upper() == "AVAILABLE":
98 logger.debug("Volume(%s) is available", vol_id)
101 retry_count = retry_count + 1
102 raise VimException("Failed to create Volume(%s): Timeout." % vol_name, "500")
104 def create_network(vim_cache, network, do_notify, progress):
106 "tenant": network["properties"]["location_info"]["tenant"],
107 "networkName": network["properties"]["network_name"],
108 "shared": NET_PRIVATE,
109 "networkType": network["properties"]["network_type"],
110 "physicalNetwork": ignore_case_get(network["properties"], "physical_network")
112 set_opt_val(param, "vlanTransparent",
113 ignore_case_get(network["properties"], "vlan_transparent"), VLAN_TRANSPARENT_YES)
114 set_opt_val(param, "segmentationId", ignore_case_get(network["properties"], "segmentation_id"))
115 vim_id = network["properties"]["location_info"]["vimid"]
116 ret = api.create_network(vim_id, param)
117 do_notify(progress, ret)
119 def create_subnet(vim_cache, subnet, do_notify, progress):
121 "tenant": subnet["properties"]["location_info"]["tenant"],
122 "networkName": subnet["properties"]["network_name"],
123 "subnetName": subnet["properties"]["name"],
124 "cidr": ignore_case_get(subnet["properties"], "cidr"),
125 "ipVersion": ignore_case_get(subnet["properties"], "ip_version", IP_V4)
127 set_opt_val(param, "enableDhcp",
128 ignore_case_get(subnet["properties"], "dhcp_enabled"), 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, 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, 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, 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")