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 = [
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         ]
40
41 1     def _get_vnf_metric_list(self, ns_id):
42 1         metric_list = self.nfvi_metric_list.copy()
43 1         vnfr_desc = self.db.get_list("vnfrs", {"nsr-id-ref": ns_id})
44 1         if not vnfr_desc:
45 1             raise EngineException(
46                 "NS not found with id {}".format(ns_id), http_code=HTTPStatus.NOT_FOUND
47             )
48         else:
49 1             for vnfr in vnfr_desc:
50 1                 vnfd_desc = self.db.get_one(
51                     "vnfds",
52                     {"_id": vnfr["vnfd-id"]},
53                     fail_on_empty=True,
54                     fail_on_more=False,
55                 )
56 1                 try:
57 1                     configs = vnfd_desc.get("df")[0]["lcm-operations-configuration"][
58                         "operate-vnf-op-config"
59                     ]["day1-2"]
60 0                 except Exception:
61 0                     configs = []
62
63 1                 for config in configs:
64 1                     if "metrics" in config:
65 0                         metric_list.extend(
66                             [quote(metric["name"]) for metric in config["metrics"]]
67                         )
68 1         metric_list = list(set(metric_list))
69 1         return metric_list
70
71 1     async def _prom_metric_request(self, ns_id, metrics_list):
72 1         try:
73 1             async with aiohttp.ClientSession() as session:
74 1                 data = []
75 1                 for metlist in metrics_list:
76 1                     request_url = (
77                         self.url
78                         + "/api/v1/query?query=osm_"
79                         + metlist
80                         + "{ns_id='"
81                         + ns_id
82                         + "'}"
83                     )
84 1                     async with session.get(request_url) as resp:
85 1                         resp = await resp.json()
86 1                         resp = resp["data"]["result"]
87 1                         if resp:
88 1                             data.append(resp)
89 1                 return data
90 1         except aiohttp.client_exceptions.ClientConnectorError as e:
91 1             raise EngineException("Connection to '{}'Failure: {}".format(self.url, e))
92
93 1     def show(self, session, ns_id, filter_q=None, api_req=False):
94 1         metrics_list = self._get_vnf_metric_list(ns_id)
95 1         loop = asyncio.new_event_loop()
96 1         asyncio.set_event_loop(loop)
97 1         prom_metric = loop.run_until_complete(
98             self._prom_metric_request(ns_id, metrics_list)
99         )
100 1         metric = {}
101 1         metric_temp = []
102 1         for index_list in prom_metric:
103 1             for index in index_list:
104 1                 process_metric = {"performanceValue": {"performanceValue": {}}}
105 1                 process_metric["objectInstanceId"] = index["metric"]["ns_id"]
106 1                 process_metric["performanceMetric"] = index["metric"]["__name__"]
107 1                 process_metric["performanceValue"]["timestamp"] = index["value"][0]
108 1                 process_metric["performanceValue"]["performanceValue"][
109                     "performanceValue"
110                 ] = index["value"][1]
111 1                 process_metric["performanceValue"]["performanceValue"][
112                     "vnfMemberIndex"
113                 ] = index["metric"]["vnf_member_index"]
114 1                 if "vdu_name" not in index["metric"]:
115 0                     pass
116                 else:
117 1                     process_metric["performanceValue"]["performanceValue"][
118                         "vduName"
119                     ] = index["metric"]["vdu_name"]
120 1                 metric_temp.append(process_metric)
121 1         metric["entries"] = metric_temp
122 1         return metric