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.
18 from lcm.pub.utils.values import ignore_case_get, set_opt_val
19 from lcm.pub.msapi.aai import get_flavor_info
21 from .exceptions import VimException
23 logger = logging.getLogger(__name__)
26 RES_EXIST, RES_NEW = 0, 1
28 BOOT_FROM_VOLUME, BOOT_FROM_IMAGE = 1, 2
31 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), ERR_CODE)
48 return vim_cache[vim_id][tenant_name]
51 def set_res_cache(res_cache, res_type, key, val):
52 if res_type not in res_cache:
53 res_cache[res_type] = {}
54 if key in res_cache[res_type]:
55 raise VimException("Duplicate key(%s) of %s" % (key, res_type), ERR_CODE)
56 res_cache[res_type][key] = val
59 def get_res_id(res_cache, res_type, key):
60 if res_type not in res_cache:
61 raise VimException("%s not found in cache" % res_type, ERR_CODE)
62 if key not in res_cache[res_type]:
63 raise VimException("%s(%s) not found in cache" % (res_type, key), ERR_CODE)
64 return res_cache[res_type][key]
67 def create_vim_res(data, do_notify):
68 vim_cache, res_cache = {}, {}
69 for vol in ignore_case_get(data, "volume_storages"):
70 create_volume(vim_cache, res_cache, vol, do_notify, RES_VOLUME)
71 for network in ignore_case_get(data, "vls"):
72 create_network(vim_cache, res_cache, network, do_notify, RES_NETWORK)
73 for subnet in ignore_case_get(data, "vls"):
74 create_subnet(vim_cache, res_cache, subnet, do_notify, RES_SUBNET)
75 for port in ignore_case_get(data, "cps"):
76 create_port(vim_cache, res_cache, data, port, do_notify, RES_PORT)
77 for vdu in ignore_case_get(data, "vdus"):
78 if vdu["type"] == "tosca.nodes.nfv.Vdu.Compute":
79 create_flavor(vim_cache, res_cache, data, vdu, do_notify, RES_FLAVOR)
80 for vdu in ignore_case_get(data, "vdus"):
81 if vdu["type"] == "tosca.nodes.nfv.Vdu.Compute":
82 create_vm(vim_cache, res_cache, data, vdu, do_notify, RES_VM)
85 def delete_vim_res(data, do_notify):
86 res_types = [RES_VM, RES_FLAVOR, RES_PORT, RES_SUBNET, RES_NETWORK, RES_VOLUME]
87 res_del_funs = [api.delete_vm, api.delete_flavor, api.delete_port,
88 api.delete_subnet, api.delete_network, api.delete_volume]
89 for res_type, res_del_fun in zip(res_types, res_del_funs):
90 for res in ignore_case_get(data, res_type):
92 if NOT_PREDEFINED == res["is_predefined"]:
93 res_del_fun(res["vim_id"], res["tenant_id"], res["res_id"])
94 except VimException as e:
95 logger.error("Failed to delete %s(%s)", res_type, res["res_id"])
96 logger.error("%s:%s", e.http_code, e.message)
97 do_notify(res_type, res["res_id"])
100 def create_volume(vim_cache, res_cache, vol, do_notify, res_type):
101 location_info = vol["properties"]["location_info"]
103 "name": vol["properties"]["volume_name"],
104 "volumeSize": int(ignore_case_get(vol["properties"], "size", "0").replace('GB', '').strip())
106 set_opt_val(param, "imageName", ignore_case_get(vol, "image_file"))
107 set_opt_val(param, "volumeType", ignore_case_get(vol["properties"], "custom_volume_type"))
108 set_opt_val(param, "availabilityZone", ignore_case_get(location_info, "availability_zone"))
109 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
110 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
111 ret = api.create_volume(vim_id, tenant_id, param)
112 ret["nodeId"] = vol["volume_storage_id"]
113 do_notify(res_type, ret)
114 vol_id, vol_name = ret["id"], ret["name"]
115 set_res_cache(res_cache, res_type, vol["volume_storage_id"], vol_id)
116 retry_count, max_retry_count = 0, 300
117 while retry_count < max_retry_count:
118 vol_info = api.get_volume(vim_id, tenant_id, vol_id)
119 if vol_info["status"].upper() == "AVAILABLE":
120 logger.debug("Volume(%s) is available", vol_id)
123 retry_count = retry_count + 1
124 raise VimException("Failed to create Volume(%s): Timeout." % vol_name, ERR_CODE)
127 def create_network(vim_cache, res_cache, network, do_notify, res_type):
128 location_info = network["properties"]["location_info"]
130 "name": network["properties"]["network_name"],
132 "networkType": network["properties"]["network_type"],
133 "physicalNetwork": ignore_case_get(network["properties"], "physical_network")
135 set_opt_val(param, "vlanTransparent", ignore_case_get(network["properties"], "vlan_transparent"))
136 set_opt_val(param, "segmentationId", int(ignore_case_get(network["properties"], "segmentation_id", "0")))
137 set_opt_val(param, "routerExternal", ignore_case_get(network, "route_external"))
138 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
139 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
140 ret = api.create_network(vim_id, tenant_id, param)
141 ret["nodeId"] = network["vl_id"]
142 do_notify(res_type, ret)
143 set_res_cache(res_cache, res_type, network["vl_id"], ret["id"])
146 def create_subnet(vim_cache, res_cache, subnet, do_notify, res_type):
147 location_info = subnet["properties"]["location_info"]
148 network_id = get_res_id(res_cache, RES_NETWORK, subnet["vl_id"])
150 "networkId": network_id,
151 "name": subnet["properties"]["name"],
152 "cidr": ignore_case_get(subnet["properties"], "cidr"),
153 "ipVersion": ignore_case_get(subnet["properties"], "ip_version", IP_V4)
155 set_opt_val(param, "enableDhcp", ignore_case_get(subnet["properties"], "dhcp_enabled"))
156 set_opt_val(param, "gatewayIp", ignore_case_get(subnet["properties"], "gateway_ip"))
157 set_opt_val(param, "dnsNameservers", ignore_case_get(subnet["properties"], "dns_nameservers"))
159 set_opt_val(allocation_pool, "start", ignore_case_get(subnet["properties"], "start_ip"))
160 set_opt_val(allocation_pool, "end", ignore_case_get(subnet["properties"], "end_ip"))
162 param["allocationPools"] = [allocation_pool]
163 set_opt_val(param, "hostRoutes", ignore_case_get(subnet["properties"], "host_routes"))
164 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
165 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
166 ret = api.create_subnet(vim_id, tenant_id, param)
167 do_notify(res_type, ret)
168 set_res_cache(res_cache, res_type, subnet["vl_id"], ret["id"])
171 def create_port(vim_cache, res_cache, data, port, do_notify, res_type):
173 port_ref_vdu_id = ignore_case_get(port, "vdu_id")
174 for vdu in ignore_case_get(data, "vdus"):
175 if vdu["vdu_id"] == port_ref_vdu_id:
176 location_info = vdu["properties"]["location_info"]
177 if port["cp_id"] not in vdu["cps"]:
178 vdu["cps"].append(port["cp_id"])
180 if not location_info:
181 err_msg = "vdu_id(%s) for cp(%s) is not defined"
182 raise VimException(err_msg % (port_ref_vdu_id, port["cp_id"]), ERR_CODE)
183 network_id = ignore_case_get(port, "networkId")
184 subnet_id = ignore_case_get(port, "subnetId")
186 network_id = get_res_id(res_cache, RES_NETWORK, port["vl_id"])
187 subnet_id = get_res_id(res_cache, RES_SUBNET, port["vl_id"])
189 "networkId": network_id,
190 "name": port["properties"].get("name", "undefined")
192 set_opt_val(param, "subnetId", subnet_id)
193 set_opt_val(param, "macAddress", ignore_case_get(port["properties"], "mac_address"))
194 set_opt_val(param, "ip", ignore_case_get(port["properties"], "ip_address"))
195 set_opt_val(param, "vnicType", ignore_case_get(port["properties"], "vnic_type"))
196 set_opt_val(param, "securityGroups", "") # TODO
197 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
198 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
199 ret = api.create_port(vim_id, tenant_id, param)
200 ret["nodeId"] = port["cp_id"]
201 do_notify(res_type, ret)
202 set_res_cache(res_cache, res_type, port["cp_id"], ret["id"])
205 def search_flavor_aai(vim_id, memory_page_size):
206 aai_flavors = get_flavor_info(vim_id)
209 logger.debug("aai_flavors:%s" % aai_flavors)
210 aai_flavor = aai_flavors[0]["flavors"]["flavor"]
211 for one_aai_flavor in aai_flavor:
212 hpa_capabilities = one_aai_flavor["hpa-capabilities"]
213 for one_hpa_capa in hpa_capabilities:
214 hpa_feature_attr = one_hpa_capa["hpa-feature-attributes"]
215 for one_hpa_attr in hpa_feature_attr:
216 hpa_key = one_hpa_attr["hpa-attribute-key"]
217 hpa_value = one_hpa_attr["hpa-attribute-value"]["value"]
218 if hpa_key == "memoryPageSize" and int(hpa_value) == memory_page_size:
219 return one_aai_flavor
222 def create_flavor(vim_cache, res_cache, data, flavor, do_notify, res_type):
223 location_info = flavor["properties"]["location_info"]
224 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
225 virtual_compute = flavor["virtual_compute"]
227 "name": "Flavor_%s" % flavor["vdu_id"],
228 "vcpu": int(virtual_compute["virtual_cpu"]["num_virtual_cpu"]),
229 "memory": int(virtual_compute["virtual_memory"]["virtual_mem_size"].replace('MB', '').strip()),
233 # just do memory huge page
234 flavor_extra_specs = ""
235 vdu_memory_requirements = virtual_compute["virtual_memory"]["vdu_memory_requirements"]
236 if "memoryPageSize" in vdu_memory_requirements:
237 memory_page_size = int(vdu_memory_requirements["memoryPageSize"].replace('MB', '').strip())
238 flavor_extra_specs = ("hw:mem_page_size=%sMB" % memory_page_size)
239 logger.debug("flavor_extra_specs:%s" % flavor_extra_specs)
242 aai_flavor = search_flavor_aai(vim_id, memory_page_size)
247 do_notify(res_type, ret)
248 set_res_cache(res_cache, res_type, flavor["vdu_id"], ret["flavor-id"])
251 disk_type = virtual_compute["virtual_storage"]["type_of_storage"]
252 disk_size = int(virtual_compute["virtual_storage"]["size_of_storage"].replace('GB', '').strip())
253 if disk_type == "root":
254 param["disk"] = disk_size
255 elif disk_type == "ephemeral":
256 param["ephemeral"] = disk_size
257 elif disk_type == "swap":
258 param["swap"] = disk_size
260 for es in flavor_extra_specs:
261 extra_specs.append({"keyName": es, "value": flavor_extra_specs[es]})
263 set_opt_val(param, "extraSpecs", extra_specs)
264 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
265 logger.debug("param:%s" % param)
266 ret = api.create_flavor(vim_id, tenant_id, param)
267 do_notify(res_type, ret)
268 set_res_cache(res_cache, res_type, flavor["vdu_id"], ret["id"])
271 def create_vm(vim_cache, res_cache, data, vm, do_notify, res_type):
272 location_info = vm["properties"]["location_info"]
273 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
274 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
276 "name": vm["properties"].get("name", "undefined"),
277 "flavorId": get_res_id(res_cache, RES_FLAVOR, vm["vdu_id"]),
284 if "image_file" in vm and vm["image_file"]:
285 param["boot"]["type"] = BOOT_FROM_IMAGE
287 for img in ignore_case_get(data, "image_files"):
288 if vm["image_file"] == img["image_file_id"]:
289 img_name = img["properties"]["name"]
292 raise VimException("Undefined image(%s)" % vm["image_file"], ERR_CODE)
293 images = api.list_image(vim_id, tenant_id)
294 for image in images["images"]:
295 if img_name == image["name"]:
296 param["boot"]["imageId"] = image["id"]
298 if "imageId" not in param["boot"]:
299 raise VimException("Image(%s) not found in Vim(%s)" % (img_name, vim_id), ERR_CODE)
300 elif vm["volume_storages"]:
301 param["boot"]["type"] = BOOT_FROM_VOLUME
302 vol_id = vm["volume_storages"][0]["volume_storage_id"]
303 param["boot"]["volumeId"] = get_res_id(res_cache, RES_VOLUME, vol_id)
305 raise VimException("No image and volume defined", ERR_CODE)
307 for cp_id in ignore_case_get(vm, "cps"):
308 param["nicArray"].append({
309 "portId": get_res_id(res_cache, RES_PORT, cp_id)
311 for inject_data in ignore_case_get(vm["properties"], "inject_data_list"):
312 param["contextArray"].append({
313 "fileName": inject_data["file_name"],
314 "fileData": inject_data["file_data"]
316 for vol_data in ignore_case_get(vm, "volume_storages"):
317 vol_id = vol_data["volume_storage_id"]
318 param["volumeArray"].append({
319 "volumeId": get_res_id(res_cache, RES_VOLUME, vol_id)
322 set_opt_val(param, "availabilityZone", ignore_case_get(location_info, "availability_zone"))
323 set_opt_val(param, "userdata", "") # TODO Configuration information or scripts to use upon launch
324 set_opt_val(param, "metadata", "") # TODO [{"keyName": "foo", "value": "foo value"}]
325 set_opt_val(param, "securityGroups", "") # TODO List of names of security group
326 set_opt_val(param, "serverGroup", "") # TODO the ServerGroup for anti-affinity and affinity
328 ret = api.create_vm(vim_id, tenant_id, param)
329 do_notify(res_type, ret)
331 if ignore_case_get(ret, "name"):
332 vm_name = vm["properties"].get("name", "undefined")
333 logger.debug("vm_name:%s" % vm_name)
334 opt_vm_status = "Timeout"
335 retry_count, max_retry_count = 0, 100
336 while retry_count < max_retry_count:
337 vm_info = api.get_vm(vim_id, tenant_id, vm_id)
338 if vm_info["status"].upper() == "ACTIVE":
339 logger.debug("Vm(%s) is active", vim_id)
341 if vm_info["status"].upper() == "ERROR":
342 opt_vm_status = vm_info["status"]
345 retry_count = retry_count + 1
346 raise VimException("Failed to create Vm(%s): %s." % (vm_name, opt_vm_status), ERR_CODE)