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.
19 from lcm.pub.utils.values import ignore_case_get, set_opt_val
20 from lcm.pub.msapi.aai import get_flavor_info
22 from .exceptions import VimException
24 logger = logging.getLogger(__name__)
27 RES_EXIST, RES_NEW = 0, 1
29 BOOT_FROM_VOLUME, BOOT_FROM_IMAGE = 1, 2
32 RES_NETWORK = "network"
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), ERR_CODE)
49 return vim_cache[vim_id][tenant_name]
52 def set_res_cache(res_cache, res_type, key, val):
53 if res_type not in res_cache:
54 res_cache[res_type] = {}
55 if key in res_cache[res_type]:
56 raise VimException("Duplicate key(%s) of %s" % (key, res_type), ERR_CODE)
57 res_cache[res_type][key] = val
60 def get_res_id(res_cache, res_type, key):
61 if res_type not in res_cache:
62 raise VimException("%s not found in cache" % res_type, ERR_CODE)
63 if key not in res_cache[res_type]:
64 raise VimException("%s(%s) not found in cache" % (res_type, key), ERR_CODE)
65 return res_cache[res_type][key]
68 def create_vim_res(data, do_notify):
69 vim_cache, res_cache = {}, {}
70 for vol in ignore_case_get(data, "volume_storages"):
71 create_volume(vim_cache, res_cache, vol, do_notify, RES_VOLUME)
72 for network in ignore_case_get(data, "vls"):
73 create_network(vim_cache, res_cache, network, do_notify, RES_NETWORK)
74 for subnet in ignore_case_get(data, "vls"):
75 create_subnet(vim_cache, res_cache, subnet, do_notify, RES_SUBNET)
76 for port in ignore_case_get(data, "cps"):
77 create_port(vim_cache, res_cache, data, port, do_notify, RES_PORT)
78 for vdu in ignore_case_get(data, "vdus"):
79 if vdu["type"] == "tosca.nodes.nfv.Vdu.Compute":
80 create_flavor(vim_cache, res_cache, data, vdu, do_notify, RES_FLAVOR)
81 for vdu in ignore_case_get(data, "vdus"):
82 if vdu["type"] == "tosca.nodes.nfv.Vdu.Compute":
83 create_vm(vim_cache, res_cache, data, vdu, do_notify, RES_VM)
86 def delete_vim_res(data, do_notify):
87 res_types = [RES_VM, RES_FLAVOR, RES_PORT, RES_SUBNET, RES_NETWORK, RES_VOLUME]
88 res_del_funs = [api.delete_vm, api.delete_flavor, api.delete_port,
89 api.delete_subnet, api.delete_network, api.delete_volume]
90 for res_type, res_del_fun in zip(res_types, res_del_funs):
91 for res in ignore_case_get(data, res_type):
93 if NOT_PREDEFINED == res["is_predefined"]:
94 res_del_fun(res["vim_id"], res["tenant_id"], res["res_id"])
95 except VimException as e:
96 logger.error("Failed to delete %s(%s)", res_type, res["res_id"])
97 logger.error("%s:%s", e.http_code, e.message)
98 do_notify(res_type, res["res_id"])
101 def create_volume(vim_cache, res_cache, vol, do_notify, res_type):
102 location_info = vol["properties"]["location_info"]
104 "name": vol["properties"]["volume_name"],
105 "volumeSize": int(ignore_case_get(vol["properties"], "size", "0").replace('GB', '').strip())
107 set_opt_val(param, "imageName", ignore_case_get(vol, "image_file"))
108 set_opt_val(param, "volumeType", ignore_case_get(vol["properties"], "custom_volume_type"))
109 set_opt_val(param, "availabilityZone", ignore_case_get(location_info, "availability_zone"))
110 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
111 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
112 ret = api.create_volume(vim_id, tenant_id, param)
113 ret["nodeId"] = vol["volume_storage_id"]
114 do_notify(res_type, ret)
115 vol_id, vol_name = ret["id"], ret["name"]
116 set_res_cache(res_cache, res_type, vol["volume_storage_id"], vol_id)
117 retry_count, max_retry_count = 0, 300
118 while retry_count < max_retry_count:
119 vol_info = api.get_volume(vim_id, tenant_id, vol_id)
120 if vol_info["status"].upper() == "AVAILABLE":
121 logger.debug("Volume(%s) is available", vol_id)
124 retry_count = retry_count + 1
125 raise VimException("Failed to create Volume(%s): Timeout." % vol_name, ERR_CODE)
128 def create_network(vim_cache, res_cache, network, do_notify, res_type):
129 location_info = network["properties"]["location_info"]
130 vl_profile = network["properties"]["vl_profile"]
132 "name": vl_profile["networkName"],
134 "networkType": vl_profile["networkType"],
135 "physicalNetwork": ignore_case_get(vl_profile, "physicalNetwork")
137 set_opt_val(param, "vlanTransparent", ignore_case_get(vl_profile, "vlanTransparent"))
138 set_opt_val(param, "segmentationId", int(ignore_case_get(vl_profile, "segmentationId", "0")))
139 set_opt_val(param, "routerExternal", ignore_case_get(network, "route_external"))
140 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
141 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
142 ret = api.create_network(vim_id, tenant_id, param)
143 ret["nodeId"] = network["vl_id"]
144 do_notify(res_type, ret)
145 set_res_cache(res_cache, res_type, network["vl_id"], ret["id"])
148 def create_subnet(vim_cache, res_cache, subnet, do_notify, res_type):
149 location_info = subnet["properties"]["location_info"]
150 network_id = get_res_id(res_cache, RES_NETWORK, subnet["vl_id"])
151 vl_profile = subnet["properties"]["vl_profile"]
152 layer_protocol = ignore_case_get(subnet["properties"]["connectivity_type"], "layer_protocol")
154 "networkId": network_id,
155 "name": vl_profile["networkName"] + "_subnet",
156 "cidr": ignore_case_get(vl_profile, "cidr"),
157 "ipVersion": IP_V4 if(layer_protocol == 'ipv4') else (IP_V6 if(layer_protocol == 'ipv6') else None)
159 set_opt_val(param, "enableDhcp", ignore_case_get(vl_profile, "dhcpEnabled"))
160 set_opt_val(param, "gatewayIp", ignore_case_get(vl_profile, "gatewayIp"))
161 set_opt_val(param, "dnsNameservers", ignore_case_get(subnet["properties"], "dns_nameservers"))
163 set_opt_val(allocation_pool, "start", ignore_case_get(vl_profile, "startIp"))
164 set_opt_val(allocation_pool, "end", ignore_case_get(vl_profile, "endIp"))
166 param["allocationPools"] = [allocation_pool]
167 set_opt_val(param, "hostRoutes", ignore_case_get(subnet["properties"], "host_routes"))
168 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
169 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
170 ret = api.create_subnet(vim_id, tenant_id, param)
171 do_notify(res_type, ret)
172 set_res_cache(res_cache, res_type, subnet["vl_id"], ret["id"])
175 def create_port(vim_cache, res_cache, data, port, do_notify, res_type):
177 port_ref_vdu_id = ignore_case_get(port, "vdu_id")
178 for vdu in ignore_case_get(data, "vdus"):
179 if vdu["vdu_id"] == port_ref_vdu_id:
180 location_info = vdu["properties"]["location_info"]
181 if port["cp_id"] not in vdu["cps"]:
182 vdu["cps"].append(port["cp_id"])
184 if not location_info:
185 err_msg = "vdu_id(%s) for cp(%s) is not defined."
186 raise VimException(err_msg % (port_ref_vdu_id, port["cp_id"]), ERR_CODE)
187 network_id = ignore_case_get(port, "networkId")
188 subnet_id = ignore_case_get(port, "subnetId")
190 network_id = get_res_id(res_cache, RES_NETWORK, port["vl_id"])
191 subnet_id = get_res_id(res_cache, RES_SUBNET, port["vl_id"])
193 "networkId": network_id,
194 "name": port["properties"].get("name", "undefined")
196 set_opt_val(param, "subnetId", subnet_id)
197 set_opt_val(param, "macAddress", ignore_case_get(port["properties"], "mac_address"))
199 for one_protocol_data in port["properties"]["protocol_data"]:
200 l3_address_data = one_protocol_data["address_data"]["l3_address_data"] # l3 is not 13
201 fixed_ip_address = ignore_case_get(l3_address_data, "fixed_ip_address")
202 ip_address.extend(fixed_ip_address)
203 set_opt_val(param, "ip", ",".join(ip_address))
204 set_opt_val(param, "vnicType", ignore_case_get(port["properties"], "vnic_type"))
205 set_opt_val(param, "securityGroups", "") # TODO
206 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
207 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
208 ret = api.create_port(vim_id, tenant_id, param)
209 ret["nodeId"] = port["cp_id"]
210 do_notify(res_type, ret)
211 set_res_cache(res_cache, res_type, port["cp_id"], ret["id"])
214 def parse_unit(val, base_unit):
215 recognized_units = ["B", "kB", "KiB", "MB", "MiB", "GB", "GiB", "TB", "TiB"]
216 units_rate = [1, 1000, 1024, 1000000, 1048576, 1000000000, 1073741824, 1000000000000, 1099511627776]
217 unit_rate_map = {unit.upper(): rate for unit, rate in zip(recognized_units, units_rate)}
218 num_unit = val.strip().split(" ")
219 if len(num_unit) != 2:
221 num, unit = num_unit[0], num_unit[1]
222 return int(num) * unit_rate_map[unit.upper()] / unit_rate_map[base_unit.upper()]
225 def search_flavor_aai(vim_id, memory_page_size, memory_page_unit):
226 aai_flavors = get_flavor_info(vim_id)
229 logger.debug("aai_flavors:%s" % aai_flavors)
230 aai_flavor = aai_flavors["flavor"]
231 for one_aai_flavor in aai_flavor:
232 if one_aai_flavor["flavor-name"].find("onap.") == -1:
234 hpa_capabilities = one_aai_flavor["hpa-capabilities"]["hpa-capability"]
235 logger.debug("hpa_capabilities=%s", hpa_capabilities)
236 for one_hpa_capa in hpa_capabilities:
237 logger.debug("one_hpa_capa=%s", one_hpa_capa)
238 hpa_feature_attr = one_hpa_capa["hpa-feature-attributes"]
239 for one_hpa_attr in hpa_feature_attr:
240 hpa_key = one_hpa_attr["hpa-attribute-key"]
241 hpa_attr_value = ast.literal_eval(one_hpa_attr["hpa-attribute-value"])
242 mem_size = ignore_case_get(hpa_attr_value, 'value')
243 mem_unit = ignore_case_get(hpa_attr_value, 'unit')
244 value = mem_size + " " + mem_unit
245 hpa_mem_size = parse_unit(value, memory_page_unit)
246 if hpa_key == "memoryPageSize" and hpa_mem_size == memory_page_size:
247 return one_aai_flavor
250 def create_flavor(vim_cache, res_cache, data, flavor, do_notify, res_type):
251 location_info = flavor["properties"]["location_info"]
252 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
253 virtual_compute = flavor["virtual_compute"]
254 virtual_storage = flavor["virtual_storage"]
255 virtual_cpu = ignore_case_get(virtual_compute, "virtual_cpu")
256 virtual_memory = ignore_case_get(virtual_compute, "virtual_memory")
258 "name": "Flavor_%s" % flavor["vdu_id"],
259 "vcpu": int(ignore_case_get(virtual_cpu, "num_virtual_cpu")),
260 "memory": int(ignore_case_get(virtual_memory, "virtual_mem_size").replace('MB', '').strip()),
264 # just do memory huge page
265 flavor_extra_specs = ""
266 vdu_memory_requirements = ignore_case_get(virtual_memory, "vdu_memory_requirements")
267 if "memoryPageSize" in vdu_memory_requirements:
268 memory_page_size = int(vdu_memory_requirements["memoryPageSize"].replace('MB', '').strip())
269 flavor_extra_specs = {"hw": memory_page_size, } # TODO
270 logger.debug("flavor_extra_specs:%s" % flavor_extra_specs)
272 # FIXME: search aai flavor
273 aai_flavor = search_flavor_aai(vim_id, memory_page_size, "MB")
278 do_notify(res_type, ret)
279 set_res_cache(res_cache, res_type, flavor["vdu_id"], ret["flavor-id"])
282 disk_type = ignore_case_get(virtual_storage, "type_of_storage")
283 disk_size = int(ignore_case_get(virtual_storage, "size_of_storage").replace('GB', '').strip())
284 if disk_type == "root":
285 param["disk"] = disk_size
286 elif disk_type == "ephemeral":
287 param["ephemeral"] = disk_size
288 elif disk_type == "swap":
289 param["swap"] = disk_size
291 for es in flavor_extra_specs:
292 extra_specs.append({"keyName": es, "value": flavor_extra_specs[es]})
294 set_opt_val(param, "extraSpecs", extra_specs)
295 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
296 logger.debug("param:%s" % param)
297 ret = api.create_flavor(vim_id, tenant_id, param)
298 do_notify(res_type, ret)
299 set_res_cache(res_cache, res_type, flavor["vdu_id"], ret["id"])
302 def create_vm(vim_cache, res_cache, data, vm, do_notify, res_type):
303 location_info = vm["properties"]["location_info"]
304 vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
305 tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
307 "name": vm["properties"].get("name", "undefined"),
308 "flavorId": get_res_id(res_cache, RES_FLAVOR, vm["vdu_id"]),
315 if "artifacts" in vm and vm["artifacts"]:
316 param["boot"]["type"] = BOOT_FROM_IMAGE
318 for artifact in vm["artifacts"]:
319 if artifact["artifact_name"] == "sw_image":
320 # TODO: after DM define
321 img_name = artifact["file"]
324 raise VimException("Undefined image(%s)" % vm["artifacts"], ERR_CODE)
325 images = api.list_image(vim_id, tenant_id)
326 for image in images["images"]:
327 if img_name == image["name"]:
328 param["boot"]["imageId"] = image["id"]
330 if "imageId" not in param["boot"]:
331 raise VimException("Undefined artifacts image(%s)" % vm["artifacts"], ERR_CODE)
332 elif vm["volume_storages"]:
333 param["boot"]["type"] = BOOT_FROM_VOLUME
334 vol_id = vm["volume_storages"][0]["volume_storage_id"]
335 param["boot"]["volumeId"] = get_res_id(res_cache, RES_VOLUME, vol_id)
337 raise VimException("No image and volume defined", ERR_CODE)
339 for cp_id in ignore_case_get(vm, "cps"):
340 param["nicArray"].append({
341 "portId": get_res_id(res_cache, RES_PORT, cp_id)
343 param["contextArray"] = ignore_case_get(vm["properties"], "inject_files")
344 logger.debug("contextArray:%s", param["contextArray"])
345 for vol_data in ignore_case_get(vm, "volume_storages"):
346 vol_id = vol_data["volume_storage_id"]
347 param["volumeArray"].append({
348 "volumeId": get_res_id(res_cache, RES_VOLUME, vol_id)
351 set_opt_val(param, "availabilityZone", ignore_case_get(location_info, "availability_zone"))
352 set_opt_val(param, "userdata", ignore_case_get(vm["properties"], "user_data"))
353 set_opt_val(param, "metadata", ignore_case_get(vm["properties"], "meta_data"))
354 set_opt_val(param, "securityGroups", "") # TODO List of names of security group
355 set_opt_val(param, "serverGroup", "") # TODO the ServerGroup for anti-affinity and affinity
357 ret = api.create_vm(vim_id, tenant_id, param)
358 do_notify(res_type, ret)
360 if ignore_case_get(ret, "name"):
361 vm_name = vm["properties"].get("name", "undefined")
362 logger.debug("vm_name:%s" % vm_name)
363 opt_vm_status = "Timeout"
364 retry_count, max_retry_count = 0, 100
365 while retry_count < max_retry_count:
366 vm_info = api.get_vm(vim_id, tenant_id, vm_id)
367 if vm_info["status"].upper() == "ACTIVE":
368 logger.debug("Vm(%s) is active", vim_id)
370 if vm_info["status"].upper() == "ERROR":
371 opt_vm_status = vm_info["status"]
374 retry_count = retry_count + 1
375 raise VimException("Failed to create Vm(%s): %s." % (vm_name, opt_vm_status), ERR_CODE)