Coverage for osm_nbi/pmjobs_topics.py: 94%

62 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2024-06-30 10:14 +0000

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 

16import asyncio 

17import aiohttp 

18from http import HTTPStatus 

19from urllib.parse import quote 

20from osm_nbi.base_topic import EngineException 

21 

22__author__ = "Vijay R S <vijay.r@tataelxsi.co.in>" 

23 

24 

25class PmJobsTopic: 

26 def __init__(self, db, host=None, port=None): 

27 self.db = db 

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 ] 

40 

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: 

45 raise EngineException( 

46 "NS not found with id {}".format(ns_id), http_code=HTTPStatus.NOT_FOUND 

47 ) 

48 else: 

49 for vnfr in vnfr_desc: 

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 ) 

56 try: 

57 configs = vnfd_desc.get("df")[0]["lcm-operations-configuration"][ 

58 "operate-vnf-op-config" 

59 ]["day1-2"] 

60 except Exception: 

61 configs = [] 

62 

63 for config in configs: 

64 if "metrics" in config: 

65 metric_list.extend( 

66 [quote(metric["name"]) for metric in config["metrics"]] 

67 ) 

68 metric_list = list(set(metric_list)) 

69 return metric_list 

70 

71 async def _prom_metric_request(self, ns_id, metrics_list): 

72 try: 

73 async with aiohttp.ClientSession() as session: 

74 data = [] 

75 for metlist in metrics_list: 

76 request_url = ( 

77 self.url 

78 + "/api/v1/query?query=osm_" 

79 + metlist 

80 + "{ns_id='" 

81 + ns_id 

82 + "'}" 

83 ) 

84 async with session.get(request_url) as resp: 

85 resp = await resp.json() 

86 resp = resp["data"]["result"] 

87 if resp: 

88 data.append(resp) 

89 return data 

90 except aiohttp.client_exceptions.ClientConnectorError as e: 

91 raise EngineException("Connection to '{}'Failure: {}".format(self.url, e)) 

92 

93 def show(self, session, ns_id, filter_q=None, api_req=False): 

94 metrics_list = self._get_vnf_metric_list(ns_id) 

95 loop = asyncio.new_event_loop() 

96 asyncio.set_event_loop(loop) 

97 prom_metric = loop.run_until_complete( 

98 self._prom_metric_request(ns_id, metrics_list) 

99 ) 

100 metric = {} 

101 metric_temp = [] 

102 for index_list in prom_metric: 

103 for index in index_list: 

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"]: 

115 pass 

116 else: 

117 process_metric["performanceValue"]["performanceValue"][ 

118 "vduName" 

119 ] = index["metric"]["vdu_name"] 

120 metric_temp.append(process_metric) 

121 metric["entries"] = metric_temp 

122 return metric