| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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 |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 16 | import asyncio |
| 17 | import aiohttp |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 18 | from http import HTTPStatus |
| 19 | from urllib.parse import quote |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 20 | from osm_nbi.base_topic import EngineException |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 21 | |
| 22 | __author__ = "Vijay R S <vijay.r@tataelxsi.co.in>" |
| 23 | |
| 24 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 25 | class PmJobsTopic: |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 26 | def __init__(self, db, host=None, port=None): |
| 27 | self.db = db |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 28 | self.url = "http://{}:{}".format(host, port) |
| 29 | self.nfvi_metric_list = [ |
| 30 | "cpu_utilization", |
| 31 | "average_memory_utilization", |
| 32 | "disk_read_ops", |
| 33 | "disk_write_ops", |
| 34 | "disk_read_bytes", |
| 35 | "disk_write_bytes", |
| 36 | "packets_dropped", |
| 37 | "packets_sent", |
| 38 | "packets_received", |
| 39 | ] |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 40 | |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 41 | def _get_vnf_metric_list(self, ns_id): |
| 42 | metric_list = self.nfvi_metric_list.copy() |
| 43 | vnfr_desc = self.db.get_list("vnfrs", {"nsr-id-ref": ns_id}) |
| 44 | if not vnfr_desc: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 45 | raise EngineException( |
| 46 | "NS not found with id {}".format(ns_id), http_code=HTTPStatus.NOT_FOUND |
| 47 | ) |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 48 | else: |
| 49 | for vnfr in vnfr_desc: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 50 | vnfd_desc = self.db.get_one( |
| 51 | "vnfds", |
| 52 | {"_id": vnfr["vnfd-id"]}, |
| 53 | fail_on_empty=True, |
| 54 | fail_on_more=False, |
| 55 | ) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 56 | try: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 57 | configs = vnfd_desc.get("df")[0]["lcm-operations-configuration"][ |
| 58 | "operate-vnf-op-config" |
| 59 | ]["day1-2"] |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 60 | except Exception: |
| 61 | configs = [] |
| 62 | |
| 63 | for config in configs: |
| 64 | if "metrics" in config: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 65 | metric_list.extend( |
| 66 | [quote(metric["name"]) for metric in config["metrics"]] |
| 67 | ) |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 68 | metric_list = list(set(metric_list)) |
| 69 | return metric_list |
| 70 | |
| 71 | async def _prom_metric_request(self, ns_id, metrics_list): |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 72 | try: |
| 73 | async with aiohttp.ClientSession() as session: |
| 74 | data = [] |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 75 | for metlist in metrics_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 76 | request_url = ( |
| 77 | self.url |
| 78 | + "/api/v1/query?query=osm_" |
| 79 | + metlist |
| 80 | + "{ns_id='" |
| 81 | + ns_id |
| 82 | + "'}" |
| 83 | ) |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 84 | async with session.get(request_url) as resp: |
| 85 | resp = await resp.json() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 86 | resp = resp["data"]["result"] |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 87 | if resp: |
| 88 | data.append(resp) |
| 89 | return data |
| 90 | except aiohttp.client_exceptions.ClientConnectorError as e: |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 91 | raise EngineException("Connection to '{}'Failure: {}".format(self.url, e)) |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 92 | |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 93 | def show(self, session, ns_id, filter_q=None, api_req=False): |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 94 | metrics_list = self._get_vnf_metric_list(ns_id) |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 95 | loop = asyncio.new_event_loop() |
| 96 | asyncio.set_event_loop(loop) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 97 | prom_metric = loop.run_until_complete( |
| 98 | self._prom_metric_request(ns_id, metrics_list) |
| 99 | ) |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 100 | metric = {} |
| 101 | metric_temp = [] |
| 102 | for index_list in prom_metric: |
| 103 | for index in index_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 104 | process_metric = {"performanceValue": {"performanceValue": {}}} |
| 105 | process_metric["objectInstanceId"] = index["metric"]["ns_id"] |
| 106 | process_metric["performanceMetric"] = index["metric"]["__name__"] |
| 107 | process_metric["performanceValue"]["timestamp"] = index["value"][0] |
| 108 | process_metric["performanceValue"]["performanceValue"][ |
| 109 | "performanceValue" |
| 110 | ] = index["value"][1] |
| 111 | process_metric["performanceValue"]["performanceValue"][ |
| 112 | "vnfMemberIndex" |
| 113 | ] = index["metric"]["vnf_member_index"] |
| 114 | if "vdu_name" not in index["metric"]: |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 115 | pass |
| 116 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 117 | process_metric["performanceValue"]["performanceValue"][ |
| 118 | "vduName" |
| 119 | ] = index["metric"]["vdu_name"] |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 120 | metric_temp.append(process_metric) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 121 | metric["entries"] = metric_temp |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 122 | return metric |