1 # -*- coding: utf-8 -*-
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from http
import HTTPStatus
19 from urllib
.parse
import quote
20 from osm_nbi
.base_topic
import EngineException
22 __author__
= "Vijay R S <vijay.r@tataelxsi.co.in>"
26 def __init__(self
, db
, host
=None, port
=None):
28 self
.url
= "http://{}:{}".format(host
, port
)
29 self
.nfvi_metric_list
= [
31 "average_memory_utilization",
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
})
45 raise EngineException(
46 "NS not found with id {}".format(ns_id
), http_code
=HTTPStatus
.NOT_FOUND
49 for vnfr
in vnfr_desc
:
50 vnfd_desc
= self
.db
.get_one(
52 {"_id": vnfr
["vnfd-id"]},
57 configs
= vnfd_desc
.get("df")[0]["lcm-operations-configuration"][
58 "operate-vnf-op-config"
63 for config
in configs
:
64 if "metrics" in config
:
66 [quote(metric
["name"]) for metric
in config
["metrics"]]
68 metric_list
= list(set(metric_list
))
71 async def _prom_metric_request(self
, ns_id
, metrics_list
):
73 async with aiohttp
.ClientSession() as session
:
75 for metlist
in metrics_list
:
78 + "/api/v1/query?query=osm_"
84 async with session
.get(request_url
) as resp
:
85 resp
= await resp
.json()
86 resp
= resp
["data"]["result"]
90 except aiohttp
.client_exceptions
.ClientConnectorError
as e
:
91 raise EngineException("Connection to '{}'Failure: {}".format(self
.url
, e
))
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
)
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"][
110 ] = index
["value"][1]
111 process_metric
["performanceValue"]["performanceValue"][
113 ] = index
["metric"]["vnf_member_index"]
114 if "vdu_name" not in index
["metric"]:
117 process_metric
["performanceValue"]["performanceValue"][
119 ] = index
["metric"]["vdu_name"]
120 metric_temp
.append(process_metric
)
121 metric
["entries"] = metric_temp