Code Coverage

Cobertura Coverage Report > osm_nbi >

pmjobs_topics.py

Trend

Classes100%
 
Lines94%
   
Conditionals100%
 

File Coverage summary

NameClassesLinesConditionals
pmjobs_topics.py
100%
1/1
94%
58/62
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
pmjobs_topics.py
94%
58/62
N/A

Source

osm_nbi/pmjobs_topics.py
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
16 1 import asyncio
17 1 import aiohttp
18 1 from http import HTTPStatus
19 1 from urllib.parse import quote
20 1 from osm_nbi.base_topic import EngineException
21
22 1 __author__ = "Vijay R S <vijay.r@tataelxsi.co.in>"
23
24
25 1 class PmJobsTopic():
26 1     def __init__(self, db, host=None, port=None):
27 1         self.db = db
28 1         self.url = 'http://{}:{}'.format(host, port)
29 1         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']
32
33 1     def _get_vnf_metric_list(self, ns_id):
34 1         metric_list = self.nfvi_metric_list.copy()
35 1         vnfr_desc = self.db.get_list("vnfrs", {"nsr-id-ref": ns_id})
36 1         if not vnfr_desc:
37 1             raise EngineException("NS not found with id {}".format(ns_id), http_code=HTTPStatus.NOT_FOUND)
38         else:
39 1             for vnfr in vnfr_desc:
40 1                 vnfd_desc = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]}, fail_on_empty=True, fail_on_more=False)
41 1                 try:
42 1                     configs = vnfd_desc.get("df")[0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"]
43 0                 except Exception:
44 0                     configs = []
45
46 1                 for config in configs:
47 1                     if "metrics" in config:
48 0                         metric_list.extend([quote(metric['name']) for metric in config["metrics"]])
49 1         metric_list = list(set(metric_list))
50 1         return metric_list
51
52 1     async def _prom_metric_request(self, ns_id, metrics_list):
53 1         try:
54 1             async with aiohttp.ClientSession() as session:
55 1                 data = []
56 1                 for metlist in metrics_list:
57 1                     request_url = self.url+'/api/v1/query?query=osm_'+metlist+"{ns_id='"+ns_id+"'}"
58 1                     async with session.get(request_url) as resp:
59 1                         resp = await resp.json()
60 1                         resp = resp['data']['result']
61 1                         if resp:
62 1                             data.append(resp)
63 1                 return data
64 1         except aiohttp.client_exceptions.ClientConnectorError as e:
65 1             raise EngineException("Connection to '{}'Failure: {}".format(self.url, e))
66
67 1     def show(self, session, ns_id, api_req=False):
68 1         metrics_list = self._get_vnf_metric_list(ns_id)
69 1         loop = asyncio.new_event_loop()
70 1         asyncio.set_event_loop(loop)
71 1         prom_metric = loop.run_until_complete(self._prom_metric_request(ns_id, metrics_list))
72 1         metric = {}
73 1         metric_temp = []
74 1         for index_list in prom_metric:
75 1             for index in index_list:
76 1                 process_metric = {'performanceValue': {'performanceValue': {}}}
77 1                 process_metric['objectInstanceId'] = index['metric']['ns_id']
78 1                 process_metric['performanceMetric'] = index['metric']['__name__']
79 1                 process_metric['performanceValue']['timestamp'] = index['value'][0]
80 1                 process_metric['performanceValue']['performanceValue']['performanceValue'] = index['value'][1]
81 1                 process_metric['performanceValue']['performanceValue']['vnfMemberIndex'] \
82                     = index['metric']['vnf_member_index']
83 1                 if 'vdu_name' not in index['metric']:
84 0                     pass
85                 else:
86 1                     process_metric['performanceValue']['performanceValue']['vduName'] = index['metric']['vdu_name']
87 1                 metric_temp.append(process_metric)
88 1         metric['entries'] = metric_temp
89 1         return metric