| 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 | |
| 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 |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 28 | self.url = 'http://{}:{}'.format(host, port) |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 29 | self.nfvi_metric_list = ['cpu_utilization', 'average_memory_utilization', 'disk_read_ops', |
| 30 | 'disk_write_ops', 'disk_read_bytes', 'disk_write_bytes', |
| 31 | 'packets_dropped', 'packets_sent', 'packets_received'] |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 32 | |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 33 | def _get_vnf_metric_list(self, ns_id): |
| 34 | metric_list = self.nfvi_metric_list.copy() |
| 35 | vnfr_desc = self.db.get_list("vnfrs", {"nsr-id-ref": ns_id}) |
| 36 | if not vnfr_desc: |
| 37 | raise EngineException("NS not found with id {}".format(ns_id), http_code=HTTPStatus.NOT_FOUND) |
| 38 | else: |
| 39 | for vnfr in vnfr_desc: |
| 40 | vnfd_desc = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]}, fail_on_empty=True, fail_on_more=False) |
| 41 | if vnfd_desc.get("vdu"): |
| 42 | for vdu in vnfd_desc['vdu']: |
| 43 | # Checks for vdu metric in vdu-configuration |
| 44 | if 'vdu-configuration' in vdu and 'metrics' in vdu['vdu-configuration']: |
| 45 | metric_list.extend([quote(metric['name']) |
| 46 | for metric in vdu["vdu-configuration"]["metrics"]]) |
| 47 | # Checks for vnf metric in vnf-configutaion |
| 48 | if 'vnf-configuration' in vnfd_desc and 'metrics' in vnfd_desc['vnf-configuration']: |
| 49 | metric_list.extend([quote(metric['name']) for metric in vnfd_desc["vnf-configuration"]["metrics"]]) |
| 50 | metric_list = list(set(metric_list)) |
| 51 | return metric_list |
| 52 | |
| 53 | async def _prom_metric_request(self, ns_id, metrics_list): |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 54 | try: |
| 55 | async with aiohttp.ClientSession() as session: |
| 56 | data = [] |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 57 | for metlist in metrics_list: |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 58 | request_url = self.url+'/api/v1/query?query=osm_'+metlist+"{ns_id='"+ns_id+"'}" |
| 59 | async with session.get(request_url) as resp: |
| 60 | resp = await resp.json() |
| 61 | resp = resp['data']['result'] |
| 62 | if resp: |
| 63 | data.append(resp) |
| 64 | return data |
| 65 | except aiohttp.client_exceptions.ClientConnectorError as e: |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 66 | raise EngineException("Connection to '{}'Failure: {}".format(self.url, e)) |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 67 | |
| 68 | def show(self, session, ns_id): |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 69 | metrics_list = self._get_vnf_metric_list(ns_id) |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 70 | loop = asyncio.new_event_loop() |
| 71 | asyncio.set_event_loop(loop) |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 72 | prom_metric = loop.run_until_complete(self._prom_metric_request(ns_id, metrics_list)) |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 73 | metric = {} |
| 74 | metric_temp = [] |
| 75 | for index_list in prom_metric: |
| 76 | for index in index_list: |
| 77 | process_metric = {'performanceValue': {'performanceValue': {}}} |
| 78 | process_metric['objectInstanceId'] = index['metric']['ns_id'] |
| 79 | process_metric['performanceMetric'] = index['metric']['__name__'] |
| 80 | process_metric['performanceValue']['timestamp'] = index['value'][0] |
| 81 | process_metric['performanceValue']['performanceValue']['performanceValue'] = index['value'][1] |
| 82 | process_metric['performanceValue']['performanceValue']['vnfMemberIndex'] \ |
| 83 | = index['metric']['vnf_member_index'] |
| preethika.p | 0952a48 | 2019-09-20 16:37:50 +0530 | [diff] [blame] | 84 | if 'vdu_name' not in index['metric']: |
| 85 | pass |
| 86 | else: |
| 87 | process_metric['performanceValue']['performanceValue']['vduName'] = index['metric']['vdu_name'] |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 88 | metric_temp.append(process_metric) |
| 89 | metric['entries'] = metric_temp |
| 90 | return metric |