X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2FROclient.py;h=54dbbf1f00e9d8100cf40024512b811dd06835d1;hb=b8a35ddafa3852c2d12ff58b42927a53c396e457;hp=d0bef3318ed9047a06f6b8ccff2a627d90daf9db;hpb=911ff7dfb3ed1164e52d1e8ac90d029cc548fe3b;p=osm%2FLCM.git diff --git a/osm_lcm/ROclient.py b/osm_lcm/ROclient.py index d0bef33..54dbbf1 100644 --- a/osm_lcm/ROclient.py +++ b/osm_lcm/ROclient.py @@ -3,8 +3,6 @@ ## # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. -# This file is part of openmano -# All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -18,8 +16,6 @@ # License for the specific language governing permissions and limitations # under the License. # -# For those usages not covered by the Apache License, Version 2.0 please -# contact with: nfvlabs@tid.es ## """ @@ -87,6 +83,12 @@ def remove_envelop(item, indata=None): elif item in ("vim", "vim_account", "datacenters"): if len(indata) == 1 and "datacenter" in indata: clean_indata = indata["datacenter"] + elif item == "wim": + if len(indata) == 1 and "wim" in indata: + clean_indata = indata["wim"] + elif item == "wim_account": + if len(indata) == 1 and "wim_account" in indata: + clean_indata = indata["wim_account"] elif item == "ns" or item == "instances": if len(indata) == 1 and "instance" in indata: clean_indata = indata["instance"] @@ -99,7 +101,7 @@ def remove_envelop(item, indata=None): class ROClient: headers_req = {'Accept': 'application/yaml', 'content-type': 'application/yaml'} client_to_RO = {'tenant': 'tenants', 'vim': 'datacenters', 'vim_account': 'datacenters', 'sdn': 'sdn_controllers', - 'vnfd': 'vnfs', 'nsd': 'scenarios', + 'vnfd': 'vnfs', 'nsd': 'scenarios', 'wim': 'wims', 'wim_account': 'wims', 'ns': 'instances'} mandatory_for_create = { 'tenant': ("name", ), @@ -107,8 +109,10 @@ class ROClient: 'nsd': ("name", "id"), 'ns': ("name", "scenario", "datacenter"), 'vim': ("name", "vim_url"), + 'wim': ("name", "wim_url"), 'vim_account': (), - 'sdn': ("name", "port", 'ip', 'dpid', 'type'), + 'wim_account': (), + 'sdn': ("name", 'type'), } timeout_large = 120 timeout_short = 30 @@ -185,7 +189,7 @@ class ROClient: def _parse_yaml(self, descriptor, response=False): try: - return yaml.load(descriptor) + return yaml.load(descriptor, Loader=yaml.Loader) except yaml.YAMLError as exc: error_pos = "" if hasattr(exc, 'problem_mark'): @@ -225,6 +229,10 @@ class ROClient: return {'tenant': indata} elif item in ("vim", "vim_account", "datacenter"): return {'datacenter': indata} + elif item == "wim": + return {'wim': indata} + elif item == "wim_account": + return {'wim_account': indata} elif item == "ns" or item == "instances": return {'instance': indata} elif item == "sdn": @@ -272,29 +280,65 @@ class ROClient: :param ns_descriptor: instance descriptor obtained with self.show("ns", ) :return: status, message: status can be BUILD,ACTIVE,ERROR, message is a text message """ - net_total = 0 - vm_total = 0 - net_done = 0 - vm_done = 0 + error_list = [] + total = {"VMs": 0, "networks": 0, "SDN_networks": 0} + done = {"VMs": 0, "networks": 0, "SDN_networks": 0} + + def _get_ref(desc): + # return an identification for the network or vm. Try vim_id if exist, if not descriptor id for net + if desc.get("vim_net_id"): + return "'vim-id={}'".format(desc["vim_net_id"]) + elif desc.get("ns_net_osm_id"): + return "'nsd-vld-id={}'".format(desc["ns_net_osm_id"]) + elif desc.get("vnf_net_osm_id"): + return "'vnfd-vld-id={}'".format(desc["vnf_net_osm_id"]) + # for VM + elif desc.get("vim_vm_id"): + return "'vim-id={}'".format(desc["vim_vm_id"]) + elif desc.get("vdu_osm_id"): + return "'vnfd-vdu-id={}'".format(desc["vdu_osm_id"]) + else: + return "" - for net in ns_descriptor["nets"]: - net_total += 1 - if net["status"] in ("ERROR", "VIM_ERROR"): - return "ERROR", net["error_msg"] - elif net["status"] == "ACTIVE": - net_done += 1 - for vnf in ns_descriptor["vnfs"]: - for vm in vnf["vms"]: - vm_total += 1 - if vm["status"] in ("ERROR", "VIM_ERROR"): - return "ERROR", vm["error_msg"] - elif vm["status"] == "ACTIVE": - vm_done += 1 - - if net_total == net_done and vm_total == vm_done: - return "ACTIVE", "VMs {}, networks: {}".format(vm_total, net_total) - else: - return "BUILD", "VMs: {}/{}, networks: {}/{}".format(vm_done, vm_total, net_done, net_total) + def _get_sdn_ref(sce_net_id): + # look for the network associated to the SDN network and obtain the identification + net = next((x for x in ns_descriptor["nets"] if x.get("sce_net_id") == sce_net_id), None) + if not sce_net_id or not net: + return "" + return _get_ref(net) + + try: + total["networks"] = len(ns_descriptor["nets"]) + for net in ns_descriptor["nets"]: + if net["status"] in ("ERROR", "VIM_ERROR"): + error_list.append("VIM network ({}) on error: {}".format(_get_ref(net), net["error_msg"])) + elif net["status"] == "ACTIVE": + done["networks"] += 1 + + total["SDN_networks"] = len(ns_descriptor["sdn_nets"]) + for sdn_net in ns_descriptor["sdn_nets"]: + if sdn_net["status"] in ("ERROR", "VIM_ERROR", "WIM_ERROR"): + error_list.append("SDN network ({}) on error: {}".format(_get_sdn_ref(sdn_net.get("sce_net_id")), + sdn_net["error_msg"])) + elif sdn_net["status"] == "ACTIVE": + done["SDN_networks"] += 1 + + for vnf in ns_descriptor["vnfs"]: + for vm in vnf["vms"]: + total["VMs"] += 1 + if vm["status"] in ("ERROR", "VIM_ERROR"): + error_list.append("VIM VM ({}) on error: {}".format(_get_ref(vm), vm["error_msg"])) + elif vm["status"] == "ACTIVE": + done["VMs"] += 1 + if error_list: + return "ERROR", "; ".join(error_list) + if all(total[x] == done[x] for x in total): # DONE == TOTAL for all items + return "ACTIVE", str({x: total[x] for x in total if total[x]}) # print only those which value is not 0 + else: + return "BUILD", str({x: "{}/{}".format(done[x], total[x]) for x in total if total[x]}) + # print done/total for each item if total is not 0 + except Exception as e: + raise ROClientException("Unexpected RO ns descriptor. Wrong version? {}".format(e)) from e @staticmethod def check_action_status(action_descriptor): @@ -311,7 +355,7 @@ class ROClient: other_done = 0 for vim_action_set in action_descriptor["actions"]: - for vim_action in vim_action_set["vim_actions"]: + for vim_action in vim_action_set["vim_wim_actions"]: if vim_action["item"] == "instance_vms": vm_total += 1 elif vim_action["item"] == "instance_nets": @@ -320,7 +364,7 @@ class ROClient: other_total += 1 if vim_action["status"] == "FAILED": return "ERROR", vim_action["error_msg"] - elif vim_action["status"] in ("DONE", "SUPERSEDED"): + elif vim_action["status"] in ("DONE", "SUPERSEDED", "FINISHED"): if vim_action["item"] == "instance_vms": vm_done += 1 elif vim_action["item"] == "instance_nets": @@ -527,7 +571,7 @@ class ROClient: if not self.check_if_uuid(item_id_name): # check that exist _all_tenants = all_tenants - if item == "datacenters": + if item in ("datacenters", 'wims'): _all_tenants = True uuid = await self._get_item_uuid(session, item, item_id_name, all_tenants=_all_tenants) else: @@ -609,7 +653,7 @@ class ROClient: for word in str(response_text).split(" "): if "." in word: version_text, _, _ = word.partition("-") - return list(map(int, version_text.split("."))) + return version_text raise ROClientException("Got invalid version text: '{}'".format(response_text), http_code=500) except aiohttp.errors.ClientOSError as e: raise ROClientException(e, http_code=504) @@ -689,7 +733,7 @@ class ROClient: try: if item not in self.client_to_RO: raise ROClientException("Invalid item {}".format(item)) - if item == 'tenant' or item == 'vim': + if item in ('tenant', 'vim', 'wim'): all_tenants = None with aiohttp.ClientSession(loop=self.loop) as session: @@ -785,7 +829,7 @@ class ROClient: raise ROClientException("'{}' is mandatory parameter for {}".format(mandatory, item)) all_tenants = False - if item in ('tenant', 'vim'): + if item in ('tenant', 'vim', 'wim'): all_tenants = None create_desc = self._create_envelop(item, desc) @@ -854,8 +898,16 @@ class ROClient: except asyncio.TimeoutError: raise ROClientException("Timeout", http_code=504) - async def attach_datacenter(self, datacenter=None, descriptor=None, descriptor_format=None, **kwargs): - + async def attach(self, item, item_id_name=None, descriptor=None, descriptor_format=None, **kwargs): + """ + Attach a datacenter or wim to a tenant, creating a vim_account, wim_account + :param item: can be vim_account or wim_account + :param item_id_name: id or name of the datacenter, wim + :param descriptor: + :param descriptor_format: + :param kwargs: + :return: + """ try: if isinstance(descriptor, str): descriptor = self._parse(descriptor, descriptor_format) @@ -863,7 +915,8 @@ class ROClient: pass else: descriptor = {} - desc = remove_envelop("vim", descriptor) + + desc = remove_envelop(item, descriptor) # # check that exist # uuid = self._get_item_uuid(session, "datacenters", uuid_name, all_tenants=True) @@ -871,17 +924,22 @@ class ROClient: if kwargs: desc = self.update_descriptor(desc, kwargs) - if not desc.get("vim_tenant_name") and not desc.get("vim_tenant_id"): - raise ROClientException("Wrong descriptor. At least vim_tenant_name or vim_tenant_id must be provided") - create_desc = self._create_envelop("vim", desc) + if item == "vim_account": + if not desc.get("vim_tenant_name") and not desc.get("vim_tenant_id"): + raise ROClientException("Wrong descriptor. At least vim_tenant_name or vim_tenant_id must be " + "provided") + elif item != "wim_account": + raise ROClientException("Attach with unknown item {}. Must be 'vim_account' or 'wim_account'". + format(item)) + create_desc = self._create_envelop(item, desc) payload_req = yaml.safe_dump(create_desc) with aiohttp.ClientSession(loop=self.loop) as session: # check that exist - item_id = await self._get_item_uuid(session, "datacenters", datacenter, all_tenants=True) + item_id = await self._get_item_uuid(session, self.client_to_RO[item], item_id_name, all_tenants=True) await self._get_tenant(session) - url = "{}/{tenant}/datacenters/{datacenter}".format(self.endpoint_url, tenant=self.tenant, - datacenter=item_id) + url = "{}/{tenant}/{item}/{item_id}".format(self.endpoint_url, tenant=self.tenant, + item=self.client_to_RO[item], item_id=item_id) self.logger.debug("RO POST %s %s", url, payload_req) with aiohttp.Timeout(self.timeout_large): async with session.post(url, headers=self.headers_req, data=payload_req) as response: @@ -891,23 +949,23 @@ class ROClient: raise ROClientException(response_text, http_code=response.status) response_desc = self._parse_yaml(response_text, response=True) - desc = remove_envelop("vim", response_desc) + desc = remove_envelop(item, response_desc) return desc except aiohttp.errors.ClientOSError as e: raise ROClientException(e, http_code=504) except asyncio.TimeoutError: raise ROClientException("Timeout", http_code=504) - async def detach_datacenter(self, datacenter=None): + async def detach(self, item, item_id_name=None): # TODO replace the code with delete_item(vim_account,...) try: with aiohttp.ClientSession(loop=self.loop) as session: # check that exist - item_id = await self._get_item_uuid(session, "datacenters", datacenter, all_tenants=False) + item_id = await self._get_item_uuid(session, self.client_to_RO[item], item_id_name, all_tenants=False) tenant = await self._get_tenant(session) - url = "{}/{tenant}/datacenters/{datacenter}".format(self.endpoint_url, tenant=tenant, - datacenter=item_id) + url = "{}/{tenant}/{item}/{datacenter}".format(self.endpoint_url, tenant=tenant, + item=self.client_to_RO[item], datacenter=item_id) self.logger.debug("RO DELETE %s", url) with aiohttp.Timeout(self.timeout_large): async with session.delete(url, headers=self.headers_req) as response: @@ -917,7 +975,7 @@ class ROClient: raise ROClientException(response_text, http_code=response.status) response_desc = self._parse_yaml(response_text, response=True) - desc = remove_envelop("vim", response_desc) + desc = remove_envelop(item, response_desc) return desc except aiohttp.errors.ClientOSError as e: raise ROClientException(e, http_code=504)