Fix [Bug 675] User is able to delete NS instance before deleting NetworkSlice intance
[osm/NBI.git] / 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
17 import asyncio
18 import aiohttp
19 from base_topic import EngineException
20
21 __author__ = "Vijay R S <vijay.r@tataelxsi.co.in>"
22
23
24 class PmJobsTopic():
25 def __init__(self, host=None, port=None):
26 self.url = 'http://{}:{}'.format(host, port)
27 self.metric_list = ['cpu_utilization', 'average_memory_utilization', 'disk_read_ops',
28 'disk_write_ops', 'disk_read_bytes', 'disk_write_bytes', 'packets_dropped',
29 'packets_sent', 'packets_received']
30
31 async def _prom_metric_request(self, ns_id):
32 try:
33 async with aiohttp.ClientSession() as session:
34 data = []
35 for metlist in self.metric_list:
36 request_url = self.url+'/api/v1/query?query=osm_'+metlist+"{ns_id='"+ns_id+"'}"
37 async with session.get(request_url) as resp:
38 resp = await resp.json()
39 resp = resp['data']['result']
40 if resp:
41 data.append(resp)
42 return data
43 except aiohttp.client_exceptions.ClientConnectorError as e:
44 raise EngineException("Connection Failure: {}".format(e))
45
46 def show(self, session, ns_id):
47 loop = asyncio.new_event_loop()
48 asyncio.set_event_loop(loop)
49 prom_metric = loop.run_until_complete(self._prom_metric_request(ns_id))
50 metric = {}
51 metric_temp = []
52 for index_list in prom_metric:
53 for index in index_list:
54 process_metric = {'performanceValue': {'performanceValue': {}}}
55 process_metric['objectInstanceId'] = index['metric']['ns_id']
56 process_metric['performanceMetric'] = index['metric']['__name__']
57 process_metric['performanceValue']['timestamp'] = index['value'][0]
58 process_metric['performanceValue']['performanceValue']['performanceValue'] = index['value'][1]
59 process_metric['performanceValue']['performanceValue']['vnfMemberIndex'] \
60 = index['metric']['vnf_member_index']
61 process_metric['performanceValue']['performanceValue']['vduName'] = index['metric']['vdu_name']
62 metric_temp.append(process_metric)
63 metric['entries'] = metric_temp
64 return metric